【问题标题】:How to interrupt voice assistant in python如何在python中中断语音助手
【发布时间】:2021-05-30 11:06:59
【问题描述】:

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

提前致谢

这是我的代码

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)

【问题讨论】:

    标签: python speech-recognition chatbot voice assistant


    【解决方案1】:

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

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

    Article on Conversational AI with Pytorch

    【讨论】:

      猜你喜欢
      • 2020-10-21
      • 2020-07-19
      • 1970-01-01
      • 1970-01-01
      • 2020-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多