简体   繁体   English

Python AI助手唤醒词

[英]Python wakeup word for AI assistant

I have a voice assistant in python and can't add a wake word.我在 python 中有语音助手,无法添加唤醒词。 When I execute the code it just keeps listening until I stop talkin then it shuts off I need to make it keep listening for a wake word and when it hears the wake word to listen for I command.当我执行代码时,它一直在听,直到我停止说话然后它关闭我需要让它继续听一个唤醒词,当它听到唤醒词时听我的命令。

import speech_recognition as sr
import pyttsx3
import pywhatkit
import datetime
import wikipedia
import pyjokes
import requests
from bs4 import BeautifulSoup
from selenium import webdriver

listener = sr.Recognizer()
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
rn = sr.Recognizer()


def talk(text):
    engine.say(text)
    engine.runAndWait()


def take_command():
    try:
        with sr.Microphone() as source:
            print('listening...')
            rn.adjust_for_ambient_noise(source)
            voice = rn.listen(source)
            command = listener.recognize_google(voice)
            command = command.lower()
            if 'alexa' in command:
                command = command.replace('alexa', '')
                print(command)
    except:
        pass
    return command


def run_alexa():
    command = take_command()
    print(command)
    if 'play' in command:
        song = command.replace('play', '')
        talk('playing ' + song)
        pywhatkit.playonyt(song)
    elif 'time' in command:
        time = datetime.datetime.now().strftime('%I:%M %p')
        print(time)
        talk('Current time is ' + time)
    else:
        talk('Please say the command again.')


while True:
    run_alexa()

You can try doing the following:您可以尝试执行以下操作:

wake_word = "your wake word"

while True:
    print("Listening")
    text = take_command()

    if text.count(wake_word) > 0:
        talk("I am ready")
        run_alexa

I'm also working on a voice assistant and I'm also trying to figure out how to implement a wake word.我也在研究语音助手,我也在试图弄清楚如何实现唤醒词。

I posted a forum here: https://python-forum.io/thread-36045.html on the python-forums.io website for help on how to implement a wake word into my code.我在这里发布了一个论坛: https://python-forum.io/thread-36045.html上的 python-forums.io 网站上的帮助代码。

Since you're code is similar to mine, I suggest you check out the link that way if someone replies or I end up figuring it out it will be posted there and you can modify it to fit you're needs.由于您的代码与我的代码相似,因此我建议您以这种方式查看链接,如果有人回复或者我最终发现它会发布在那里,您可以修改它以满足您的需求。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM