Welcome! Meet our Python Code Assistant, your new coding buddy. Why wait? Start exploring now!
Speech recognition is the ability of computer software to identify words and phrases in spoken language and convert them to human-readable text. In this tutorial, you will learn how you can convert speech to text in Python using the SpeechRecognition library.
As a result, we do not need to build any machine learning model from scratch, this library provides us with convenient wrappers for various well-known public speech recognition APIs (such as Google Cloud Speech API, IBM Speech To Text, etc.).
Note that if you do not want to use APIs, and directly perform inference on machine learning models instead, then definitely check this tutorial, in which I'll show you how you can use the current state-of-the-art machine learning model to perform speech recognition in Python.
Also, if you want other methods to do ASR, then check this speech recognition comprehensive tutorial.
Learn also: How to Translate Text in Python.
Alright, let's get started, installing the library using pip
:
Okay, open up a new Python file and import it:
The nice thing about this library is it supports several recognition engines:
We gonna use Google Speech Recognition here, as it's straightforward and doesn't require any API key.
Make sure you have an audio file in the current directory that contains English speech (if you want to follow along with me, get the audio file here):
This file was grabbed from the LibriSpeech dataset, but you can use any audio WAV file you want, just change the name of the file, let's initialize our speech recognizer:
The below code is responsible for loading the audio file, and converting the speech into text using Google Speech Recognition:
This will take a few seconds to finish, as it uploads the file to Google and grabs the output, here is my result:
The above code works well for small or medium size audio files. In the next section, we gonna write code for large files.
If you want to perform speech recognition of a long audio file, then the below function handles that quite well:
Note: You need to install Pydub using pip
for the above code to work.
The above function uses split_on_silence()
function from pydub.silence
module to split audio data into chunks on silence. The min_silence_len
parameter is the minimum length of silence in milliseconds to be used for a split.
silence_thresh
is the threshold in which anything quieter than this will be considered silence, I have set it to the average dBFS minus 14, keep_silence
argument is the amount of silence to leave at the beginning and the end of each chunk detected in milliseconds.
These parameters won't be perfect for all sound files, try to experiment with these parameters with your large audio needs.
After that, we iterate over all chunks and convert each speech audio into text, and then add them up altogether, here is an example run:
Note: You can get 7601-291468-0006.wav
file here.
Output:
So, this function automatically creates a folder for us and puts the chunks of the original audio file we specified, and then it runs speech recognition on all of them.
In case you want to split the audio file into fixed intervals, we can use the below function instead:
The above function splits the large audio file into chunks of 5 minutes. You can change the minutes
parameter to fit your needs. Since my audio file isn't that large, I'm trying to split it into chunks of 10 seconds:
Output:
This requires PyAudio to be installed on your machine, here is the installation process depending on your operating system:
You can just pip install it:
You need to first install the dependencies:
You need to first install portaudio, then you can just pip install it:
Now let's use our microphone to convert our speech:
This will hear from your microphone for 5 seconds and then try to convert that speech into text!
It is pretty similar to the previous code, but we are using the Microphone()
object here to read the audio from the default microphone, and then we used the duration
parameter in the record()
function to stop reading after 5 seconds and then upload the audio data to Google to get the output text.
You can also use the offset
parameter in the record()
function to start recording after offset
seconds.
Also, you can recognize different languages by passing the language
parameter to the recognize_google()
function. For instance, if you want to recognize Spanish speech, you would use:
Check out supported languages in this StackOverflow answer.
As you can see, it is pretty easy and simple to use this library for converting speech to text. This library is widely used out there in the wild. Check the official documentation.
If you want to convert text to speech in Python as well, check this tutorial.
Read Also: How to Recognize Optical Characters in Images in Python.
Happy Coding ♥
Liked what you read? You'll love what you can learn from our AI-powered Code Explainer. Check it out!
View Full Code Switch My Framework
Got a coding query or need some guidance before you comment? Check out this Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips!