如何解决语音识别监听方法不听
我在 Raspberry Pi 4 (Debian Buster) 上的语音识别包出现了一些奇怪的行为。 listen() 方法不监听。它立即执行下一条指令。它不会等待麦克风的响应。
我使用的是 Adafruit USB PMP 迷你 USB 麦克风。我的耳机中也有一个麦克风,但我从来没有成功地将它与 Raspberry Pi 一起使用。
我使用 Alsa 命令来验证麦克风是否正常工作。麦克风的响应非常微弱。几乎听不到,但这是另一个 stackoverflow 问题。我最担心的是麦克风收不到音频。
没有任何改动的相同代码在 Windows 10 上运行良好。
代码如下:
import speech_recognition as sr
import pyttsx3
# 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:
engine = pyttsx3.init()
""" RATE"""
rate = engine.getProperty('rate') # getting details of current speaking rate
print(rate) # printing current voice rate
engine.setProperty('rate',120) # setting up new voice rate
"""VOLUME"""
volume = engine.getProperty('volume') # getting to know current volume level (min=0 and max=1)
print(volume) # printing current volume level
engine.setProperty('volume',1.0) # setting up volume level between 0 and 1
"""VOICE"""
voices = engine.getProperty('voices') # getting details of current voice
# engine.setProperty('voice',voices[0].id) #changing index,changes voices. o for male
engine.setProperty('voice',voices[1].id)
"""ASK THE QUESTION"""
engine.say("Hello,this is Leo. Ask me a question about New Mexico Volcanoes")
engine.runAndWait()
print("Ask A Question")
#r.adjust_for_ambient_noise(source) # adjust for ambient noise
r.adjust_for_ambient_noise(source,duration=1)
audio_text = r.listen(source)
print("ask question")
# recoginize_() method will throw a request error if the API is unreachable,h
try:
# using google speech recognition
question = r.recognize_google(audio_text)
print("Text: "+question)
"""ANSWER"""
engine.say('You asked:'+question)
engine.runAndWait()
except:
print("Sorry,I did not get that")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。