3

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

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()
4

1 回答 1

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()
于 2018-11-11T13:44:37.030 回答