2017-10-13 173 views
1

我一直在研究一个项目,我需要调用pyautogui.press('space'),但是,当调用这个项目时,会有一个明显的滞后。我需要尝试使代码以相当快的速度运行,因为正在使用OpenCV。如果有人知道我怎样才能在pyautogui.press('space')被调用时阻止代码放慢速度,那将是惊人的。当恐龙跳跃时,您还可以在视频中看到延迟:https://www.youtube.com/watch?v=vceDabnT3OEpyautogui.press()在调用时导致延迟

下面是代码:

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 

回答

0

我PyAutoGUI的作者。 PyAutoGUI具有“自动防故障”功能,可帮助防止您的脚本出现故障并想关闭它,但它可能会移动鼠标,导致无法敲击键盘。所有PyAutoGUI调用后都会有0.1秒的延迟,让您有机会将鼠标猛击到右上角(如果鼠标位于坐标(0,0)处,PyAutoGUI将增加FailSafeException)。

第十二秒延迟使用户有机会将鼠标移动到左上角。不过,您也可以通过设置pyautogui.PAUSE0禁用此:

>>> pyautogui.PAUSE = 0 

但是,这意味着如果事情搞砸了,你的脚本会导致鼠标点击不断身边,你可能有一个更难杀死你的脚本。