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.
sudo apt-get install python-pyaudio python3-pyaudio
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
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
text = r.recognize_google(audio)
print(“You said: “ + text)
print(“Google Speech Recognition could not understand audio”)
except sr.RequestError as e:
print(“Could not request results from Google Speech Recognition service” + format(e))
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.