语音识别模块提供意外的关键字参数'溢出时异常'错误

时间:2017-07-19 12:59:25

标签: python python-3.x speech-recognition speech-to-text

我正在尝试使用语音识别模块将语音转换为文本。

我的代码以:

开头
import speech_recognition as sr
r = sr.Recognizer()
m = sr.Microphone()
with m as source:
    print("Say Something...")
    audio = r.listen(source)

然而,这会产生以下错误:

Traceback (most recent call last):
  File "<pyshell#8>", line 3, in <module>
    data = r.listen(source)
  File "/usr/local/lib/python3.4/dist-
packages/speech_recognition/__init__.py", line 553, in listen
    buffer = source.stream.read(source.CHUNK)
  File "/usr/local/lib/python3.4/dist-
packages/speech_recognition/__init__.py", line 161, in read
return self.pyaudio_stream.read(size, exception_on_overflow = False)
TypeError: read() got an unexpected keyword argument 'exception_on_overflow'

请有人解释一下发生了什么以及如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

试试这个:

import speech_recognition as sr
# obtain audio from the microphone
r = sr.Recognizer()
with sr.Microphone() as source:
    print ('Attention! Say something')
    audio = r.listen(source)

# recognize speech using Google Speech Recognition
try:
# for testing purposes, we're just using the default API key
# to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
# instead of `r.recognize_google(audio)`
    data='something'
    data = r.recognize_google(audio,language='en-US')

except sr.UnknownValueError:
    print ('Attention ! Google could not understand audio')
    data='Could not understand anything'
except sr.RequestError as e:

   print ('Attention ! Could not request results from Google service.')

print data

希望这有帮助。