使用Python&;PyAudio在操作系统上不工作
我正在尝试使用Python 3.7(OS X/内部麦克风)上的语音识别库 以下是我目前使用的代码:使用Python&;PyAudio在操作系统上不工作,python,python-3.x,speech-recognition,speech-to-text,pyaudio,Python,Python 3.x,Speech Recognition,Speech To Text,Pyaudio,我正在尝试使用Python 3.7(OS X/内部麦克风)上的语音识别库 以下是我目前使用的代码: 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.re
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))
但是,运行程序时没有输出,也没有错误消息。但是当我按下“停止”按钮时,出现了这个错误消息
回溯(最近一次呼叫最后一次):
文件“/Users/diandraelmira/PycharmProjects/untitled/venv/APP.py”,第6行,在
音频=r.listen(源)
文件“/Users/diandraelmira/PycharmProjects/untitled/venv/lib/python3.7/site packages/speech_recognition/init.py”,第620行,在listen中
buffer=source.stream.read(source.CHUNK)
文件“/Users/diandraelmira/PycharmProjects/untitled/venv/lib/python3.7/site packages/speech_recognition/init.py”,第161行,已读
返回self.pyaudio\u stream.read(大小、溢出异常\u=False)
文件“/Users/diandraelmira/PycharmProjects/untitled/venv/lib/python3.7/site packages/pyaudio.py”,第608行,已读
返回pa.read\u流(self.\u流、num\u帧、异常\u on\u溢出)
键盘中断
如何修复此问题?您好,请尝试此方法并查找错误
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))
您好,请您尝试一下,并查找错误
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))
我认为您的代码需要稍作修改,具体如下:
import speech_recognition as sr
rObject = sr.Recognizer()
audio = ''
with sr.Microphone() as source:
print("Speak...")
audio = rObject.listen(source, phrase_time_limit = 0)
print("Stop.")
try:
text = rObject.recognize_google(audio, language ='en-US')
print("You : "+ text)
except:
speak("Could not understand your audio...PLease try again !")
试试这个!希望错误会得到解决。我认为需要对代码进行一些小的更改,它是:
import speech_recognition as sr
rObject = sr.Recognizer()
audio = ''
with sr.Microphone() as source:
print("Speak...")
audio = rObject.listen(source, phrase_time_limit = 0)
print("Stop.")
try:
text = rObject.recognize_google(audio, language ='en-US')
print("You : "+ text)
except:
speak("Could not understand your audio...PLease try again !")
试试这个!希望错误会被解决