1
votes

The code is stuck at listening ( audio=r.listen(source) line) and doesn't go beyond it. No error messages or anything else.

My code:

import speech_recognition as sr

def takeCommand():
    '''
    It takes user's voice as input
    '''
    r=sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        r.pause_threshold = 1
        audio=r.listen(source)

try:
    print("Recognizing...")
    query = r.recognize_google(audio, language="en-in")
    print(f"Recognized Command: {query}")

except Exception as e:
    print(e)
    print("I didn't recognize what you said please repeat")
    return "None"

return query


takeCommand()
3
Please debug your code and find which line is the problem. If you don't have a debugger, simply insert a print("XYZ") after lines r.pause_threshold = 1 and audio=r.listen(source) to detect the line. - AmirSina Mashayekh
Any code below this line isn't executing. "audio=r.listen(source)". - Vinay Kumar
Your question isn't a good question (bad title, bad text, ...). Please have a look at help to learn how to ask good questions. - AmirSina Mashayekh
Thanks a lot for your suggestion, I'll surely work on it and try to improve. Being new to this platform my question may sound bad, but my problem is real. - Vinay Kumar

3 Answers

5
votes

I just checked the link you posted in the comment.

There is this code:

with mic as source:
    audio = r.listen(source)

If this code does not work, one reason could be that the microphone picks up too much ambient noise.

The solution may be

with mic as source:
    r.adjust_for_ambient_noise(source)
    audio = r.listen(source)
0
votes

I didn't work with this library; but I think r.listen(source) is a blocking function (method). A blocking method is a method that blocks code from continuing execution until it is done (returned). Your app is waiting for r.listen(source) to finish.

You should put print("Recognizing...") before it. Also as I read here, r.listen(source) will return result (finish) when it detects silence (probably after a voice). So your code should continue execution after it detects silence.

-1
votes

The solution is you have to set some arguments in the listen() method.

For example:

audio=r.listen(source,timeout=8,phrase_time_limit=8)