How to make chatbot starts listening on calling its name?

200 Views Asked by At

I want my python chatbot to start listening when I say "Echo". How do I do that? A snippet of chatbot is below.

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

There are 1 best solutions below

0
On

After hit and trials, I got it right. But I find it slow. If you have better strategy, please comment.

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