Speech Recognition in Python using Google Speech API - GeeksforGeeks

22 Oct, 2017
Speech Recognition is an important feature in several applications used such as home automation, artificial intelligence, etc. This article aims to provide an introduction on how to make use of the SpeechRecognition library of Python. This is useful as it can be used on microcontrollers such as Raspberri Pis with the help of an external microphone.
Required Installations
The following must be installed:
Python Speech Recognition module: sudo pip install SpeechRecognition PyAudio: Use the following command for linux userssudo apt-get install python-pyaudio python3-pyaudioIf the versions in the repositories are too old, install pyaudio using the following commandsudo apt-get install portaudio19-dev python-all-dev python3-all-dev && 
sudo pip install pyaudioUse pip3 instead of pip for python3.Windows users can install pyaudio by executing the following command in a terminalpip install pyaudio
Python Speech Recognition module: sudo pip install SpeechRecognition 
 sudo pip install SpeechRecognition 
PyAudio: Use the following command for linux userssudo apt-get install python-pyaudio python3-pyaudioIf the versions in the repositories are too old, install pyaudio using the following commandsudo apt-get install portaudio19-dev python-all-dev python3-all-dev && 
sudo pip install pyaudioUse pip3 instead of pip for python3.Windows users can install pyaudio by executing the following command in a terminalpip install pyaudio
sudo apt-get install python-pyaudio python3-pyaudio
If the versions in the repositories are too old, install pyaudio using the following command
sudo apt-get install portaudio19-dev python-all-dev python3-all-dev && 
sudo pip install pyaudio
Use pip3 instead of pip for python3.Windows users can install pyaudio by executing the following command in a terminal
pip install pyaudio
Speech Input Using a Microphone and Translation of Speech to Text
Configure Microphone (For external microphones): It is advisable to specify the microphone during the program to avoid any glitches.Type lsusb in the terminal. A list of connected devices will show up. The microphone name would look like thisUSB Device 0x46d:0x825: Audio (hw:1, 0)Make a note of this as it will be used in the program.Set Chunk Size: This basically involved specifying how many bytes of data we want to read at once. Typically, this value is specified in powers of 2 such as 1024 or 2048Set Sampling Rate: Sampling rate defines how often values are recorded for processingSet Device ID to the selected microphone: In this step, we specify the device ID of the microphone that we wish to use in order to avoid ambiguity in case there are multiple microphones. This also helps debug, in the sense that, while running the program, we will know whether the specified microphone is being recognized. During the program, we specify a parameter device_id. The program will say that device_id could not be found if the microphone is not recognized.Allow Adjusting for Ambient Noise: Since the surrounding noise varies, we must allow the program a second or too to adjust the energy threshold of recording so it is adjusted according to the external noise level.Speech to text translation: This is done with the help of Google Speech Recognition. This requires an active internet connection to work. However, there are certain offline Recognition systems such as PocketSphinx, but have a very rigorous installation process that requires several dependencies. Google Speech Recognition is one of the easiest to use.The Above steps have been implemented below:#Python 2.x program for Speech Recognition  import speech_recognition as sr  #enter the name of usb microphone that you found#using lsusb#the following name is only used as an examplemic_name = "USB Device 0x46d:0x825: Audio (hw:1, 0)"#Sample rate is how often values are recordedsample_rate = 48000#Chunk is like a buffer. It stores 2048 samples (bytes of data)#here. #it is advisable to use powers of 2 such as 1024 or 2048chunk_size = 2048#Initialize the recognizerr = sr.Recognizer()  #generate a list of all audio cards/microphonesmic_list = sr.Microphone.list_microphone_names()  #the following loop aims to set the device ID of the mic that#we specifically want to use to avoid ambiguity.for i, microphone_name in enumerate(mic_list):    if microphone_name == mic_name:        device_id = i  #use the microphone as source for input. Here, we also specify #which device ID to specifically look for incase the microphone #is not working, an error will pop up saying "device_id undefined"with sr.Microphone(device_index = device_id, sample_rate = sample_rate,                         chunk_size = chunk_size) as source:    #wait for a second to let the recognizer adjust the     #energy threshold based on the surrounding noise level    r.adjust_for_ambient_noise(source)    print "Say Something"    #listens for the user's input    audio = r.listen(source)              try:        text = r.recognize_google(audio)        print "you said: " + text          #error occurs when google could not understand what was said          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))Transcribe an Audio file to textIf we have an audio file that we want to translate to text, we simply have to replace the source with the audio file instead of a microphone.Place the audio file and the program in the same folder for convenience. This works for WAV, AIFF, of FLAC files.An implementation has been shown below#Python 2.x program to transcribe an Audio fileimport speech_recognition as sr  AUDIO_FILE = ("example.wav")  # use the audio file as the audio source  r = sr.Recognizer()  with sr.AudioFile(AUDIO_FILE) as source:    #reads the audio file. Here we use record instead of    #listen    audio = r.record(source)    try:    print("The audio file contains: " + 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))
Configure Microphone (For external microphones): It is advisable to specify the microphone during the program to avoid any glitches.Type lsusb in the terminal. A list of connected devices will show up. The microphone name would look like thisUSB Device 0x46d:0x825: Audio (hw:1, 0)Make a note of this as it will be used in the program.
USB Device 0x46d:0x825: Audio (hw:1, 0)
Make a note of this as it will be used in the program.
Set Chunk Size: This basically involved specifying how many bytes of data we want to read at once. Typically, this value is specified in powers of 2 such as 1024 or 2048
Set Sampling Rate: Sampling rate defines how often values are recorded for processing
Set Device ID to the selected microphone: In this step, we specify the device ID of the microphone that we wish to use in order to avoid ambiguity in case there are multiple microphones. This also helps debug, in the sense that, while running the program, we will know whether the specified microphone is being recognized. During the program, we specify a parameter device_id. The program will say that device_id could not be found if the microphone is not recognized.
Allow Adjusting for Ambient Noise: Since the surrounding noise varies, we must allow the program a second or too to adjust the energy threshold of recording so it is adjusted according to the external noise level.
Speech to text translation: This is done with the help of Google Speech Recognition. This requires an active internet connection to work. However, there are certain offline Recognition systems such as PocketSphinx, but have a very rigorous installation process that requires several dependencies. Google Speech Recognition is one of the easiest to use.The Above steps have been implemented below:#Python 2.x program for Speech Recognition  import speech_recognition as sr  #enter the name of usb microphone that you found#using lsusb#the following name is only used as an examplemic_name = "USB Device 0x46d:0x825: Audio (hw:1, 0)"#Sample rate is how often values are recordedsample_rate = 48000#Chunk is like a buffer. It stores 2048 samples (bytes of data)#here. #it is advisable to use powers of 2 such as 1024 or 2048chunk_size = 2048#Initialize the recognizerr = sr.Recognizer()  #generate a list of all audio cards/microphonesmic_list = sr.Microphone.list_microphone_names()  #the following loop aims to set the device ID of the mic that#we specifically want to use to avoid ambiguity.for i, microphone_name in enumerate(mic_list):    if microphone_name == mic_name:        device_id = i  #use the microphone as source for input. Here, we also specify #which device ID to specifically look for incase the microphone #is not working, an error will pop up saying "device_id undefined"with sr.Microphone(device_index = device_id, sample_rate = sample_rate,                         chunk_size = chunk_size) as source:    #wait for a second to let the recognizer adjust the     #energy threshold based on the surrounding noise level    r.adjust_for_ambient_noise(source)    print "Say Something"    #listens for the user's input    audio = r.listen(source)              try:        text = r.recognize_google(audio)        print "you said: " + text          #error occurs when google could not understand what was said          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))Transcribe an Audio file to textIf we have an audio file that we want to translate to text, we simply have to replace the source with the audio file instead of a microphone.Place the audio file and the program in the same folder for convenience. This works for WAV, AIFF, of FLAC files.An implementation has been shown below#Python 2.x program to transcribe an Audio fileimport speech_recognition as sr  AUDIO_FILE = ("example.wav")  # use the audio file as the audio source  r = sr.Recognizer()  with sr.AudioFile(AUDIO_FILE) as source:    #reads the audio file. Here we use record instead of    #listen    audio = r.record(source)    try:    print("The audio file contains: " + 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))
The Above steps have been implemented below:
#Python 2.x program for Speech Recognition  import speech_recognition as sr  #enter the name of usb microphone that you found#using lsusb#the following name is only used as an examplemic_name = "USB Device 0x46d:0x825: Audio (hw:1, 0)"#Sample rate is how often values are recordedsample_rate = 48000#Chunk is like a buffer. It stores 2048 samples (bytes of data)#here. #it is advisable to use powers of 2 such as 1024 or 2048chunk_size = 2048#Initialize the recognizerr = sr.Recognizer()  #generate a list of all audio cards/microphonesmic_list = sr.Microphone.list_microphone_names()  #the following loop aims to set the device ID of the mic that#we specifically want to use to avoid ambiguity.for i, microphone_name in enumerate(mic_list):    if microphone_name == mic_name:        device_id = i  #use the microphone as source for input. Here, we also specify #which device ID to specifically look for incase the microphone #is not working, an error will pop up saying "device_id undefined"with sr.Microphone(device_index = device_id, sample_rate = sample_rate,                         chunk_size = chunk_size) as source:    #wait for a second to let the recognizer adjust the     #energy threshold based on the surrounding noise level    r.adjust_for_ambient_noise(source)    print "Say Something"    #listens for the user's input    audio = r.listen(source)              try:        text = r.recognize_google(audio)        print "you said: " + text          #error occurs when google could not understand what was said          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))
Transcribe an Audio file to text
If we have an audio file that we want to translate to text, we simply have to replace the source with the audio file instead of a microphone.Place the audio file and the program in the same folder for convenience. This works for WAV, AIFF, of FLAC files.An implementation has been shown below
#Python 2.x program to transcribe an Audio fileimport speech_recognition as sr  AUDIO_FILE = ("example.wav")  # use the audio file as the audio source  r = sr.Recognizer()  with sr.AudioFile(AUDIO_FILE) as source:    #reads the audio file. Here we use record instead of    #listen    audio = r.record(source)    try:    print("The audio file contains: " + 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))
Troubleshooting
The following problems are commonly encountered
Muted Microphone: This leads to input not being received. To check for this, you can use alsamixer.It can be installed usingsudo apt-get install libasound2 alsa-utils alsa-ossType amixer. The output will look somewhat like thisSimple mixer control 'Master', 0
  Capabilities: pvolume pswitch pswitch-joined
  Playback channels: Front Left - Front Right
  Limits: Playback 0 - 65536
  Mono:
  Front Left: Playback 41855 [64%] [on]
  Front Right: Playback 65536 [100%] [on]
Simple mixer control 'Capture', 0
  Capabilities: cvolume cswitch cswitch-joined
  Capture channels: Front Left - Front Right
  Limits: Capture 0 - 65536
  Front Left: Capture 0 [0%] [off] #switched off
  Front Right: Capture 0 [0%] [off]
As you can see, the capture device is currently switched off. To switch it on, type alsamixerAs you can see in the first picture, it is displaying our playback devices. Press F4 to toggle to Capture devices.In the second picture, the highlighted portion shows that the capture device is muted. To unmute it, press space barAs you can see in the last picture, the highlighted part confirms that the capture device is not muted.Current microphone not selected as capture device:In this case, the microphone can be set by typing alsamixer and selecting sound cards. Here, you can select default microphone device.As shown in the picture, the highlighted portion is where you have to select sound card.The second picture shows the screen selection for sound cardNo Internet Connection: The speech to text conversion requires an active internet connection.
Muted Microphone: This leads to input not being received. To check for this, you can use alsamixer.It can be installed usingsudo apt-get install libasound2 alsa-utils alsa-ossType amixer. The output will look somewhat like thisSimple mixer control 'Master', 0
  Capabilities: pvolume pswitch pswitch-joined
  Playback channels: Front Left - Front Right
  Limits: Playback 0 - 65536
  Mono:
  Front Left: Playback 41855 [64%] [on]
  Front Right: Playback 65536 [100%] [on]
Simple mixer control 'Capture', 0
  Capabilities: cvolume cswitch cswitch-joined
  Capture channels: Front Left - Front Right
  Limits: Capture 0 - 65536
  Front Left: Capture 0 [0%] [off] #switched off
  Front Right: Capture 0 [0%] [off]
As you can see, the capture device is currently switched off. To switch it on, type alsamixerAs you can see in the first picture, it is displaying our playback devices. Press F4 to toggle to Capture devices.In the second picture, the highlighted portion shows that the capture device is muted. To unmute it, press space barAs you can see in the last picture, the highlighted part confirms that the capture device is not muted.
sudo apt-get install libasound2 alsa-utils alsa-oss
Type amixer. The output will look somewhat like this
Simple mixer control 'Master', 0
  Capabilities: pvolume pswitch pswitch-joined
  Playback channels: Front Left - Front Right
  Limits: Playback 0 - 65536
  Mono:
  Front Left: Playback 41855 [64%] [on]
  Front Right: Playback 65536 [100%] [on]
Simple mixer control 'Capture', 0
  Capabilities: cvolume cswitch cswitch-joined
  Capture channels: Front Left - Front Right
  Limits: Capture 0 - 65536
  Front Left: Capture 0 [0%] [off] #switched off
  Front Right: Capture 0 [0%] [off]

As you can see, the capture device is currently switched off. To switch it on, type alsamixerAs you can see in the first picture, it is displaying our playback devices. Press F4 to toggle to Capture devices.
In the second picture, the highlighted portion shows that the capture device is muted. To unmute it, press space bar
As you can see in the last picture, the highlighted part confirms that the capture device is not muted.
Current microphone not selected as capture device:In this case, the microphone can be set by typing alsamixer and selecting sound cards. Here, you can select default microphone device.As shown in the picture, the highlighted portion is where you have to select sound card.The second picture shows the screen selection for sound card
The second picture shows the screen selection for sound card
No Internet Connection: The speech to text conversion requires an active internet connection.
This article is contributed by Deepak Srivatsav. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
python-utility
GBlog
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
DSA Sheet by Love Babbar
GET and POST requests using Python
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Types of Software Testing
Working with csv files in Python
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
How to get column names in Pandas dataframe