Search code examples
pythonstringspeech-recognitionspeech-to-text

How to give input using speech recognition in a element in python


import speech_recognition as sr 
import pyttsx3
import string
import random
#Text To Speech
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
#print(voices)
engine.setProperty('voice',voices[0].id)
engine.setProperty('rate', 145) #you can replace it to incease or decrease dound speed default(200)
def speak(audio):  #here audio is var which contain text
    engine.say(audio)
    engine.runAndWait()
#now convert audio to text
def takecom():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listning....")
        audio = r.listen(source)
    try:
        print("Recognising....") 
        text = r.recognize_google(audio,language='en-in')
        print(text)
    except Exception:      
        speak("error...")
        print("Network connection error") 
        return "none"
    return text
#for main function                               
if __name__ == "__main__":
    while True:
        query = takecom().lower()
        if 'create password' in query or 'c' in query :
            if __name__ == "__main__":
                s1 = string.ascii_lowercase
                s2 = string.ascii_uppercase
                s3 = string.digits
                s4 = string.punctuation
                speak('what do you want to keep the length of the password type here')
                plen =int(input('what is the length of the password'))  #p
                s=[]
                s.extend(list(s1))
                s.extend(list(s2))
                s.extend(list(s3))
                s.extend(list(s4))
                print("Your password is:")
                print("".join(random.sample(s,plen)))
                speak("".join(random.sample(s,plen)))
        elif query == 'none':
            continue 
        elif 'exit' in query or 'abort' in query or 'stop' in query or 'bye' in query or 'quit' in query:
            ex_exit = 'ok byy'
            speak(ex_exit)
            exit()   

When i run this code every thing work fine but it ask to write the length of the password and when i write to length in it then the code proceed but i don't want to write anything in it is there any way so that i can give a voice input in the function plen (#p) to do the program work with the help of voice command. I am using python 3.8


Solution

  • Oh god finally i got answer of my question by my own. let's see the answer In the line plen =int(input('what is the length of the password')) #p we need to change this code to plen =int(takecom()) show that the word which we will speak will get directly converted into input in it and the code will work properly with voice command.

    Here is the final code

    import speech_recognition as sr 
    import pyttsx3
    import string
    import random
    #Text To Speech
    engine = pyttsx3.init('sapi5')
    voices = engine.getProperty('voices')
    #print(voices)
    engine.setProperty('voice',voices[0].id)
    engine.setProperty('rate', 145) #you can replace it to incease or decrease dound speed default(200)
    def speak(audio):  #here audio is var which contain text
        engine.say(audio)
        engine.runAndWait()
    #now convert audio to text
    def takecom():
        r = sr.Recognizer()
        with sr.Microphone() as source:
            print("Listning....")
            audio = r.listen(source)
        try:
            print("Recognising....") 
            text = r.recognize_google(audio,language='en-in')
            print(text)
        except Exception:      
            speak("error...")
            print("Network connection error") 
            return "none"
        return text
    #for main function                               
    if __name__ == "__main__":
        while True:
            query = takecom().lower()
            if 'create password' in query or 'c' in query :
                if __name__ == "__main__":
                    s1 = string.ascii_lowercase
                    s2 = string.ascii_uppercase
                    s3 = string.digits
                    s4 = string.punctuation
                    speak('what do you want to keep the length of the password type here')
                    plen =int(takecom())  #p
                    s=[]
                    s.extend(list(s1))
                    s.extend(list(s2))
                    s.extend(list(s3))
                    s.extend(list(s4))
                    print("Your password is:")
                    print("".join(random.sample(s,plen)))
                    speak("".join(random.sample(s,plen)))
            elif query == 'none':
                continue 
            elif 'exit' in query or 'abort' in query or 'stop' in query or 'bye' in query or 'quit' in query:
                ex_exit = 'ok byy'
                speak(ex_exit)
                exit()