Python:说“嘿助手”后,让虚拟助手再次收听

时间:2019-11-17 01:28:33

标签: python virtual assistant

使用虚拟助手下方的代码将侦听命令,但在说“再试一次”后将停止侦听命令。如何使用唤醒命令“嘿助手”使其再次收听。

def myCommand():

    r = sr.Recognizer()                                                                                   
    with sr.Microphone() as source:                                                                       
        print("Listening...")
        r.pause_threshold =  1
        text = r.listen(source)
    try:
        query = r.recognize_google(text, language='en')
        print('User: ' + query + '\n')

    except sr.UnknownValueError:
        speak('Sorry sir! I didn\'t get that.')
        query = str(input('Try again'))




    return query

1 个答案:

答案 0 :(得分:1)

# you can call it using this...
hotword = 'hey assistant'
while 1:
    result = myCommand()
    if hotword in result:
        #here what you want to do...
        #respose_of_assistant()