when following the CS50 course of Python, I tried to do the Speech recognition as David did, but the program fails. I have installed SpeechRecognition and Pyaudio as well througn pip install. Here is my code:

import speech_recognition as sr

recognizer = sr.Recognizer()
with sr.Microphone() as source:
    print("say sth:")
    audio = recognizer.listen(source)
print("I heard: ")

here is the traceback:

Traceback (most recent call last): File "E:/python/p4eb/ai.py", line 14, in print("You said: " + r.recognize_google(audio)) File "C:\Users\Minyan\Anaconda3\lib\site-packages\speech_recognition__init__.py", line 840, in recognize_google response = urlopen(request, timeout=self.operation_timeout) File "C:\Users\Minyan\Anaconda3\lib\urllib\request.py", line 222, in urlopen return opener.open(url, data, timeout) File "C:\Users\Minyan\Anaconda3\lib\urllib\request.py", line 525, in open response = self._open(req, data) File "C:\Users\Minyan\Anaconda3\lib\urllib\request.py", line 543, in _open '_open', req) File "C:\Users\Minyan\Anaconda3\lib\urllib\request.py", line 503, in _call_chain result = func(*args) File "C:\Users\Minyan\Anaconda3\lib\urllib\request.py", line 1345, in http_open return self.do_open(http.client.HTTPConnection, req) File "C:\Users\Minyan\Anaconda3\lib\urllib\request.py", line 1320, in do_open r = h.getresponse() File "C:\Users\Minyan\Anaconda3\lib\http\client.py", line 1321, in getresponse response.begin() File "C:\Users\Minyan\Anaconda3\lib\http\client.py", line 296, in begin version, status, reason = self._read_status() File "C:\Users\Minyan\Anaconda3\lib\http\client.py", line 278, in _read_status raise BadStatusLine(line) http.client.BadStatusLine: UNKNOWN 400 Bad Request

Can somebody help me?


When you have to use an external library, reading the documentation is very useful... 👌🏻

I saw this section of the code in the Github documentation:

    # 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 " + r.recognize_google(audio))
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))

Maybe the audio was not good enough to be understood... Try using this try catch code and post the result please.

Kind regards

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .