Convert speech to text in Python
In this post, you will learn how to convert speech to text using the Python programming language.
Speech recognition is one of the most intriguing topics in AI. It helps to translate the human language into text. As we can see, speech recognition is utilized in numerous everyday applications, like marketing, news, banking, healthcare, internet of things, etc. A speech recognition system translates spoken languages into text. Speech-To-Text (STT) system takes human speech through an audio file or microphone as input and returns a string of words as output.
Here, we have mentioned how to recognize the speech from the microphone and the audio file and convert it to text.
Install required modules
The process of converting speech to text can be done with the help of "Speech Recognition" and "PyAudio" libraries. The PyAudio library helps to receive audio input and output through the microphone and speaker.
pip install pyaudio
pip install SpeechRecognition
Python Speech Recognition from Microphone
In the given Python program, first we have imported the 'speech_recognition' module and initialized the recognizer class in order to recognize the speech. This code records the sound from the microphone. We have set the time 4 seconds to hear the microphone and convert that into text using google speech recognition. The google recognizer supports different languages, by default it reads English.
import speech_recognition as sr
# initialize the recognizer
recognizer = sr.Recognizer()
# recording the sound using microphone
with sr.Microphone() as source:
print("Adjusting noise ")
recognizer.adjust_for_ambient_noise(source, duration=1)
print("Recording for 4 seconds")
recorded_audio = recognizer.listen(source, timeout=4)
print("Done recording")
# Convert Speech to text
# Using google speech recognition
try:
print("Recognizing the text")
text = recognizer.recognize_google(
recorded_audio,
language="en-US"
)
print("Converting sound records into text : {}".format(text))
except Exception as ex:
print(ex)
Python Speech Recognition from Audio File
In the given example, we have used the Audio class instead of the Microphone class. The remaining steps are the same as above.
import speech_recognition as sr
# initialize the recognizer
recognizer = sr.Recognizer()
# recording the sound using audio file
with sr.AudioFile("lecture.wav") as source:
print("Adjusting noise ")
recognizer.adjust_for_ambient_noise(source, duration=1)
print("Recording for 4 seconds")
recorded_audio = recognizer.listen(source)
print("Done recording")
# Convert Speech to text
# Using google speech recognition
try:
print("Recognizing the text")
text = recognizer.recognize_google(
recorded_audio,
language="en-US"
)
print("Converting sound records into text : {}".format(text))
except Exception as ex:
print(ex)
Related Articles
Python convert list to numpy arrayHow to shuffle a list in Python
Print hollow diamond pattern in Python
Python *args **kwargs
Write a python program to sum all the numbers in a list
Python Text to Speech
Convert array to list Python
Python Spell Checker Program
Convert list to dictionary Python
Python dict inside list
Python Web Scraping Documentation
Remove last element from list Python
Convert string to list Python
Python add list to list
Contour Detection using Python OpenCV
Python weather api
Alphabetical order Python
Python progress bar tqdm
Create word cloud using Python