import pyautogui
def first_find():
front_door = None
while front_door is None:
front_door_save=pyautogui.locateOnScreen('frontdoor.png',confidence=.95,region=1722,748, 200,450)
front_door=front_door_save
return front_door_save
def second_find():
front_door=None
while front_door is None:
front_door = pyautogui.locateOnScreen('frontdoor.png',confidence=.95,region=front_door_save)
return front_door
def find_person():
person=None
while person is None:
person= pyautogui.locateOnScreen('person.png',confidence=.95,region=front_door)
while True:
first_find()
second_find()
if front_door is None:
pass
if front_door is not None:
find_person()
import pyautogui as pg
import numpy as np
import cv2 as cv
from PIL import ImageGrab, Image
import time
REGION = (0, 0, 400, 400)
GAME_OVER_PICTURE_PIL = Image.open("./balloon_fight_game_over.png")
GAME_OVER_PICTURE_CV = cv.imread('./balloon_fight_game_over.png')
def timing(f):
def wrap(*args, **kwargs):
time1 = time.time()
ret = f(*args, **kwargs)
time2 = time.time()
print('{:s} function took {:.3f} ms'.format(
f.__name__, (time2-time1)*1000.0))
return ret
return wrap
@timing
def benchmark_pyautogui():
res = pg.locateOnScreen(GAME_OVER_PICTURE_PIL,
grayscale=True, # should provied a speed up
confidence=0.8,
region=REGION)
return res is not None
@timing
def benchmark_opencv_pil(method):
img = ImageGrab.grab(bbox=REGION)
img_cv = cv.cvtColor(np.array(img), cv.COLOR_RGB2BGR)
res = cv.matchTemplate(img_cv, GAME_OVER_PICTURE_CV, method)
# print(res)
return (res >= 0.8).any()
if __name__ == "__main__":
im_pyautogui = benchmark_pyautogui()
print(im_pyautogui)
methods = ['cv.TM_CCOEFF', 'cv.TM_CCOEFF_NORMED', 'cv.TM_CCORR',
'cv.TM_CCORR_NORMED', 'cv.TM_SQDIFF', 'cv.TM_SQDIFF_NORMED']
# cv.TM_CCOEFF_NORMED actually seems to be the most relevant method
for method in methods:
print(method)
im_opencv = benchmark_opencv_pil(eval(method))
print(im_opencv)
型 结果显示出X10的改进。
benchmark_pyautogui function took 175.712 ms
False
cv.TM_CCOEFF
benchmark_opencv_pil function took 21.283 ms
True
cv.TM_CCOEFF_NORMED
benchmark_opencv_pil function took 23.377 ms
False
cv.TM_CCORR
benchmark_opencv_pil function took 20.465 ms
True
cv.TM_CCORR_NORMED
benchmark_opencv_pil function took 25.347 ms
False
cv.TM_SQDIFF
benchmark_opencv_pil function took 23.799 ms
True
cv.TM_SQDIFF_NORMED
benchmark_opencv_pil function took 22.882 ms
True
4条答案
按热度按时间vaqhlq811#
官方文档说在1920 x1080屏幕上应该需要1-2秒。你的时间似乎有点慢。我会尝试通过以下方式优化代码:
grayscale=True
应该可以提高大约30%的速度)。这一切都在上面链接的文档中描述。
如果这仍然不够快,你可以检查sources of pyautogui,看到'locate on screen'使用了Python中实现的特定算法(Knuth-Morris-Pratt搜索算法)。在C中实现这一部分可能会导致相当明显的加速。
xzv2uavs2#
make a function and use threading confidence(需要opencv)
字符串
如果知道区域在屏幕上的大致位置,则可以使用区域参数
在某些情况下,您可以在屏幕上找到区域并将区域分配给变量,并使用region=somevar作为参数,使其看起来与上次找到的位置相同,以帮助加快检测过程。
例如:
型
bt1cpqcv3#
我在pyautogui上遇到了同样的问题。虽然这是一个非常方便的图书馆,但它很慢。
我依靠cv2和PIL获得了x10的加速:
字符串
其中使用TM_CCOEFF_NORMED效果良好。(显然,您也可以调整0.8的阈值)
来源:Fast locateOnScreen with Python
为了完整起见,下面是完整的基准测试:
型
结果显示出X10的改进。
型
3pvhb19x4#
如果您正在寻找图像识别,您可以使用Sikuli。检查Hello World tutorial。