簡體   English   中英

Python AI助手喚醒詞

[英]Python wakeup word for AI assistant

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

您可以嘗試執行以下操作:

wake_word = "your wake word"

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

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

我也在研究語音助手,我也在試圖弄清楚如何實現喚醒詞。

我在這里發布了一個論壇: https://python-forum.io/thread-36045.html上的 python-forums.io 網站上的幫助代碼。

由於您的代碼與我的代碼相似,因此我建議您以這種方式查看鏈接,如果有人回復或者我最終發現它會發布在那里,您可以修改它以滿足您的需求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM