Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pyautogui.press()调用时导致延迟_Python_Opencv_Numpy_Lag_Pyautogui - Fatal编程技术网

Python pyautogui.press()调用时导致延迟

Python pyautogui.press()调用时导致延迟,python,opencv,numpy,lag,pyautogui,Python,Opencv,Numpy,Lag,Pyautogui,我一直在做一个需要调用pyautogui.press('space')的项目,但是,当调用它时,会有明显的延迟。我需要尝试保持代码运行相当快,因为OpenCV正在使用。如果有人知道我如何在调用pyautogui.press('space')时阻止代码变慢,那将是令人惊讶的。你也可以在这段视频中看到恐龙跳跃时的延迟: 代码如下: import numpy as np import cv2 import pyautogui import time from PIL import ImageGrab

我一直在做一个需要调用pyautogui.press('space')的项目,但是,当调用它时,会有明显的延迟。我需要尝试保持代码运行相当快,因为OpenCV正在使用。如果有人知道我如何在调用pyautogui.press('space')时阻止代码变慢,那将是令人惊讶的。你也可以在这段视频中看到恐龙跳跃时的延迟:

代码如下:

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
将numpy导入为np
进口cv2
导入pyautogui
导入时间
从PIL导入ImageGrab
#定义模板图像
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')
#指定示例图像尺寸
h、 w=恐龙形状[:-1]
sch,scw=小仙人掌。形状[:-1]
bch,bcw=bigCactus.shape[:-1]
ph,pw=翼形[:-1]
#时间变量
lastTime=time.time()
运行时间=0
#关键变量
keyDown=False
pyautogui.keyDown('space')
尽管如此:
#截屏
#“bbox”是围绕游戏的矩形
screen=np.array(ImageGrab.grab(bbox=(150125800300)))
#时间的东西
#打印('循环耗时{}秒'。格式(time.time()-lastTime))
runningTime+=time.time()-lastTime
lastTime=time.time()
#检查游戏是否结束
gameOverRes=cv2.matchTemplate(屏幕,gameOver,cv2.TM\u CCOEFF\u规范)
minValG,maxValG,minLocG,maxLocG=cv2.minMaxLoc(游戏覆盖)
如果maxValG>=0.9且运行时间>4:
打印('游戏结束于',整数(回合(运行时间)),'秒')
pyautogui.press('space')
运行时间=0
#寻找恐龙
dinoRes=cv2.matchTemplate(屏幕、dino、cv2.TM\u CCOEFF\u规范)
minVal,maxVal,minLoc,maxLoc=cv2.minMaxLoc(迪诺雷斯)
#寻找小仙人掌
smallCactusRes=cv2.matchTemplate(屏幕,smallCactus,cv2.TM\u cFoff\u NORMED)
小仙人掌阈值=0.725
smallCactusLoc=np.其中(smallCactusRes>=SmallCactusThreshold)
#寻找大仙人掌
bigCactusRes=cv2.matchTemplate(屏幕,bigCactus,cv2.TM\u cceff\u NORMED)
BigCactusThreshold=0.725
bigCactusLoc=np.其中(bigCactusRes>=BigCactusThreshold)
#寻找翼手龙
pteroRes=cv2.matchTemplate(屏幕、ptero、cv2.TM\u CCOEFF\u规范)
minValP,maxValP,minLocP,maxLocP=cv2.minMaxLoc(翼龙)
#围绕恐龙的画框
cv2.矩形(屏幕,maxLoc,(maxLoc[0]+w,maxLoc[1]+h),(0,255,0),2)
#小仙人掌
如果smallCactusLoc[0]。大小>0:
leftmostXS=min(smallCactusLoc[1])
leftmostYS=min(smallCactusLoc[0])
distS=(leftmostXS-maxLoc[0])
如果(距离<175且距离>0):
pyautogui.press('space')
cv2.矩形(屏幕,(leftmostXS,leftmostYS),(leftmostXS+scw,leftmostYS+sch),(255,160,0),2)
#大仙人掌
如果bigCactusLoc[0]。大小>0:
leftmostXB=min(bigCactusLoc[1])
leftmostYB=min(bigCactusLoc[0])
distB=(leftmostXB-maxLoc[0])
如果distB<175且distB>0:
pyautogui.press('space')
cv2.矩形(屏幕,(leftmostXB,leftmostYB),(leftmostXB+bcw,leftmostYB+bch),(255,0,0),2)
#避免翼手龙
#选中“maxValP”,否则恐龙会被误认为是翼手龙
#向下箭头需要“keyDown”,否则无法正常工作
如果maxValP>=0.60:
distP=maxLocP[0]-maxLoc[0]
高度p=maxLoc[1]-maxLocP[1]
如果distP<190且distP>0:
如果高度P>10:
keyDown=True
pyautogui.keyDown('down'))
其他:
pyautogui.press('space')
cv2.矩形(屏幕,maxLocP,(maxLocP[0]+pw,maxLocP[1]+ph),(0,025),2)
#elif keyDown==真:
#pyautogui.keyUp('down'))
#keyDown=False
#显示图像
cv2.imshow('Dino Game',cv2.CVT颜色(屏幕,cv2.COLOR_BGR2RGB))
#退出
如果cv2.waitKey(1)&0xFF==27:
cv2.destroyAllWindows()
打破

我是PyAutoGUI的作者。PyAutoGUI有一个“故障保护”功能,可以在脚本有缺陷并且您想关闭它时提供帮助,但它可能会移动鼠标,使其无法敲击键盘。所有PyAutoGUI调用后都有0.1秒的延迟,让您有机会将鼠标猛击到右上角(如果鼠标在坐标(0,0)处,PyAutoGUI将引发
FailSafeException

第十秒的延迟让用户有机会将鼠标移动到左上角。但是,您也可以通过将
pyautogui.PAUSE
设置为
0
来禁用此功能:

>>> pyautogui.PAUSE = 0

但是,这意味着,如果出现问题,您的脚本会导致鼠标不停地点击,那么您可能很难删除脚本。

我认为单击或打字等功能存在一些问题。我发现,如果你在循环中使用它们,你可以得到空鼠标点击或错误写入,例如,如果你想写入1001,你只能得到101。目前正在测试mouseDown和mouseUp等原始函数,它们似乎工作得更好。
希望这对某人有所帮助。

在包中的init.py文件中设置最小睡眠=0.0。它会工作得非常快