Is there any way to get Speechrecognition{python} to wait until the user speaks?

Asked

Viewed 68 times

0

I am making a program that will return me a response according to what I tell him, for that I used the Speechrecognition library to take what I say and pass as parameter, only that I realized that if I do not say anything is issued an error and the program ends, That’s why I didn’t end up saying anything, so I was wondering if there’s any way I could get him to wait for me to talk? The code:

def escutar_microfone():
    microfone = sr.Recognizer()
    with sr.Microphone() as source:
        microfone.adjust_for_ambient_noise(source)
        print("Fale, estou escutando!!")
        audio = microfone.listen(source)
        try:
            frase = microfone.recognize_google(audio, language='pt-BR')
            return frase.lower()
        except:
            print("Erro")

1 answer

0


import speech_recognition as sr

r = sr.Recognizer()
while True :
    with sr.Microphone() as source:
        print("Diga algo!")
        audio = r.listen(source,timeout = None)
    try:
        print("Você disse: " + r.recognize_google(audio, language = 'pt-BR'))
    except sr.UnknownValueError:
        pass
    except sr.RequestError as e:
        pass

I think looping solves your problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.