Speech to text in python

These days a number of apps use speech to text feature to interact with users. Typical example is using voice search on a grocery app, where you want to find items by voice search rather typing item names. Let us implement a basic speech to text solution using python and SpeechRecognition module.

First you have to install two modules SpeechRecognition and python3-pyaudio. On debian 10 you can install these with following commands.


sudo pip3 install SpeechRecognition
sudo aptitude install python-pyaudio python3-pyaudio

Now lets jump to the code.


#!/usr/bin/python3

import speech_recognition as sr
r = sr.Recognizer()

with sr.Microphone() as source:
  print("I am listening....")
  audio = r.listen(source)
  print("Here is what I understood....")

try:
  print("You said: "+r.recognize_google(audio))
#  print("You said: "+r.recognize_google(audio, language = 'hi-IN'))
except:
  print("Error reaching google API...")

Save above code in speech2text.py file and give execute permission. Lets have some fun with it…..


./speech2text.py
I am listening....
Here is what I understood....
You said: go back or I will shoot you