sr.Recogniser.listen() 不能正常工作,导致程序卡死
重现步骤
以下代码无法通过从麦克风获取音频: `import speech_recognition as sr r = sr.Recognizer() m = sr.Microphone()
try: print("请保持一段时间的沉默...") with m as source: r.adjust_for_ambient_noise(source) print("将最小能量阈值设置为 {}".format(r.energy_threshold)) while True: print("说点什么!") with m as source: audio = r.listen(source) print("已经获取!接下来是识别...") try: # 使用 Google 语音识别进行语音识别 value = r.recognize_google(audio) # 需要对以下代码进行特殊处理,以便将 Unicode 字符正确输出到标准输出 if str is bytes: # Python 版本使用字符串为字节 (Python 2) print(u"You said {}".format(value).encode("utf-8")) else: # Python 版本使用字符串为 Unicode (Python 3+) print("You said {}".format(value)) except sr.UnknownValueError: print("糟糕! 没有捕获到") except sr.RequestError as e: print("糟糕! 无法从 Google 语音识别服务请求结果; {0}".format(e)) except KeyboardInterrupt: pass`
预期行为
应该进行识别: value = r.recognize_google(audio)
实际行为
在说话后,with m as source: audio = r.listen(source) 无法进入下一行。
内容来源: Uberi/speech_recognition