Python 3.10.4语音识别3.9.0 AudioSource和AttributeError使用麦克风时



我试着玩周围的语音识别,这是我的代码:

import speech_recognition as sr
r = sr.Recognizer()
# device_index=1
with sr.Microphone() as source:
print("Say something")
audio = r.listen(source) # <- Error

try:
voice_data = r.recognize_google(audio)
print(voice_data)
except Exception as e:
print(e)

当我运行的结果是:

Say something
AssertionError: Audio source must be entered before listening, see documentation for ``AudioSource``; are you using ``source`` outside of a ``with`` statement?
During handling of the above exception, another exception occurred:
AttributeError: 'NoneType' object has no attribute 'close'

我安装PyAudio 0.2.11从https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyaudio因为问题(但也尝试与pip安装)。我也试着改变

with sr.Microphone(device_index=1) as source:

同样的错误…

我注意到你的代码和我的工作代码有两个不同。

  1. 尝试在使用r.listen(源)抓取音频之前设置暂停阈值:r.pause_threshold = 1.0.

  2. 第二个是在你的尝试块,当你设置voice_data变量和使用r.r ackize_google(音频)。在我的工作代码中,我将language='EN-US'指定为第二个参数,如下所示:r.r ackize_google (audio, language='EN-US')。

最新更新