top of page
Writer's pictureAbhijeet Srivastav

Speech to Text -- Python



I know ! I know you have got bored from solving mathematical and logical problems in python and want something new.


Hmm....One of the advances in Computer Science is Speech Recognition.


Shouldn't We try to make something using this technology in Python?


I know you are excited to do this....Hold on your seats pythonistas lets begin!


Installing Required Modules

We need two modules for our purpose. One is SpeechRecognition module and another one is PyAudio.



pip install SpeechRecognition

pip install PyAudio

Let's Code Now

Open Your Favourite IDE and Start Coding with me.



import speech_recognition as sr 
r = sr.Recgnizer()  
with sr.Microphone() as source: 
      print('Say Something')    
               
      audio = r.listen(source)     
      
     print('Time Over, I heard that')        

try:
     print('Text :' + r.recognize_google(audio)) 
except:       
      print('Sorry Some Error Occurred! Check Your Internet Connection And Modules Installation')

What's Going On In the Code?

  1. We are importing modules.

  2. Now we are making an recognizer object from Recognition class.

  3. Now with Microphone open we are listening to the voice using audio object from Listener class.

  4. After all that we are printing our text after converting it with recognize_google class.


Note: Internet Connection is required for working of this programm.


What If I want Converted Text In My Regional Language?

So for doing this you have to just pass your language as argument in recognize_google class.



print('Text: ' + r.recognize_google(audio, language='hi-IN'))

This will give output in hindi Language.

For more languages you can google their argument on google.
















 

 

 
     
4 views0 comments

Comments


bottom of page