python——为什么ai会重复这个函数?

eoigrqb6  于 2021-09-08  发布在  Java
关注(0)|答案(2)|浏览(367)

当我说“星期五”“截图”时,它截图。一切都好。但它重复了这个功能。上面写着“我拍了你的主屏幕截图”,然后一次又一次地拍。只有这个功能。我尝试了其他函数,但它只重复了一次——这是另一个需要解决的问题。
主要代码:

import functions as FF
import speakandrecognizefunctions as SRF
import datetime
import pyautogui

WAKE_WORD = "friday"
USER = "user"
PATH = "C://MeineDirection"

def success():
    print("Succesful")

def screenshot():
    date = datetime.datetime.now()
    filename = str(date).replace(":", "-") + "-screenshot.png"
    img = pyautogui.screenshot()
    img.save(f'{PATH}//screenshots//{filename}')
    SRF.speak("I took a screenshot of your main screen " + USER)

while True:
    text = SRF.takecommandbackground()

    if text.count(WAKE_WORD) > 0:

        SRF.speak("Im listening " + USER)
        print("\nListening....")

        text = SRF.takecommand()

        SCREENSHOT_STRS = ["make a screenshot", "take a screenshot", "screenshot"]
        for phrase in SCREENSHOT_STRS:
            if phrase in text:
                screenshot()
                success()

语音识别码:

import pyttsx3
import speech_recognition as sr
import config

engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)

WAKE_WORD = "friday"

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

def takecommand():
    r = sr.Recognizer()

    with sr.Microphone() as source:
        #r.adjust_for_ambient_noise(source, duration=0.5)
        audio = r.listen(source)
        said = ""

        try:
            said = r.recognize_google(audio)
            print(said)

        except Exception as e:
            speak("I didnt get that !!")
            print(f"Exception. Say {WAKE_WORD} and try again " + str(e))
            pass

    return said.lower()

def takecommandbackground():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        r.adjust_for_ambient_noise(source, duration=0.3)
        #print("Ready")
        audio = r.listen(source, phrase_time_limit=4)

        try:
            # print("Recognizing....")
            query = r.recognize_google(audio)
            #print("user said : ", query)
            # speak(query)

        except Exception:
            #print("Say that again please !! ")
            return "none"
    return query.lower()
owfi6suc

owfi6suc1#

简单的“截图”将触发“截图”和“截图”从 SCREENSHOT_STRS 看看是什么。如果这样做有效,那么您希望按照@jasonharper的建议打破循环

3phpmpom

3phpmpom2#

您需要中断循环,因为它一直在循环,或者清除文本变量。

相关问题