我正在编写一个程序来识别来自麦克风的语音,并且代码将相应地处理。我为此目的编写的代码如下。
import speech_recognition as sr
import webbrowser
import pyttsx
from time import sleep
engine = pyttsx.init()
engine.setProperty('rate', 70)
r = sr.Recognizer()
def recognize(audio):
try:
return r.recognize(audio)
except LookupError, e:
print e
return ''
with sr.Microphone() as source:
while True:
engine.say("Hi How can i help you ?")
sleep(0.15)
print "Start Speaking"
audio = r.listen(source)
words = recognize(audio)
print("You said " + words)
if words == "Facebook":
engine.say("Shall i open the Facebook page for you ?")
engine.runAndWait()
audio = r.listen(source)
words = recognize(audio)
if words == "Yes":
webbrowser.open('https://www.facebook.com')
elif words == "stop":
break
在这里,我也尝试过睡眠,但在引擎说话之前,我可以看到文本开始说话被打印出来。除了睡眠之外,有没有什么好的方法可以捕捉麦克风中的语音并等到说些什么或长时间沉默?