Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I locate something on my screen quickly in Python?

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().

like image 825
Darth Vador Avatar asked Mar 23 '17 10:03

Darth Vador


2 Answers

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:

  • Use grayscaling unless color information is important (grayscale=True is supposed to give 30%-ish speedup)
  • Use a smaller image to locate (like only a part if this is already uniquely identifying the position you need to get)
  • Don't load the image you need to locate from file everytime new but keep it in memory
  • Pass in a region argument if you already know something about the possible locations (e.g. from previous runs)

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.

like image 169
Trilarion Avatar answered Sep 19 '22 01:09

Trilarion


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()
like image 25
user11335084 Avatar answered Sep 18 '22 01:09

user11335084