Python - convert speech to text
Python - convert speech to text
We can convert a speech to text in a easy way using python. Please follow below steps to implement the same.
Step 1: Install Python & pip in your machine.
Step 2: Install below python libraries
1) pip install SpeechRecognition
2) pip install PyAudio
Step 3: Open any IDE and write the below code.
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:
print("SAY SOMETHING")
audio = r.listen(source)
print("TIME OVER, THANKS")
try:
print("Text: "+r.recognize_google(audio));
except:
pass;
In the above code, it takes 'English' as default language. If you want to speak any other language and convert to speech, you need to modify the code little bit.
I have added a language option at 'recognize_google' line. In my case, I have added the language 'Telugu' (te-IN).
Language Code:
print("Text: "+r.recognize_google(audio, language='te-IN'));
So that, the final code will look like below:
Final Code:
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:
print("SAY SOMETHING")
audio = r.listen(source)
print("TIME OVER, THANKS")
try:
print("Text: "+r.recognize_google(audio));
except:
pass;
Step 1: Install Python & pip in your machine.
Step 2: Install below python libraries
1) pip install SpeechRecognition
2) pip install PyAudio
Step 3: Open any IDE and write the below code.
Code:
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:
print("SAY SOMETHING")
audio = r.listen(source)
print("TIME OVER, THANKS")
try:
print("Text: "+r.recognize_google(audio));
except:
pass;
In the above code, it takes 'English' as default language. If you want to speak any other language and convert to speech, you need to modify the code little bit.
I have added a language option at 'recognize_google' line. In my case, I have added the language 'Telugu' (te-IN).
Language Code:
print("Text: "+r.recognize_google(audio, language='te-IN'));
So that, the final code will look like below:
Final Code:
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:
print("SAY SOMETHING")
audio = r.listen(source)
print("TIME OVER, THANKS")
try:
print("Text: "+r.recognize_google(audio));
except:
pass;
Comments
Post a Comment