【问题标题】:Python wakeup word for AI assistantAI助手的Python唤醒词
【发布时间】:2022-06-21 07:38:04
【问题描述】:

我有一个 Python 语音助手,无法添加唤醒词。 当我执行代码时,它一直在听,直到我停止说话然后它关闭我需要让它继续听一个唤醒词,当它听到唤醒词时听我的命令。

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()

【问题讨论】:

  • 请考虑edit您的标题,以便更好地总结您的问题。 How to Ask

标签: python voice-recognition


【解决方案1】:

您可以尝试执行以下操作:

wake_word = "your wake word"

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-27
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    • 2012-12-08
    • 1970-01-01
    相关资源
    最近更新 更多