[英]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
会一直倾听,所以你有两个选择
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"
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.