【问题标题】:How do I load a bytes object WAV audio file in torchaudio?如何在 torchaudio 中加载字节对象 WAV 音频文件?
【发布时间】:2022-11-29 21:42:13
【问题描述】:

我正在尝试加载一个名为“audio”的字节类对象作为 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,

【问题讨论】:

    标签: python torch torchaudio


    【解决方案1】:

    如果它是 WAV 格式,torchaudio.load 应该能够从类似文件的对象中解码它。你的代码 sn-p 对我来说看起来不错。

    以下教程使用不同的类文件对象对其进行了演示。

    https://pytorch.org/audio/0.13.0/tutorials/audio_io_tutorial.html#loading-from-file-like-object

    不过,它不起作用的原因有很多。例如,您的文件类对象的光标是否指向正确的位置(音频数据的开头)? read方法是否符合io.RawIOBase.read协议?

    如果没有看到错误堆栈跟踪,很难说清楚。

    【讨论】:

    • 您好,我收到此错误:发生异常:AttributeError(注意:显示了完整的异常跟踪,但执行暂停于:_run_module_as_main)“bytes”对象没有属性“seek”。您只能从可搜索的文件中加载 torch.load。请将数据预加载到像 io.BytesIO 这样的缓冲区中,然后尝试从中加载。文件“/home/felipe/.local/lib/python3.10/site-packages/torch/serialization.py”,第 348 行,在 _check_seekable f.seek(f.tell()) 我将添加到主要评论
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-23
    • 2022-08-24
    • 1970-01-01
    • 1970-01-01
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多