简体   繁体   English

如何让我的 python 虚拟助手仅在听到唤醒词后才收听?

[英]How do I get my python virtual assistant to only listen once it hears a wake word?

I have a python virtual assistant that is always listening for commands.我有一个 python 虚拟助手,它总是在监听命令。 How can I make it so she only starts listening to commands once she hears the wake word such as Alexa, or in this case Anna.我怎样才能让她只在听到唤醒词(例如 Alexa,或者在本例中为 Anna)后才开始听命令。 If you have any tips or answers they will be greatly appreciated.如果您有任何提示或答案,我们将不胜感激。 Thank you!谢谢!

import speech_recognition as sr
import pyttsx3

tts = pyttsx3.init()

voices = tts.getProperty('voices')
tts.setProperty('voice', voices[1].id)

def takeCommand():

    r = sr.Recognizer()

    with sr.Microphone() as source:
        print('Listening...')

        r.pause_threshold = 0.5
        audio = r.listen(source)
        
        try:
            print("Recognizing")
            
            Query = r.recognize_google(audio, language='en-us')
            print("the command is printed=", Query)
            
        except Exception as e:
            print(e)
            print("Say that again")
            return "None"
        
        return Query

def Take_query():

    while(True):

        query = takeCommand().lower()
        if "Hello" in query:
            tts.say('Hello')
            tts.runAndWait()
            continue

        elif "How are you" in query:
            tts.say('I am good')
            tts.runAndWait()
            continue

if __name__ == '__main__':
    Take_query()

Anna will always listen, so you have 2 choices Anna会一直倾听,所以你有两个选择

  1. force the program to ignore any query without Anna强制程序忽略没有Anna的任何查询

so the program will ignore anything doesn't start with Anna所以程序会忽略任何不是以Anna开头的内容

def takeCommand():

    r = sr.Recognizer()

    with sr.Microphone() as source:
        print('Listening...')

        r.pause_threshold = 0.5
        audio = r.listen(source)
        
        try:
            print("Recognizing")
            
            Query = r.recognize_google(audio, language='en-us')
            print("the command is printed=", Query)
            
            
        except Exception as e:
            print(e)
            print("Say that again")
            return "None"
        if Query.startswith("Anna"):
            return Query
        else:
            return "None"
  1. Nested recognition嵌套识别

this is might be better for you, just nest the recognition, first recognize Anna then and only then the command这可能对你更好,只需嵌套识别,首先识别Anna ,然后再识别命令

hope that helps you希望对你有帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:让虚拟助手在说“嘿助手”后再次聆听 - Python: Make Virtual Assistant Listen Again After Saying “Hey Assistant” 如何在Raspberry Pi上为Google智能助理SDK设置唤醒字 - How to set up wake word for Google Assistant SDK on a Raspberry Pi 如何仅从python词典中获取一个单词? - How do I only get one word from a dictionary in python? 我如何重复该程序,以便它要求输入另一个单词并让它只打印我的句子一次? - How do I repeat the program so its asks for a another word and let it only print my sentences once? 我的代码为每一行打印“未找到”,如果搜索不成功,我如何让它只打印一次“未找到”? - My code prints “Not Found” for every line, how do i get it to only print “Not Found” once if the search is unsuccessful? 在python中,如何在选择的子字符串之前仅搜索一个单词 - In python, how do I search for only one word before my selected substring 如何通过 python 中的正则表达式搜索获取完整字符串,该字符串仅捕获部分单词? - How do i get the full strings with a RegEx search in python that only captures part of the word? 如何获取python的“ in”运算符,使其仅产生真正的单词匹配,而不仅仅是子字符串匹配? - How do I get python's “in” operator to only yield true word matching, and not just substring matching? 虚拟助手python - Virtual assistant python 如何修复我的递归倒计时 python 函数的代码,以便它只打印“LIFT OFF!” 一次? - How do I fix my code for my recursive countdown python function so that it only prints “LIFT OFF!’ once?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM