如何在 python 中的元素中使用语音识别提供输入

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()   

当我 运行 这段代码一切正常,但它要求写入密码的长度,当我在其中写入长度时,代码会继续,但我不想在其中写入任何内容有什么方法可以让我在函数 plen (#p) 中提供语音输入,以便在语音命令的帮助下完成程序工作。 我正在使用 python 3.8

天哪,我终于自己得到了问题的答案。 让我们看看答案 在行 plen =int(input('what is the length of the password')) #p 我们需要将此代码更改为 plen =int(takecom()) 表明我们将要说的单词将直接转换为其中的输入,并且该代码将与语音命令一起正常工作。

这是最终代码

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() 

改一下↓

plen =int(input('what is the length of the password'))  #p

到↓

plen =int(takecom())  #p