如何解决Raspberry Pi Zero W Python SpeechRecognition 在不更改任何代码的情况下停止工作
我正在从事一个涉及 Raspberry Pi Zero W 和语音识别的项目。它第一次工作,然后在我再次运行后它只是卡住了录音。运行 python -m speech_recognition
一开始不起作用,但现在我修复了它,但我的程序仍然无法运行。代码如下:
print("INFO: Importing libraries...")
import speech_recognition as sr
import i2c
print("INFO: Done.")
r = sr.Recognizer()
m = sr.Microphone(1)
lcd = i2c.lcd()
"""Sets parameters to stop recognizer.listening faster.
Parameters:
"r.pause_threshold": an integer in seconds for how long the recognizer needs of silence to stop recording
"r.non_speaking_duration: seconds of non-speaking audio to keep on both sides of the recording,changed because it cannot be higher than `r.pause_threshold`
"""
r.pause_threshold = 0.3
r.non_speaking_duration = 0.3
def speech(recognizer,microphone):
"""Transcribe speech from recorded from `microphone`.
Returns a dictionary with three keys:
"success": a boolean indicating whether or not the API request was
successful
"error": `None` if no error occured,otherwise a string containing
an error message if the API could not be reached or
speech was unrecognizable
"transcription": `None` if speech could not be transcribed,otherwise a string containing the transcribed text
"""
# check that recognizer and microphone arguments are appropriate type
if not isinstance(recognizer,sr.Recognizer):
raise TypeError("`recognizer` must be `Recognizer` instance")
if not isinstance(microphone,sr.Microphone):
raise TypeError("`microphone` must be `Microphone` instance")
# adjust the recognizer sensitivity to ambient noise and record audio
# from the microphone
with microphone as source:
#recognizer.adjust_for_ambient_noise(source)
print("INFO: Recording...")
audio = recognizer.listen(source)
print("INFO: Recognizing...")
# set up the response object
response = {
"success": True,"error": None,"transcription": None
}
# try recognizing the speech in the recording
# if a RequestError or UnknownValueError exception is caught,# update the response object accordingly
try:
response["transcription"] = recognizer.recognize_google(audio)
except sr.RequestError:
# API was unreachable or unresponsive
response["success"] = False
response["error"] = "API unavailable"
except sr.UnknownValueError:
# speech was unintelligible
response["error"] = "Unable to recognize speech"
return response
while True:
print("INFO: Ready for speech.")
trans = speech(r,m)["transcription"]
if trans == "exit":
exit()
if trans == None:
trans = "*unaudable*"
print(trans);
lcd.lcd_clear()
lcd.lcd_display_string(trans)
感谢您的时间!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。