Speech to text conversion in Python
In this tutorial, we are going to learn how to convert Speech into text in very few lines of code in Python.
So first of all, you need to make sure that you have the following libraries installed in your machine.
pip install SpeechRecognition
pip install PyAudio
You might get an attribute error as “Could not find PyAudio, check installation”. If you encounter this error, try pip install pipwin followed by pipwin install PyAudio.
Now we need to import SpeechRecognition as
import speech_recognition as sr
After importing, now we need to complete the code which is to recognize and use microphone as a source followed by listening to what is said and then print.
r = sr.Recognizer() with sr.Microphone() as source: print("Say Something") audio = r.listen(source) try: print("Did you said"+ r.recognize_google(audio)) except: print("Could not recognize")
In the above code, we have a few lines of code to know how speech recognition works. The purpose of sr.Recognizer() class is to recognize speech, sr.Microphone() will select default Microphone as a source of audio. When “Say Something” appears, start speaking. listen() will stop listening only when silence is detected. The speech file will be then sent to google API and then it will return a string which will be displayed in the output. If any error occurs, it will print (“Could not recognize”).
Input: We have just completed a tutorial in CodeSpeedy to convert Speech into Text. Output: Did you said we have just completed a tutorial in codespeedy to convert speech into text.
You might refer to some of Codespeedy’s previous tutorials as well to make it more clear.
If you get any issue related to the microphone name or device index, you can refer to the following tutorial.
Thanks for reading!