我说语音识别时没有反应

时间:2018-10-15 16:18:29

标签: python-2.7 speech-recognition pyaudio portaudio

import speech_recognition as sr
r = sr.Recognizer()
#
with sr.Microphone() as source:
    print("Say something!")
    audio = r.listen(source)

try:
    print("google speech recognition thinks you said " + r.recognize_google(audio))
except sr.UnknowValueError:
    print("google speech recognition could not understand audio")
except sr.RequestError as e:
    print("Could not request results from google speech recognition service;{0}".format(e))

macOS Mojave。

portaudio-19.6.0

pyaudio-0.2.11

运行python main.py之后,我对Mac说了些什么,然后等待了几分钟。完全没有回应! Ctrl + C停止该过程,并在下面显示错误消息。

Say something!
^CTraceback (most recent call last):
  File "main.py", line 6, in <module>
    audio = r.listen(source)
  File "/Library/Python/2.7/site-packages/speech_recognition/__init__.py", line 620, in listen
    buffer = source.stream.read(source.CHUNK)
  File "/Library/Python/2.7/site-packages/speech_recognition/__init__.py", line 161, in read
    return self.pyaudio_stream.read(size, exception_on_overflow=False)
  File "/Library/Python/2.7/site-packages/pyaudio.py", line 608, in read
    return pa.read_stream(self._stream, num_frames, exception_on_overflow)
KeyboardInterrupt

1 个答案:

答案 0 :(得分:0)

我在 macOS Mojave 上通过 PyCharm 使用 python 3.9 时遇到了同样的问题。

为了让应用程序和程序使用麦克风,macOS 必须授予它这样做的权限,因此在运行脚本时没有音频输入和响应。

话虽如此,为了解决这个问题,您必须打开系统偏好设置>安全与隐私>麦克风并检查相应的程序以允许使用麦克风。 >

请记住,我的案例涉及 PyCharm 作为运行脚本的应用程序,因此在麦克风权限下,我授予 PyCharm 访问我的麦克风的权限,并提示重新启动 IDE。

之后问题就解决了。