0

I am trying to use the Google Web Speech API in Python. I just tried the following code:

import speech_recognition as sr import pyaudio   r = sr.Recognizer() with sr.Microphone() as source:
    # read the audio data from the default microphone
    print("Speak!")
    audio_text = r.listen(source)
    print("Recognizing...")
    # convert speech to text
    text = r.recognize_google(audio_text)
    print("You said: ", text)

But I get Bad Request 400 Error.

raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request
speech_recognition.RequestError: recognition request failed: Bad Request

All my Python packages are up to date, so what am I missing?

uk_butterfly
  • 93
  • 1
  • 2
  • 8

1 Answers1

0

The first step is to import Speech recognition because it is a dependency. Install by running pip and typing "pip install SpeechRecognition" Then import speech_recognition as sr

The next step is the actual important part which is where google sr obtains the speech as a query You do this by creating a function. I chose "takeCommand" as the function As you can see below, you assign the recognizer to a variable You open the microphone up with the statement below the variable line You can only use the microphone when Pyaudio is installed You do this by typing "pip install pyaudio" in a terminal or the ide terminal You do require an exception handler incase anything was missed by google.

Create a function def takeCommand(): and put the following in the function.

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-us')
    print("User said: {query}\n")

except Exception as e:
    print(e)
    print("Google didn't Understand.")
    return "None"

return query

This can be used in many different ways. If any questions come up, please tell me and I can even provide samples of programs featuring this.

Edit

This solution will not work unless you are sure of the following:

  1. Anti-virus is disabled (if applicable)
  2. Firewall or Network Security isn't interfering with things on your PC
  3. You confirm that every prerequisite package has been installed for this Speech Recognition API Documentation for Python
Redgar Tech
  • 347
  • 3
  • 13