我怎样才能让蟒蛇等到我说完? [英] How can i make the python to wait till i complete speaking?
问题描述
我正在编写一个程序来识别来自麦克风的语音,代码将进行相应的处理.我为此编写的代码如下.
I am writing a program to recognise the speech from a microphone and the code will process accordingly. The code I wrote for this purpose is below.
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
在这里,我也尝试过睡眠,但在引擎说话之前,我可以看到文本开始说话被打印出来.除了 Sleep 之外,有没有什么好的方法可以在麦克风中捕获语音并等到说些什么或长时间沉默?
Here I tried sleep also but before the engine speaks I can see the text Start Speaking getting printed. Instead of Sleep, is there any nice way to capture the speech in microphone and wait till say something or for a long silence?
推荐答案
此方法:
engine.runAndWait()
等待语音完成.您不仅需要在 engine.say("Shall i open the Facebook page for you ?")
之后使用它,还需要在 engine.say("Hi How can I help you ?")
而不是 sleep
waits for speech to complete. You need to use it not just after engine.say("Shall i open the Facebook page for you ?")
, but also after engine.say("Hi How can i help you ?")
instead of sleep
这篇关于我怎样才能让蟒蛇等到我说完?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!