我试图加载一个名为“音频”的字节类对象,作为torchaudio对象加载:
def convert_audio(audio, target_sr: int = 16000):
wav, sr = torchaudio.load(audio)
#(...) some other code我在网上找不到任何关于如何在Torchaudio中加载字节音频对象的说明的文档,它似乎只接受路径字符串。但是我必须在我的应用程序中保存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,发布于 2022-11-29 02:46:42
如果它是WAV格式,torchaudio.load应该能够从类似文件的对象中解码它.你的代码片段在我看来不错。
下面的教程用不同的类文件对象演示它。
然而,它不起作用的原因有很多。例如,您的文件类对象的光标是否指向正确的位置(音频数据的开头)?read方法是否符合io.RawIOBase.read协议?
在没有看到错误堆栈跟踪的情况下,很难说。
https://stackoverflow.com/questions/74605909
复制相似问题