asweigart / pyautogui

A cross-platform GUI automation Python module for human beings. Used to programmatically control the mouse & keyboard.
BSD 3-Clause "New" or "Revised" License
10.22k stars 1.24k forks source link

logging confidence values for locateOnScreen #707

Open Alpha-Rome0 opened 2 years ago

Alpha-Rome0 commented 2 years ago

for the locateOnScreen function, is there anyway to log the confidence values of what it finds?

Headshot56 commented 2 years ago

thats not how confidence works. If you want to change your confidence value the best way is to add it to your locate command as an argument. pyautogui.locateOnScreen("image.jpg", confidence=0.8) The lower you set the decimal the less precise it has to be. I hope this helped!

renato-bosa commented 1 year ago

for the locateOnScreen function, is there anyway to log the confidence values of what it finds?

Well, interesting question @Alpha-Rome0 !

I would try something like this:

# Creates a empty list to add the confidence log.
confidences_log = list()

# Creates a function to evaluate confidence level.
def locate_dynamic_confidence(image):
    confidence_var = 0.9
    while(confidence_var > 0):
        try:
            pyautogui.locateOnScreen(image, confidence=confidence_var)
            return f"Image: {image} (confidence > {confidence_var})"
        except ImageNotFoundException:
            confidence_var = confidence_var - 0.1
    return f"Image: {image} (Not found!)"

# Runs the function and get the result.
result = locate_dynamic_confidence("image.jpg")

# Append result to the log.
confidences_log.append(result)

Ps: I did not try to run this, so it can have minor mistake(s).

Ps2: As of version 0.9.41, if the locate functions can’t find the provided image, they’ll raise ImageNotFoundException. On previous versions it returns None, so it should need some minor changes to work on previous versions.