pyautogui.press()在调用时导致滞后

时间:2017-10-13 19:14:51

标签: python opencv numpy lag pyautogui

我一直致力于一个我需要调用pyautogui.press(' space')的项目,但是,当调用它时,会出现明显的滞后量。我需要尝试保持代码运行得相当快,因为​​正在使用OpenCV。如果有人知道如何在调用pyautogui.press(' space')时试图阻止代码减速,那将是惊人的。每当恐龙跳跃时,您还可以在此视频中看到延迟:https://www.youtube.com/watch?v=vceDabnT3OE

以下是代码:

import numpy as np
import cv2
import pyautogui
import time
from PIL import ImageGrab

# Defining Template Images
gameOver = cv2.imread('GameOver.png')
dino = cv2.imread('Dino.png')
smallCactus = cv2.imread('SmallCactus.png')
bigCactus = cv2.imread('BigCactus.png')
ptero = cv2.imread('Ptero.png')

# Assigning Sample Image Dimensions
h, w = dino.shape[:-1]
sch, scw = smallCactus.shape[:-1]
bch, bcw = bigCactus.shape[:-1]
ph, pw = ptero.shape[:-1]

# Time Variables
lastTime = time.time()
runningTime = 0

# Key Variables
keyDown = False

pyautogui.keyDown('space')

while True:
    # Capturing Screen
    # 'bbox' Is Rectangle Around The Game
    screen = np.array(ImageGrab.grab(bbox=(150,125,800,300)))

    # Time stuff
    #print('Loop took {} seconds'.format(time.time() - lastTime))
    runningTime += time.time() - lastTime
    lastTime = time.time()

    # Checking If Game Over
    gameOverRes = cv2.matchTemplate(screen, gameOver, cv2.TM_CCOEFF_NORMED)
    minValG, maxValG, minLocG, maxLocG = cv2.minMaxLoc(gameOverRes)

    if maxValG >= 0.9 and runningTime > 4:
        print('Game Ended In ', int(round(runningTime)), ' Seconds')
        pyautogui.press('space')
        runningTime = 0

    # Finding Dinosaur
    dinoRes = cv2.matchTemplate(screen, dino, cv2.TM_CCOEFF_NORMED)
    minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(dinoRes)

    # Finding Small Cacti
    smallCactusRes = cv2.matchTemplate(screen, smallCactus, cv2.TM_CCOEFF_NORMED)
    smallCactusThreshhold = 0.725
    smallCactusLoc = np.where(smallCactusRes >= smallCactusThreshhold)

    # Finding Big Cacti
    bigCactusRes = cv2.matchTemplate(screen, bigCactus, cv2.TM_CCOEFF_NORMED)
    bigCactusThreshhold = 0.725
    bigCactusLoc = np.where(bigCactusRes >= bigCactusThreshhold)

    # Finding Pterodactyls
    pteroRes = cv2.matchTemplate(screen, ptero, cv2.TM_CCOEFF_NORMED)
    minValP, maxValP, minLocP, maxLocP = cv2.minMaxLoc(pteroRes)

    # Drawing Box Around Dinosaur
    cv2.rectangle(screen, maxLoc, (maxLoc[0] + w, maxLoc[1] + h), (0, 255, 0), 2)

    # Avoiding Closest Small Cactus
    if smallCactusLoc[0].size > 0:
        leftmostXS = min(smallCactusLoc[1])
        leftmostYS = min(smallCactusLoc[0])

        distS = (leftmostXS - maxLoc[0])

        if (distS < 175 and distS > 0):
            pyautogui.press('space')

        cv2.rectangle(screen, (leftmostXS, leftmostYS), (leftmostXS+scw, leftmostYS+sch), (255, 160, 0), 2)

    # Avoiding Closest Big Cactus
    if bigCactusLoc[0].size > 0:
        leftmostXB = min(bigCactusLoc[1])
        leftmostYB = min(bigCactusLoc[0])

        distB = (leftmostXB - maxLoc[0])

        if distB < 175 and distB > 0:
            pyautogui.press('space')

        cv2.rectangle(screen, (leftmostXB, leftmostYB), (leftmostXB+bcw, leftmostYB+bch), (255, 0, 0), 2)


    # Avoiding Pterodactyls
    # Check 'maxValP' Because Otherwise Dino Gets Mistaken As Pterodactyl
    # 'keyDown' Is Needed For Down Arrow, Otherwise It Doesn't Work Properly
    if maxValP >= 0.60:

        distP = maxLocP[0] - maxLoc[0]
        heightP = maxLoc[1] - maxLocP[1]

        if distP < 190 and distP > 0:
            if heightP > 10:
                keyDown = True
                pyautogui.keyDown('down')
            else:
                pyautogui.press('space')            

        cv2.rectangle(screen, maxLocP, (maxLocP[0] + pw, maxLocP[1] + ph), (0, 0, 255), 2)

    # elif keyDown == True:
        # pyautogui.keyUp('down')
        # keyDown = False

    # Showing Image
    cv2.imshow('Dino Game', cv2.cvtColor(screen, cv2.COLOR_BGR2RGB))

    # Quit
    if cv2.waitKey(1) & 0xFF == 27:
        cv2.destroyAllWindows()
        break

3 个答案:

答案 0 :(得分:2)

我是PyAutoGUI的作者。 PyAutoGUI有一个“故障安全”功能,以帮助你的脚本有问题,你想要关闭它,但它可能会移动鼠标,使其无法击中键盘。在所有PyAutoGUI调用之后有0.1秒的延迟,让你有机会将鼠标猛击到右上角(如果鼠标位于坐标(0,0),PyAutoGUI将引发FailSafeException。)

第十秒延迟使用户有机会将鼠标移动到左上角。但是,您也可以将pyautogui.PAUSE设置为0

来停用此功能
>>> pyautogui.PAUSE = 0

但是,这意味着如果出现问题并且您的脚本导致鼠标不断点击,您可能会更难以杀死脚本。

答案 1 :(得分:0)

我认为单击或打字等功能存在一些故障。我发现,如果您循环使用它们,可能会出现空鼠标点击或误写的情况,例如如果您想编写1001,则只能得到101。目前正在测试诸如mouseDown和mouseUp之类的原始函数,看来它们往往会更好地工作。 希望能对某人有所帮助。

答案 2 :(得分:0)

在包的 init .py文件中

设置MINIMUM_SLEEP = 0.0。它将超级快速地工作。