7

Speech Recognition with the following code just not working at all

with sr.Microphone() as source:
# read the audio data from the default microphone
audio = r.record(source, duration=4)
print("Recognizing...")
# convert speech to text
# recognize speech using Google Speech Recognition
try:
    # for testing purposes, we're just using the default API key
    # to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
    # instead of `r.recognize_google(audio)`
    print("Google Speech Recognition thinks you said in English: -  " + r.recognize_google(audio, language = "en-US"))
except sr.UnknownValueError:
    print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
    print("Could not request results from Google Speech Recognition service; {0}".format(e))

Here is the full error, it seems like the request is just failing, however this same code seems to work fine if I upload an audio file as the source. I have already checked by sr.Microphone and the default option is also linked correctly to my actual microphone...

    ---------------------------------------------------------------------------
HTTPError                                 Traceback (most recent call last)
~/anaconda3/lib/python3.7/site-packages/speech_recognition/__init__.py in recognize_google(self, audio_data, key, language, show_all)
    839         try:
--> 840             response = urlopen(request, timeout=self.operation_timeout)
    841         except HTTPError as e:

~/anaconda3/lib/python3.7/urllib/request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
    221         opener = _opener
--> 222     return opener.open(url, data, timeout)
    223 

~/anaconda3/lib/python3.7/urllib/request.py in open(self, fullurl, data, timeout)
    530             meth = getattr(processor, meth_name)
--> 531             response = meth(req, response)
    532 

~/anaconda3/lib/python3.7/urllib/request.py in http_response(self, request, response)
    640             response = self.parent.error(
--> 641                 'http', request, response, code, msg, hdrs)
    642 

~/anaconda3/lib/python3.7/urllib/request.py in error(self, proto, *args)
    568             args = (dict, 'default', 'http_error_default') + orig_args
--> 569             return self._call_chain(*args)
    570 

~/anaconda3/lib/python3.7/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args)
    502             func = getattr(handler, meth_name)
--> 503             result = func(*args)
    504             if result is not None:

~/anaconda3/lib/python3.7/urllib/request.py in http_error_default(self, req, fp, code, msg, hdrs)
    648     def http_error_default(self, req, fp, code, msg, hdrs):
--> 649         raise HTTPError(req.full_url, code, msg, hdrs, fp)
    650 

HTTPError: HTTP Error 400: Bad Request

During handling of the above exception, another exception occurred:

RequestError                              Traceback (most recent call last)
<ipython-input-109-50b94b08a896> in <module>
      3     audio = r.record(source, duration=4)
      4     print("Recognizing...")
----> 5     r.recognize_google(audio, language = "en-US")

~/anaconda3/lib/python3.7/site-packages/speech_recognition/__init__.py in recognize_google(self, audio_data, key, language, show_all)
    840             response = urlopen(request, timeout=self.operation_timeout)
    841         except HTTPError as e:
--> 842             raise RequestError("recognition request failed: {}".format(e.reason))
    843         except URLError as e:
    844             raise RequestError("recognition connection failed: {}".format(e.reason))

RequestError: recognition request failed: Bad Request

enter image description here

Dan Bonachea
  • 2,408
  • 5
  • 16
  • 31
Zhangyi Fan
  • 71
  • 1
  • 2
  • Add a new error exception: `except HTTPError as e`. Somehow the server is not accepting your request. May be `audio` has some wrong data format. – Lutfar Rahman Milu Jul 03 '20 at 17:01

1 Answers1

7

This may not be the solution for the code you've written. But will help many others trying to use Google SpeechRecognition and getting the same vague error message.

I resolved the same issue by cutting short my audio input file to less than 10 MB. Currently, the quota for synchronous requests is ~1 minute(10 MB).

Quoting documentation:

There is a limit of 10 MB on all single requests sent to the API using local files. In the case of the Recognize and LongRunningRecognize methods, this limit applies to the size of the request sent. In the case of the StreamingRecognize method, the 10 MB limit applies to both the initial StreamingRecognize request and the size of each individual message in the stream. Exceeding this limit will throw an error.

YetAnotherBot
  • 1,937
  • 2
  • 25
  • 32