为什么SpeechRecognition库不能处理临时音频文件?

2024-05-17 19:20:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我的目标是从客户端接收一个音频文件,并将其转录成文本,而无需永久保存到磁盘。我使用的是SpeechRecognition库,如果上传的文件被保存了,它会工作得很好,但是当我尝试使用临时文件时,它会导致错误。你知道吗

来自CherryPy docs:

When a client uploads a file to a CherryPy application, it’s placed on disk immediately. CherryPy will pass it to your exposed method as an argument; that arg will have a “file” attribute, which is a handle to the temporary uploaded file

由于sr.AudioFile构造函数也接受“类似文件的对象”,因此我使用io.BytesIO将临时文件的内容传递给构造函数。你知道吗

以下是我的服务器代码:

import io
import cherrypy
import speech_recognition as sr

class HttpServer(object):    
    @cherrypy.expose
    def index(self, ufile):
        ufile_content = ufile.file.read() # reading temp file
        buffer = io.BytesIO(ufile_content)
        buffer.seek(0)
        with sr.AudioFile(buffer) as source:
            audio = self.recognizer.record(source)
        result = self.recognizer.recognize_google(audio, language='ru-RU')

我正在使用以下代码向服务器发送请求:

import requests
url = 'http://localhost:8080/'
files = {'ufile': ('audio.flac', open('audio.flac', 'rb'), 'audio/flac')}
r = requests.post(url, files=files)

我得到以下错误:

  File "C:\Users\Артём\AppData\Local\Programs\Python\Python37\lib\site-packages\speech_recognition\__init__.py", line 203, in __enter__
    self.audio_reader = wave.open(self.filename_or_fileobject, "rb")
  File "C:\Users\Артём\AppData\Local\Programs\Python\Python37\lib\wave.py", line 510, in open
    return Wave_read(f)
  File "C:\Users\Артём\AppData\Local\Programs\Python\Python37\lib\wave.py", line 164, in __init__
    self.initfp(f)
  File "C:\Users\Артём\AppData\Local\Programs\Python\Python37\lib\wave.py", line 131, in initfp
    raise Error('file does not start with RIFF id')
wave.Error: file does not start with RIFF id

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Артём\AppData\Local\Programs\Python\Python37\lib\site-packages\speech_recognition\__init__.py", line 208, in __enter__
    self.audio_reader = aifc.open(self.filename_or_fileobject, "rb")
  File "C:\Users\Артём\AppData\Local\Programs\Python\Python37\lib\aifc.py", line 917, in open
    return Aifc_read(f)
  File "C:\Users\Артём\AppData\Local\Programs\Python\Python37\lib\aifc.py", line 358, in __init__
    self.initfp(f)
  File "C:\Users\Артём\AppData\Local\Programs\Python\Python37\lib\aifc.py", line 316, in initfp
    raise Error('file does not start with FORM id')
aifc.Error: file does not start with FORM id

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Артём\AppData\Local\Programs\Python\Python37\lib\site-packages\speech_recognition\__init__.py", line 234, in __enter__
    self.audio_reader = aifc.open(aiff_file, "rb")
  File "C:\Users\Артём\AppData\Local\Programs\Python\Python37\lib\aifc.py", line 917, in open
    return Aifc_read(f)
  File "C:\Users\Артём\AppData\Local\Programs\Python\Python37\lib\aifc.py", line 358, in __init__
    self.initfp(f)
  File "C:\Users\Артём\AppData\Local\Programs\Python\Python37\lib\aifc.py", line 314, in initfp
    chunk = Chunk(file)
  File "C:\Users\Артём\AppData\Local\Programs\Python\Python37\lib\chunk.py", line 63, in __init__
    raise EOFError
EOFError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Артём\AppData\Local\Programs\Python\Python37\lib\site-packages\cherrypy\_cprequest.py", line 628, in respond
    self._do_respond(path_info)
  File "C:\Users\Артём\AppData\Local\Programs\Python\Python37\lib\site-packages\cherrypy\_cprequest.py", line 687, in _do_respond
    response.body = self.handler()
  File "C:\Users\Артём\AppData\Local\Programs\Python\Python37\lib\site-packages\cherrypy\lib\encoding.py", line 219, in __call__
    self.body = self.oldhandler(*args, **kwargs)
  File "C:\Users\Артём\AppData\Local\Programs\Python\Python37\lib\site-packages\cherrypy\_cpdispatch.py", line 54, in __call__
    return self.callable(*self.args, **self.kwargs)
  File "new1.py", line 19, in index
    with audio_file as source:
  File "C:\Users\Артём\AppData\Local\Programs\Python\Python37\lib\site-packages\speech_recognition\__init__.py", line 236, in __enter__
    raise ValueError("Audio file could not be read as PCM WAV, AIFF/AIFF-C, or Native FLAC; check if file is corrupted or in another format")
ValueError: Audio file could not be read as PCM WAV, AIFF/AIFF-C, or Native FLAC; check if file is corrupted or in another format

有人能解决这个问题吗?你知道吗


Tags: inpyselfliblocallinesiteaudio