首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >人工智能助手的Python唤醒词

人工智能助手的Python唤醒词
EN

Stack Overflow用户
提问于 2022-01-02 18:56:46
回答 1查看 687关注 0票数 1

我在python有一个语音助理,不能添加一个唤醒词。当我执行代码时,它一直在听,直到我停止说话,然后它关闭,我需要让它继续侦听一个唤醒词,当它听到唤醒词来听我命令时。

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

发布于 2022-02-08 00:25:03

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

代码语言:javascript
复制
wake_word = "your wake word"

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

    if text.count(wake_word) > 0:
        talk("I am ready")
        run_alexa
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70558862

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档