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: # use the default microphone as the audio source
audio = r.listen(source) # listen for the first phrase and extract it into audio data
try:
print("You said " + r.recognize(audio)) # recognize speech using Google Speech Recognition
except LookupError: # speech is unintelligible
print("Could not understand audio")
The output gives a blinking pointer. That's it. Please help, as I am new to this.
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
You may have to install these things first:
pip install pyaudio
pip install --upgrade pyaudio
pip install wheel
pip install google-api-python-client
sudo apt-get install flac
pip install monotonic
pip install SpeechRecognition
After that, refer the site (https://realpython.com/python-speech-recognition/) it will clearly explain what you wanted.
Here I am attaching the code I've edited from that site. Since I am new it will not be perfect, but I've tried. This is to check weather the voice input is similar to the text I've given and also it will print what you said.
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.
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 callrecognizer_instance.adjust_for_ambient_noise(source, duration = 1)
. You can learn more about it at Speech RecognitionPut the Try and Except inside the indentation.
Here is my Working code:-
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.