speech_recognition worthless

Asked

Viewed 20 times

0

I made a program to open apps from a folder, and in this case it works perfectly, the problem is that it gives error if I say nothing, and my goal was to let the program running in the background

import speech_recognition as sr
import os

while True:
    recognizer = sr.Recognizer()
    mic = sr.Microphone()
    with mic as source:
        audio = recognizer.listen(source)
    output = recognizer.recognize_google(audio)
    print(output)
    if 'break' in output:
        break
    elif 'open' in output:
        app = output.replace('open ', '')
        print(app)
        os.startfile(f"C:\AtalhosFaim\{app}")

        print('Nada')
    print(output)
print('Fim')

1 answer

0


You have to treat Unknownvaluror Requesterror Timeout error:

import speech_recognition as sr
import os

while True:
    recognizer = sr.Recognizer()
    mic = sr.Microphone()
    with mic as source:
        print('# ouvindo.')
        audio = recognizer.listen(source, timeout = None)

    try:
        output = recognizer.recognize_google(audio)
        print(output)
        
        if 'break' in output:
            break
        elif 'open' in output:
            app = output.replace('open ', '')
            print(app)
            os.startfile(f"C:\AtalhosFaim\{app}")
            
    except sr.UnknownValueError:
        pass
    except sr.RequestError as e:
        pass

Using this Try block you can keep your code running.

Browser other questions tagged

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