top of page
  • Writer's pictureThe Arithmancer

Voice Recognition in Python

Updated: Oct 31, 2019

In this blog, we will write some code to do speech recognition using Google Speech-To-Text from within your Python Code. Finally, we will walk you through


$ sudo pip3 install SpeechRecognition
$ sudo apt-get install python-pyaudio 
$ sudo apt-get install flac 

Python Import Functions at Top of Code:

import pyaudio
import speech_recognition as sr

Microphone Setup

The first thing to do is figure out the index of your microphone so we can set it up correctly.

for index, name in enumerate(sr.Microphone.list_microphone_names()):
    print(" \"{1}\" at (device_index={0})`".format(index, name))

This will print out something like the following:

"bcm2835 ALSA: IEC958/HDMI (hw:0,1)" at (device_index=0)` 
"AK5371: USB Audio (hw:1,0)" at (device_index=1)` 
"dmix" at (device_index=2)`

Setting Up the Listener

# Note that you only need to call the function "adjust_for_ambient_noise"  once when setting up the listener. The default is set too high so the  microphone may mistake speech for noise!

The listen_in_background function now sets up a thread where the Pi will listen out for speech in the background while your code can continue running other functions (we will need this later). When speech is heard, the "callback" function will be called.

r = sr.Recognizer() 
m = sr.Microphone(device_index=1) 
with m as source: 
stop_listening = r.listen_in_background(m, callback) 

In the callback function, we call "recognizer.recognize_google" which returns the speech as text. We pass this to our function "ReactToSpeech" which is the code where we write the code where we want the mirror to react. One example below.

def callback(recognizer,audio): 
        Speech= recognizer.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 service; {0}".format(e)) 
def ReactToSpeech(Speech): 
   if "mirror on the wall" in Speech: 
        Say("Who is the fairest of them all") # From last tutorial
def StopListening(): 

When we are ready to quit, we can call the function "StopListening" to release the thread.

Full code up to this tutorial can be downloaded from my github account here.

358 views0 comments

Recent Posts

See All


bottom of page