如何在python中中断语音助手

2024-05-23 14:40:54 发布

您现在位置: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)

Tags: inforifastagbotwithline
1条回答
网友
1楼 · 发布于 2024-05-23 14:40:54

这在您使用的语音识别中是不可能的。这种语音识别接受输入,不提供输出。对于您的输出系统,我假设是类似于pyttsx的东西,它只是按照它所说的那样读取。你需要一个停止系统,你需要一个基于机器学习的程序来完成这项工作,该程序能够进行对话,并且在被告知停止时可以停止,并将关键字作为命令

我推荐Pytork作为Python机器学习的初学者。这里有一篇关于对话人工智能的文章

Article on Conversational AI with Pytorch

相关问题 更多 >