如何在python中打断语音助手

问题描述

我正在构建一个可以讲故事的语音助手。当机器人在讲故事时,我想在中间打断它并要求它停止或倒退或结束故事。我尝试了几种方法,但它们不起作用我在说话时无法听,因为在说话部分结束后它会进入听力部分。

提前致谢

这是我的代码

while True:
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Talk")
        
        audio_text = r.listen(source)
        print("Time over,thanks")
        try:
            
            inp=r.recognize_google(audio_text,language = "en-IN")
            print("Text: "+inp)
        except:
            inp="sorry"
            print("Sorry,I did not get that")
    
    
    if inp.lower() == "quit":
        bot_voice("Ok bye see you later")
        break
    if inp.lower() == "sorry":
        bot_voice("Sorry,I did not get that")
    if (deter==0):
        y=-1
        deter=1
        for x in stories:
            m=x
            y=m.find(inp)
            if(y>-1):
                filename = 'Stories/data/'+x+'.txt'
                with open(filename) as f_in:
                    for line in f_in:
                        bot_voice(line)
                break
            
    
    else:
        results = model.predict([bag_of_words(inp,words)])
        results_index = numpy.argmax(results)
        tag = labels[results_index]

        for tg in data["intents"]:
            if tg['tag'] == tag:
                responses = tg['responses']
        reply=random.choice(responses)
        if(reply=='7417'):
            filename = "Stories/list.txt"
            bot_voice("I kNow quite a few stories,they are")
            with open(filename) as f_in:
                for x,line in enumerate(f_in):
                    bot_voice(line)
            bot_voice("which one you want")
            deter=0
        else:
            print("bot:",reply)
            bot_voice(reply)

解决方法

这在您使用的语音识别中是不可能的。这种语音识别接受输入而不提供输出。使用您的输出系统(我认为它类似于 pyttsx),它只会按照说明读取。您将需要一个停止系统,并且您需要使用基于机器学习的程序来执行此操作,该程序能够进行对话并且可以在被告知停止并将关键字作为命令时停止。

我推荐 Pytorch 作为 Python 机器学习的入门者。这是一篇关于对话式 AI 的文章。

Article on Conversational AI with Pytorch