pythonpython-3.xspeech-recognitionspeech

No output in speech recognition


I've seen over 5 yt tutorials on how to work with speech recognition modules but when i try them,I just get no output!

import speech_recognition as sr

root = sr.Recognizer()

with sr.Microphone() as source :
    print('Listening:')
    audio = root.listen(source)
text = root.recognize_google(audio)
print(text)

this is my code. it prints Listening: but If I talk to it nothing more gets printed. Im sure that my microphone is okay because I tested windows default speek recognition and it worked with no errors. also I installed all the libraies required so I don't think that would be much of a problem too.


Solution

  • I think you should give the listen() function a time duration so that the Recognizer can parse your saying.

    The phrase_time_limit parameter is the maximum number of seconds that this will allow a phrase to continue before stopping and returning the part of the phrase processed before the time limit was reached. The resulting audio will be the phrase cut off at the time limit. - based on the official docs

    import speech_recognition as sr  
                                                                    
    root = sr.Recognizer()                                                                                   
    with sr.Microphone(device_index=0) as source:                                                                       
        print("Speak:")                                                                                   
        audio = root.listen(source, phrase_time_limit=5.0) # in seconds  
        # or you can try this ...
        # audio = root.record(source, duration = 5.0)
    
    print("You said " + root.recognize_google(audio))