使用Python的语音到文本PyAudio不工作

2024-04-29 06:37:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Python3.7(OSX/内部麦克风)上的语音识别库

以下是我目前使用的代码:

import speech_recognition as sr

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

print("Google Speech Recognition thinks you said " + r.recognize_google(audio))

但是,在运行程序时没有输出,也没有错误消息。但是当我按下“停止”按钮时,这个错误信息出现了

Traceback (most recent call last): File "/Users/diandraelmira/PycharmProjects/untitled/venv/APP.py", line 6, in audio = r.listen(source) File "/Users/diandraelmira/PycharmProjects/untitled/venv/lib/python3.7/site-packages/speech_recognition/init.py", line 620, in listen buffer = source.stream.read(source.CHUNK) File "/Users/diandraelmira/PycharmProjects/untitled/venv/lib/python3.7/site-packages/speech_recognition/init.py", line 161, in read return self.pyaudio_stream.read(size, exception_on_overflow=False) File "/Users/diandraelmira/PycharmProjects/untitled/venv/lib/python3.7/site-packages/pyaudio.py", line 608, in read return pa.read_stream(self._stream, num_frames, exception_on_overflow) KeyboardInterrupt

我怎样才能解决这个问题?你知道吗


Tags: inpysourcereadstreamvenvlinespeech
1条回答
网友
1楼 · 发布于 2024-04-29 06:37:47

嗨,你能试试这个吗?你会找出错误的

try:
    print("Sphinx thinks you said " + r.recognize_sphinx(audio))
except sr.UnknownValueError:
    print("Sphinx could not understand audio")
except sr.RequestError as e:
    print("Sphinx error; {0}".format(e))

相关问题 更多 >