Speech Recognition using Google Speech API


Google has a great Speech Recognition API. This API converts spoken text (microphone) into written text (Python strings), briefly Speech to Text. You can simply speak in a microphone and Google API will translate this into written text. The API has excellent results for English language.

Google has also created the JavaScript Web Speech API, so you can recognize speech also in JavaScript if you want, here’s the link: https://www.google.com/intl/en/chrome/demos/speech.html. To use it on the web you will need Google Chrome version 25 or later.

Related courses:

Installation

Google Speech API v2 is limited to 50 queries per day. Make sure you have a good microphone.
Are you are looking for text to speech instead?

This is the installation guide for Ubuntu Linux. But this will probably work on other platforms is well. You will need to install a few packages: PyAudio, PortAudio and SpeechRecognition. PyAudio 0.2.9 is required and you may need to compile that manually.

git clone http://people.csail.mit.edu/hubert/git/pyaudio.git
cd pyaudio
sudo python setup.py install
sudo apt-get installl libportaudio-dev
sudo apt-get install python-dev
sudo apt-get install libportaudio0 libportaudio2 libportaudiocpp0 portaudio19-dev
sudo pip3 install SpeechRecognition

Program

This program will record audio from your microphone, send it to the speech API and return a Python string.

The audio is recorded using the speech recognition module, the module will include on top of the program. Secondly we send the record speech to the Google speech recognition API which will then return the output.
r.recognize_google(audio) returns a string.

#!/usr/bin/env python3
# Requires PyAudio and PySpeech.
 
import speech_recognition as sr
 
# Record Audio
r = sr.Recognizer()
with sr.Microphone() as source:
    print("Say something!")
    audio = r.listen(source)
 
# Speech recognition 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("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))

You may like: Personal Assistant Jarvis (Speech Recognition and Text to Speech) or Speech Engines

44 thoughts on “Speech Recognition using Google Speech API

  1. Tama rindo - January 19, 2018

    Let’s say I want to use audio fingerprinting to auto mute TV commercials using the infrared on my cell phone. Can I record the undesired commercials to be recognized automatically in order to have the phone mute them when they reappear?

    1. Frank - January 20, 2018

      Yes technically that’s possible, but you’d want to use plain audio fingerprinting instead of speech recognition.

  2. ILYES Zine - January 16, 2018

    Hi
    i have some wave audio files. and i would like to use this API in order to do “speech ro text”. without using the microphone.
    audio files as input and the text string for output.
    is that possible?
    Thanks a lot !

    1. Frank - January 20, 2018

      try loading the wave file as the variable audio.

  3. Venkat Narendra - January 15, 2018

    is it possible to limit the listening time of google speech api ..if it is possible can any one please help me how to do that

  4. Akmal Hakimi - January 12, 2018

    Hi Frank. Is there a way to use arecord instead of pyaudio? Also, where do I change the recognition language? Thanks!

    1. Frank - January 20, 2018

      This module requires the pyaudio module, but technically it should be possible

  5. pkmn SM - January 9, 2018

    Frank i done all thing in my windows and it is not showing any error but it stuck on ‘say something’
    Not showing any output or error . please help me

    1. Frank - January 20, 2018

      Is the request done? It may be stuck trying to fetch audio data from your microphone source. Do you have an usb microphone or standard?