asweigart / pyscreeze

PyScreeze is a simple, cross-platform screenshot module for Python 2 and 3.
BSD 3-Clause "New" or "Revised" License
194 stars 96 forks source link

Issue with pyscreeze version 0.1.30 - ImageNotFoundException #110

Open hayashi7w opened 1 year ago

hayashi7w commented 1 year ago

Version Information:

pyscreeze version: 0.1.30 (Problematic), 0.1.29 (Problem Solved) Operating System: Windows 10 Python Version: 3.12.0 Detailed Description of the Issue: I encountered an issue with pyscreeze version 0.1.30 where it consistently threw an ImageNotFoundException despite the image being present on the screen. The error message indicated a high confidence level (0.999) in locating the image, but the image could not be found. After downgrading to pyscreeze version 0.1.29, the issue was resolved and the image was correctly identified.

Code Snippet to Reproduce the Issue:

import pyautogui as pg

img_path = "path_to_image.png"
location = pg.locateOnScreen(img_path)
if location:
    pg.click(location)
else:
    print("Image not found.")

Error Message and Stack Trace:

Exception has occurred: ImageNotFoundException Could not locate the image (highest confidence = 0.999) pyscreeze.ImageNotFoundException: Could not locate the image (highest confidence = 0.999)

Additional Context: The same code and image file work perfectly with pyscreeze version 0.1.29. The issue seems specific to version 0.1.30. Any insights or resolutions would be greatly appreciated.

mikigo commented 11 months ago

Is opencv installed? If it is not installed, it is illogical.

hayashi7w commented 11 months ago

Yes, OpenCV is installed.

PieVagabonde commented 10 months ago

I confirm the bug. With version 1.0.29 the code given by hayashi7w works. With version 1.0.30 we get the error message mentioned if the image is not found.

GScrok commented 10 months ago

Yes, I have the same problem, I had to return to the previous version 0.1.29

aentwist commented 6 months ago

Between the lack of git tags and commit message indication, the versioning is unclear. But there aren't that many commits, I checked based on the date and it turns out it's literally the last commit https://github.com/asweigart/pyscreeze/commit/eeca245a135cf171c163b3691300138518efa64e

IanSapp128 commented 2 months ago

I too am experiencing this issue and when trying to use "locateAllOnScreen" with PyAutogui, it hits an exception. Thing is, a try/catch block doesn't catch it. Pyscreeze will still error out.

aentwist commented 2 months ago

I created https://github.com/aentwist/gamedriver to replace all image matching PyScreeze does. There were several important reasons for it, one of which was the interesting behavior around "confidence".

The error message indicated a high confidence level (0.999) in locating the image, but the image could not be found.

What is confidence?

Both our libraries use OpenCV template matching. It is not machine learning, but rather a 2D convolution. There is no confidence involved. There is only a threshold value that determines whether potential matches are "good enough" to be considered matches. In other words how close a match is, not how confident we are either in a match or that we matched. In some roundabout way, the "confidence" here is that threshold value. I cannot tell you exactly what it does after reading the source code. Just understand that increasing the "confidence" makes matching more strict - you'll be able to be very confident that any matches that do occur are correct. But with a "confidence" of 0.999, those matches probably won't occur at all; unless they are extremely precise it will result in a miss. It is no surprise that with such a high number matches are being missed.

As for the actual regression here, anyone can dig through the commit I linked, no further comment on that. Feel free to try out my library for matching purposes.