Codementor Events

Simple Speech to Text Converter Using Speech Recognization in Python

Published Mar 23, 2020
Simple Speech to Text Converter Using Speech Recognization in Python

In this part, I'm going to share small script to create speech to text converter in python using google Speech Recognition module.

Here is the code -

import speech_recognition as sr

r = sr.Recognizer() 

with sr.Microphone() as source:
    print('Speak Anything : ')
    audio = r.listen(source)

    try:
        text = r.recognize_google(audio)
        print('You said: {}'.format(text))
    except:
        print('Sorry could not hear')

Explanation -
Line 1 - We are importing the speech_recognition module as sr
Line 3 - We are assingning the Recognizer object to r
Line 5 to 7 - We are using Microphone object to listen the audio.
Line 9 to 13 - We are using try-catch to convert the audion into source.

Thanks.
Happy Coding.

Discover and read more posts from Avnish Yadav
get started