我的目标是从客户端接收音频文件并将其转录为文本,而不是永久保存到磁盘。我使用的是SpeechRecognition库,如果保存了上载的文件,它可以完美运行,但是当我尝试使用临时文件时,会导致错误。
从CherryPy文档:
当客户端将文件上传到CherryPy应用程序时,该文件会立即放在磁盘上。 CherryPy会将其作为参数传递给您的公开方法;该arg将具有“文件”属性,该属性是临时上传文件的句柄
由于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
有人对这个问题有什么解决办法吗?