使用 Python 和 PyAudio 的语音转文本无法在操作系统上运行
Posted
技术标签:
【中文标题】使用 Python 和 PyAudio 的语音转文本无法在操作系统上运行【英文标题】:Speech to Text using Python & PyAudio not working on OS 【发布时间】:2020-02-10 10:51:56 【问题描述】:我正在尝试在 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.recognize_google(audio))
但是,当我运行程序时,没有输出,也没有错误消息。但是当我点击“停止”按钮时,会出现此错误消息
Traceback(最近一次调用最后一次): 文件“/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 行,听 缓冲区 = source.stream.read(source.CHUNK) 文件“/Users/diandraelmira/PycharmProjects/untitled/venv/lib/python3.7/site-packages/speech_recognition/init.py”,第 161 行,读取 return self.pyaudio_stream.read(size, exception_on_overflow=False) 文件“/Users/diandraelmira/PycharmProjects/untitled/venv/lib/python3.7/site-packages/pyaudio.py”,第 608 行,已读 返回 pa.read_stream(self._stream, num_frames, exception_on_overflow) 键盘中断
我该如何解决这个问题?
【问题讨论】:
【参考方案1】:您好,请您尝试一下,然后查找错误
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))
【讨论】:
【参考方案2】:我认为您的代码需要稍作改动,它是:
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 !")
试试这个!希望错误能得到解决。
【讨论】:
【参考方案3】:使用以下代码:
#import library
import speech_recognition as sr
# Initialize recognizer class (for recognizing the speech)
r = sr.Recognizer()
# Reading Microphone as source
# listening the speech and store in audio_text variable
with sr.Microphone() as source:
print("Talk")
audio_text = r.listen(source)
print("Time over, thanks")
# recoginize_() method will throw a request error if the API is unreachable, hence using exception handling
try:
# using google speech recognition
print("Text: "+r.recognize_google(audio_text))
except:
print("Sorry, I did not get that")
希望它有效。
【讨论】:
以上是关于使用 Python 和 PyAudio 的语音转文本无法在操作系统上运行的主要内容,如果未能解决你的问题,请参考以下文章
痞子衡嵌入式:语音处理工具pzh-speech诞生记- 音频录播实现(PyAudio)
AttributeError:找不到 PyAudio;检查安装...无法使用语音识别
Python 调用pyaudio库录制以及播放wav音频文件
[python] 2python使用pyaudio进行录音,及其在python虚拟环境virtualenv中安装遇到的问题