speech recognition python code not working

Solution 1:

The possible reason could be that the recognizer_instance.energy_threshold property is probably set to a value that is too high to start off with. You should decrease this threshold, or call recognizer_instance.adjust_for_ambient_noise(source, duration = 1). You can learn more about it at Speech Recognition

Solution 2:

I have solved the same problem for me with the following (noise suppression), The "listen" function has problems with environment noise. So the running code is only blinking waiting.

Use this ambient noise suppression/adjustment; r.adjust_for_ambient_noise(source, duration=5)

Referance Tuesday, March 28, 2017 Easy Speech Recognition in Python with PyAudio and Pocketsphinx

Solution 3:

have you tried replacing

    print("You said " + r.recognize(audio))
    except LookupError:                          
    print("Could not understand audio")

with

    text = r.recognize_google(audio)
    print("You said : {}".format(text))
    text = r.recognize_google(audio)
    except:
        print("Sorry could not recognize your voice")

ensure pyaudio.h is installed by running the below command

    sudo apt-get install portaudio19-dev python-pyaudio python3-pyaudio

Solution 4:

try to add

r.adjust_for_ambient_noise(source,duration=1)

where r is recogniser instance, like this

import speech_recognition as sr

r=sr.Recognizer()
print(sr.Microphone.list_microphone_names())
with sr.Microphone() as source:
    r.adjust_for_ambient_noise(source,duration=1)
    # r.energy_threshold()
    print("say anything : ")
    audio= r.listen(source)
    try:
        text = r.recognize_google(audio)
        print(text)
    except:
        print("sorry, could not recognise")