Python Speech Recognition Using Google Speech API

In this tutorial you will learn about python speech recognition. There are plenty of options available for this. But Google Speech API is best among all of them.

So in this article we are going to see how we can implement Google Speech API in Python.

Required Installation Files

1. Install  PyAudio

PyAudio provides Python bindings for Port Audio, the cross-platform audio I/O library. With PyAudio, you can easily use Python to play and record audio on a variety of platforms. To install PyAudio open terminal or command prompt and type this command and hit enter. 

For linux:

sudo apt-get install python-pyaudio python3-pyaudio 

For windows:

pip install PyAudio

This command will install PyAudio for both Python 2 and Python 3.

Also Read: Python Text to Speech Example

2. Install Python Speech Recognition Module

It is a Library for performing speech recognition, with support for several engines and APIs, online and offline. To install it open terminal or command prompt, type the command  mentioned below and hit enter.

pip install SpeechRecognition

Python Speech Recognition Program


Say something!

You said: hello how are you

I have said “hello are you”. That’s why it is printing the same. You can speak anything you want and  this program will record your audio and convert it into text and print it on the screen.

Isn’t it awesome.

Now let’s understand the program.

In first line we’re just importing the package speech_recognition as sr. Then we are making a object of class Recognizer to use the methods listen() and recognize_google(). Class Microphone is used to use the default microphone as the audio source.

listen() method will listen for the first phrase and extract it into audio data.

Then we’ll pass that audio in recognize_google() method and this method will return the text generated from that audio.

The reason to put recognize_google method into try – except statements is that if any error  occur while recognizing it then  we can print that error using except.

Note: Internet connection will be needed while running this program.

Comment down below if you have any queries related to python speech to text tutorial.

2 thoughts on “Python Speech Recognition Using Google Speech API”

  1. Hi sir,
    I have installed all the requirements. I am trying on ubuntu 16.04. Code not give any error . But it doesnt provide any output. It stucks in “Say something!” page. I uses external microphone. please help me.

  2. We are using google api to convert speech to text in this code but isn’t using google api chargeable after a certain use ?

Leave a Comment

Your email address will not be published. Required fields are marked *