How to Convert Voice to Text in Python
Speech Recognition is an important feature in several applications used such as home automation, artificial intelligence, etc.
First, let’s install the required modules:
pip install speechrecognition
pip install pyaudio
pip install pyttsx3
Speech Input Using a Microphone and Translation of Speech to Text
- 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.
import speech_recognition as sr import pyttsx3 # Initialize the recognizer r = sr.Recognizer() # Function to convert text to # speech def SpeakText(command): # Initialize the engine engine = pyttsx3.init() engine.say(command) engine.runAndWait() # Loop infinitely for user to # speak while(1): # Exception handling to handle # exceptions at the runtime try: # use the microphone as source for input. with sr.Microphone() as source2: # wait for a second to let the recognizer # adjust the energy threshold based on # the surrounding noise level r.adjust_for_ambient_noise(source2, duration=0.2) #listens for the user's input audio2 = r.listen(source2) # Using ggogle to recognize audio MyText = r.recognize_google(audio2) MyText = MyText.lower() print(MyText) SpeakText(MyText) except sr.RequestError as e: print("Could not request results; {0}".format(e)) except sr.UnknownValueError: print("unknown error occured")
Subscribe
Login
Please login to comment
0 Discussion