如何使聊天机器人开始监听其名字?

时间:2018-11-11 13:16:33

标签: python speech-recognition chatbot assistant

当我说“ Echo”时,我希望我的python chatbot开始监听。我怎么做?下面是一个聊天机器人的片段。

import speech_recognition as sr
running=True
r = sr.Recognizer()
def Speech():
    with sr.Microphone() as source:
        r.adjust_for_ambient_noise(source, duration=1)
        print("Say something!")
        audio = r.listen(source)    
    try:
        x=r.recognize_google(audio)
        print(x)
    except sr.UnknownValueError:
        pass
    except sr.RequestError as e:
        pass

while running==True:
    r = sr.Recognizer()
    with sr.Microphone() as source:
        while 1:
            Speech()

1 个答案:

答案 0 :(得分:0)

经过反复试验后,我说对了。但是我发现它很慢。如果您有更好的策略,请发表评论。

import speech_recognition as sr
running=True
r = sr.Recognizer()
def Speech():
    with sr.Microphone() as source:
        r.adjust_for_ambient_noise(source, duration=1)
        print("Say something!")
        audio = r.listen(source)    
        try:
            x=r.recognize_google(audio)
            if x=="hello":
                print("Speak up")
                audio = r.listen(source)
                print(r.recognize_google(audio))
        except sr.UnknownValueError:
            pass
        except sr.RequestError as e:
            pass

while running==True:
    Speech()