我正试图增加对
Rev.ai service
进入
Python SpeechRecognition library
使用
Rev.ai Python SDK
.我已经编写了一个集成函数,但当我试图在一个简单的应用程序中使用它时,我收到了一个ValueError。我怀疑这可能是由于输入音频配置中的错误,希望能帮助理解和解决它。
我的语音识别功能如下:
def recognize_rev_ai(self, audio_data, key=None, language="en"):
"""
This function requires a Rev.ai account.
"""
assert isinstance(audio_data, AudioData), "``audio_data`` must be audio data"
assert key is None or isinstance(key, str), "``key`` must be ``None`` or a string"
assert isinstance(language, str), "``language`` must be a string"
try:
from rev_ai.models import MediaConfig
from rev_ai.streamingclient import RevAiStreamingClient
except ImportError:
raise RequestError('missing rev_ai module')
config = MediaConfig('audio/x-raw', 'interleaved', 16000, 'S16LE', 1)
raw_data = audio_data.get_raw_data(
convert_rate=16000
)
if key is not None:
streamclient = RevAiStreamingClient(key, config)
try:
with io.open(raw_data, 'rb') as stream:
media_generator = [stream.read()]
response_generator = streamclient.start(media_generator)
for response in response_generator:
print(response)
except URLError as e:
raise RequestError("recognition connection failed: {0}".format(e.reason))
下面是一个示例脚本,演示如何使用。我根据SpeechRecognition代码样本中的一个示例对其进行了建模。这个脚本失败了
ValueError: embedded null byte
.最后包含完整的错误回溯。
import speech_recognition as sr
# obtain audio from the microphone
r = sr.Recognizer()
with sr.Microphone(sample_rate = 16000) as source:
print("Say something!")
audio = r.listen(source)
try:
print ('Rev thinks you said: ' + r.recognize_rev_ai(audio, 'KEY'))
except sr.UnknownValueError:
print("Rev could not understand audio")
except sr.RequestError as e:
print("Rev error; {0}".format(e))
完整错误回溯:
Say something!
Traceback (most recent call last):
File "b.py", line 10, in <module>
print ('Rev thinks you said: ' + r.recognize_rev_ai(audio, 'KEY'))
File "/home/vikram/.local/lib/python3.8/site-packages/speech_recognition/__init__.py", line 1431, in recognize_rev_ai
with io.open(raw_data, 'rb') as stream:
ValueError: embedded null byte
如果您能帮我调试以上内容,我将不胜感激。