首页 > 解决方案 > 如何让语音助手停止听我的命令并开始听我的命令

问题描述

嗨,所以我做了一个 python 语音助手,我想知道是否有人可以帮助我弄清楚如何在不退出/结束脚本的情况下禁用它,所以例如我说“去睡觉”它会停止听我或做为我完成任务,但它不会退出,当我说“唤醒”时它会重新上线,并会听我说什么并完成我的任务

这是我使用的代码:

import speech_recognition as sr
import wikipedia
import datetime
import pywhatkit
import webbrowser as wb
import os
from requests import get
import wolframalpha

#wolframalpha
try:
    app = wolframalpha.Client('')
except Exception:
    print("Some features are not working")

#pyttsx3

engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[4].id) #changing index changes voices
engine.runAndWait()


def speak(audio):
    engine.say(audio)
    print(audio)
    engine.runAndWait()


def takeCommand():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        speak("Listening...")
        audio = r.listen(source)

    try:
        print("Recognizing...")
        query = r.recognize_google(audio, language='en-in')
        print(f"user said: {query}\n")

    except Exception as e:
        print("Sorry i didn't catch that...")
        return ""
    return query


def wish():
    hour = datetime.datetime.now().hour
    if hour >= 6 and hour < 12:
        speak("Good Morning Sir!")
    elif hour >= 12 and hour < 18:
        speak("Good after noon Sir!")
    elif hour >= 18 and hour < 24:
        speak("Good evening Sir!")
    
    speak("I am jarvis your personal assistant")


def time():
    Time = datetime.datetime.now().strftime("%I:%M:%S")
    speak(Time)


def date():
    year = int(datetime.datetime.now().year)
    month = int(datetime.datetime.now().month)
    date = int(datetime.datetime.now().day)
    speak(date)
    speak(month)
    speak(year)



if __name__ == "__main__":
    wish()
    while True:

        query = takeCommand()

        if 'according to wikipedia' in query.lower():
            speak('Searching wikipedia...')
            query = query.replace("wikipedia", "")
            try:
                results = wikipedia.summary(query, sentences=3)
                speak(results)
            except wikipedia.exceptions.PageError:
                pass

        elif 'numbers' in query.lower():
            try:
                res = app.query(query)
                speak(next(res.results).text)
            except:
                print("Internet connection error ")

        elif 'science' in query.lower():
            try:
                res = app.query(query)
                speak(next(res.results).text)
            except:
                print("Internet connection error ")
            

        elif 'what is your name' in query.lower():
            speak("My name is Jarvis, I am your personal assistant"

标签: pythonpython-3.x

解决方案


您可以创建一个布尔变量,

if __name__ == "__main__":
    wish()
    is_taking_commands = True
    while True:
        query = takeCommand()
        if 'go to sleep' in query.lower():
            is_taking_commands = False
        if 'wake up' in query.lower():
            is_taking_commands = True
        if not is_taking_commands:
            continue
        if 'according to wikipedia' in query.lower():
            speak('Searching wikipedia...')
            query = query.replace("wikipedia", "")
            try:
                results = wikipedia.summary(query, sentences=3)
                speak(results)
            except wikipedia.exceptions.PageError:
                pass

        elif 'numbers' in query.lower():
            try:
                res = app.query(query)
                speak(next(res.results).text)
            except:
                print("Internet connection error ")

        elif 'science' in query.lower():
            try:
                res = app.query(query)
                speak(next(res.results).text)
            except:
                print("Internet connection error ")
            

        elif 'what is your name' in query.lower():
            speak("My name is Jarvis, I am your personal assistant")

你最后也错过了一个括号


推荐阅读