-1

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()
Vinay Kumar
  • 170
  • 1
  • 11
  • 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 Jun 16 '20 at 06:09
  • Any code below this line isn't executing. "audio=r.listen(source)". – Vinay Kumar Jun 16 '20 at 13:28
  • Your question isn't a good question (bad title, bad text, ...). Please have a look at [help](https://stackoverflow.com/help) to learn how to ask good questions. – AmirSina Mashayekh Jun 16 '20 at 14:29
  • 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 Jun 18 '20 at 12:36

3 Answers3

8

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)
yabberth
  • 507
  • 4
  • 9
0

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.

AmirSina Mashayekh
  • 498
  • 1
  • 5
  • 21
0

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)
Pawara Siriwardhane
  • 1,873
  • 10
  • 26
  • 38