I am running the following code in Python 2.7 with pyAudio installed.
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:             
        try this code:
r = sr.Recognizer()
with sr.Microphone() as source:
        r.adjust_for_ambient_noise(source=source)
        audio = r.listen(source,timeout=3)
        
    
    data = ''
    try :
        data = r.recognize_google(audio)
        print(data)
    except sr.UnknownValueError:
        print(" Error")
        
    except sr.RequestError as e:
        print("Request Error")
or add timeout and r.adjust_for_ambient_noise(source=source) to your code as above.please can anyone help me with this
check the input volume of your microphone. It is by default set to 0 in ubuntu (in my case). Since your program got stuck on the line audio = r.listen(source), which simply means that the microphone is not able to listen to any voice input. Hope this helps.
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
just try;
pip install sounddevice
it works.
In addition to Tushar's answer, I suggest trying a nicer external USB microphone. PyAudio can have issues with a simple built-in laptop microphone.
Please set minimum threshold.
After running commend python -m speech_recognition. Set minimum energy threshold which it display.
Setting procedure: press Ctrl then click mouse Recognizer(). Now set energy threshold.