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.
The following must be installed:
- Python Speech Recognition module:
sudo pip install SpeechRecognition
- PyAudio: Use the following command for linux users
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 this
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:
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
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 using
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 alsamixer
As 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
- 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 firstname.lastname@example.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 | Speech recognition on large audio files
- Python: Convert Speech to text and text to Speech
- Python | Text to Speech by using pyttsx3
- Convert Text to Speech in Python
- Python | Convert image to text and then to speech
- Python | Part of Speech Tagging using TextBlob
- Python Text To Speech | pyttsx module
- Text-To-Speech changing voice in Python
- Convert Text to Speech in Python using win32com.client
- Part of Speech Tagging with Stop words using NLTK in python
- Converting Text to Speech in Java
- Speech To Text using IBM Watson Studio
- NLP | Part of Speech - Default Tagging
- NLP | Part of speech tagged - word corpus
- Google Chrome Dino Bot using Image Recognition | Python
- Python | Multiple Face Recognition using dlib
- Python | Named Entity Recognition (NER) using spaCy
- Python | Reading contents of PDF using OCR (Optical Character Recognition)
- Python | Get a google map image of specified location using Google Static Maps API
- ML | Face Recognition Using PCA Implementation