我怎样才能让python等我说完呢?

我怎样才能让python等我说完呢?,python,python-2.7,speech-recognition,pyttsx,Python,Python 2.7,Speech Recognition,Pyttsx,我正在编写一个程序来识别来自麦克风的语音,代码将相应地进行处理。下面是我为此编写的代码 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: retu

我正在编写一个程序来识别来自麦克风的语音,代码将相应地进行处理。下面是我为此编写的代码

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
在这里,我也尝试了睡眠,但在引擎说话之前,我可以看到文本开始说话正在打印。有没有什么好方法可以代替睡眠,用麦克风捕捉讲话,然后等着说点什么或长时间沉默?

这种方法:

        engine.runAndWait()
等待演讲完成。你不仅需要在
引擎之后使用它。比如说(“我可以为你打开Facebook页面吗?”)
,还需要在
引擎之后使用它。比如说(“嗨,我能帮你什么吗?”)

你为什么用睡眠来代替睡眠?这没有帮助,您必须为
r.listen(source)
返回的
audio
设置一个阈值。如果
audio
大于阈值,则您正在讲话,否则将是长时间的沉默。Cory。。您能告诉我如何设置阈值吗?没有直接的假设,因为它高度依赖于
r返回的值。listen(source)
,您需要进行一些实验,分析不同高度的语音样本的输出,并进行适当的选择。但这里我需要等到引擎完成“嗨,我能帮你什么忙吗?”