I've tried using the pyautogui module and i's function that locates an image on the screen
pyautogui.locateOnScreen()
but it's processing time is about 5-10 seconds. Is there any other way for me to locate an image on the screen more quickly? Basically, I want a faster version of locateOnScreen().
The official documentation says it should take 1-2 seconds on a 1920x1080 screen, so your time seems to be a bit slow. I would try to optimize:
grayscale=True
is supposed to give 30%-ish speedup)This is all described in the documentation linked above.
Is this is still not fast enough you can check the sources of pyautogui, see that locate on screen uses a specific algorithm (Knuth-Morris-Pratt search algorithm) implemented in Python. So implementing this part in C, may result in quite a pronounced speedup.
make a function and use threading confidence (requires opencv)
import pyautogui
import threading
def locate_cat():
cat=None
while cat is None:
cat = pyautogui.locateOnScreen('Pictures/cat.png',confidence=.65,region=(1722,748, 200,450)
return cat
you can use the region argument if you know the rough location of where it is on screen
there may be some instances where you can locate on screen and assign the region to a variable and use region=somevar as an argument so it looks in the same place it found it last time to help speed up the detection process.
eg:
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With