Open In App

Python: Convert Speech to text and text to Speech

Speech Recognition is an important feature in several applications used such as home automation, artificial intelligence, etc. This article aims to provide an introduction on how to make use of the SpeechRecognition and pyttsx3 library of Python.
Installation required: 
 

pip install speechrecognition
sudo apt-get install python3-pyaudio
pip install pyaudio
pip install pyttsx3

Speech Input Using a Microphone and Translation of Speech to Text 
 



Translation of Speech to Text:
First, we need to import the library and then initialize it using init() function. This function may take 2 arguments. 
 

init(driverName string, debug bool)

 



After initialization, we will make the program speak the text using say() function. 
This method may also take 2 arguments. 
 

say(text unicode, name string)

 

Finally, to run the speech we use runAndWait() All the say() texts won’t be said unless the interpreter encounters runAndWait().
Below is the implementation.
 




# Python program to translate
# speech to text and text to speech
 
 
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 google to recognize audio
            MyText = r.recognize_google(audio2)
            MyText = MyText.lower()
 
            print("Did you say ",MyText)
            SpeakText(MyText)
             
    except sr.RequestError as e:
        print("Could not request results; {0}".format(e))
         
    except sr.UnknownValueError:
        print("unknown error occurred")

Input: voice speech (Hi buddy how are you)  

Output: Did you say hi buddy how are you

Article Tags :