How To Place Speech Recognition In Python

 


In this post we are going to make a Speech Recognition. Speech Recognition helps in various applications like Home Automation,  Artificial Intelligence, and Apps. If you don't know what is Speech Recognition don't worry! I will help you see that, if we open any application in a smartphone, we see there was a search with voice option so this feature is enabled with Speech Recognition. Now you understand what is Speech Recognition. Now I will help you to how to install Speech Recognition in Python. There were three modules you need to install. Modules need to install are following command to install in Python.

 1."pip install speechRecognition" 

2. "pip install pyaudio

3. "pip install pyttx3

Here is the code for Speech Recognition. This will take command and give output.

import pyttsx3 #pip install pyttsx3
import speech_recognition as sr
import datetime
r = sr.Recognizer()
def SpeakText(command):
    engine = pyttsx3.init()
    engine.say(command) 
    engine.runAndWait()
while(1):    
    try:
        with sr.Microphone() as source2: 
            r.adjust_for_ambient_noise(source2, duration=0.2) 
            audio2 = r.listen(source2)
            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 occured")

Post a Comment

Previous Post Next Post