Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

speech recognition python code not working

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.

like image 830
alloyeduniv Avatar asked Aug 14 '15 08:08

alloyeduniv


People also ask

How do I use speechrecognition in Python?

Speech recognition engine/API support: Quickstart: pip install SpeechRecognition. See the “Installing” section for more details. To quickly try it out, run python -m speech_recognition after installing. The library reference documents every publicly accessible object in the library.

What are some common speech recognition errors in Python?

0 Speech recognition using the speech_recognition module in python 0 Microphone is not Listening 0 speech_recognition not recognizing my voice or anything of that sort 1 IndexError: index 14708 is out of bounds for axis 0 with size 295

Is it possible to do speech recognition in Python with pyaudio?

Referance Tuesday, March 28, 2017 Easy Speech Recognition in Python with PyAudio and Pocketsphinx Share Improve this answer Follow answered Nov 27 '17 at 14:34 cinsdikicicinsdikici

Why can’t I take speech input from a microphone in Python?

Because you have to tell your Python program that you want to take speech input or voice input from which particular microphone. If you still don’t know how to find the device ID please read my previous tutorial,


5 Answers

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

like image 181
Tushar Gautam Avatar answered Oct 17 '22 22:10

Tushar Gautam


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

like image 31
cinsdikici Avatar answered Oct 17 '22 21:10

cinsdikici


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")
like image 20
MANISH RAWAT Avatar answered Oct 17 '22 22:10

MANISH RAWAT


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
like image 30
Michael Maigwa Avatar answered Oct 17 '22 21:10

Michael Maigwa


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.

like image 26
Deepak Chaudhary Avatar answered Oct 17 '22 21:10

Deepak Chaudhary