![](/img/trans.png)
[英]torchaudio “RuntimeError: Error loading audio file: failed to open file” for wav file
[英]How do I load a bytes object WAV audio file in torchaudio?
我正在嘗試加載一個名為“音頻”的字節類 object 以作為 torchaudio object 加載:
def convert_audio(audio, target_sr: int = 16000):
wav, sr = torchaudio.load(audio)
#(...) some other code
我無法在網上找到任何有關如何在 Torchaudio 中加載字節音頻 object 的說明的文檔,它似乎只接受路徑字符串。 但是我必須在我的應用程序中保存 I/O,我不能寫入和加載.wav 文件,只能直接處理音頻對象。
在這種情況下有人有建議嗎?
如果我直接使用音頻,我會得到這個錯誤:
Exception has occurred: AttributeError (note: full exception trace is shown but execution is paused at: _run_module_as_main)
'bytes' object has no attribute 'seek'. You can only torch.load from a file that is seekable. Please pre-load the data into a buffer like io.BytesIO and try to load from it instead.
File "/home/felipe/.local/lib/python3.10/site-packages/torch/serialization.py", line 348, in _check_seekable
f.seek(f.tell())
使用 BytesIO:
Exception has occurred: UnpicklingError (note: full exception trace is shown but execution is paused at: _run_module_as_main)
invalid load key, '\x00'.
File "/home/felipe/.local/lib/python3.10/site-packages/torch/serialization.py", line 1002, in _legacy_load
magic_number = pickle_module.load(f, **pickle_load_args)
File "/home/felipe/.local/lib/python3.10/site-packages/torch/serialization.py", line 795, in load
return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args)
File "/home/felipe/Coding projects/silero/stt.py", line 35, in convert_audio
wav,sr = torch.load(io.BytesIO(audio))
File "/home/felipe/Coding projects/silero/stt.py", line 60, in transcribe
input = prepare_model_input(convert_audio(audio),
File "/home/felipe/Coding projects/silero/psgui.py", line 97, in <module>
transcripton = stt.transcribe('en',audio)
File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
exec(code, run_globals)
File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main (Current frame)
return _run_code(code, main_globals, None,
如果它是 WAV 格式, torchaudio.load
應該能夠從類似 object 的文件中解碼它。你的代碼片段對我來說看起來不錯。
以下教程使用不同的類文件對象對其進行了演示。
https://pytorch.org/audio/0.13.0/tutorials/audio_io_tutorial.html#loading-from-file-like-object
不過,它不起作用的原因有很多。 例如,您的類文件對象的 cursor 是否指向正確的 position(音頻數據的開頭)? read
方法是否符合io.RawIOBase.read協議?
如果沒有看到錯誤堆棧跟蹤,很難說清楚。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.