drov0 / python-imagesearch

A wrapper around opencv2 and pyautogui to do image searching easily.
MIT License
279 stars 98 forks source link

Trouble with imagesearcharea when region x2 and y2 is less than double the size of x1 and y1? #43

Open vil17 opened 3 years ago

vil17 commented 3 years ago

I've got this program with a for loop which runs through a list. However when I put the x1 and y1 cords in at non-zero I start getting problems.

I threw together a gui where I input the region to grab, but as soon as I put in x1 and y1 as something non-zero I start getting issues, different issues depending on values.

400, 400, 400, 400 gives me: ...Programs\Python\Python39\lib\site-packages\mss\windows.py", line 285, in _grab_impl raise ScreenShotError("gdi32.GetDIBits() failed.") mss.exception.ScreenShotError: gdi32.GetDIBits() failed.

200, 200, 400, 400 gives me ...Programs\Python\Python39\lib\site-packages\python_imagesearch\imagesearch.py", line 73, in imagesearcharea img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY) cv2.error: OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-wvn_it83\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

Exception: Line: 0 Callable failed || error occured in a callback

if I put in something like 200, 200, 500, 500 it does run but it doesn't seem to find the images.

def findPicOpt3(sender, data):
    t = Timer(name="random stuff")
    t.start()
    confidence = get_value("inputint4")
    print("searching with confidence level", confidence)
    # im = region_grabber((confidence[0], confidence[1], confidence[2], confidence[3]))
    for num, name in enumerate(imagelist.testImages):
        pos = imagesearcharea(
            imagelist.testImages[num],
            confidence[0],
            confidence[1],
            confidence[2],
            confidence[3],
            #    im,
        )
        if pos[0] != -1:
            print("Found", imagelist.testNames[num], "at", pos)
        else:
            print("image not found")
    t.stop()

I wonder if it has to do with the fact that when I try to debug using im.save it tells me screenshot has no attribute called save: python_imagesearch\imagesearch.py", line 70, in imagesearcharea im.save( AttributeError: 'ScreenShot' object has no attribute 'save' Exception: Line: 0 Callable failed || error occured in a callback

Might I be missing a package or an import? If so I haven't been able to spot which and what I've seen this being reported when people have their path wrong, but it works with regular imagesearch and makes an absolute path

I have triple monitor 1920x1080 setup (one vertical) but I tried disconnecting two of them and this was still a problem. I'm on windows.

is this a bug or should I just give up programming and become a truck driver?

edit: btw lets say I want to grap the middle 800x800 region on a (theoretical) monitor of 1200x1200, I assume I should put in 200, 200, 1000,1000? or 200,200,800,800?.. ive tried both anyway, both don't seem to work.. it's like the region_grabber is accepting widthxheight, and something else is working on topx, topy, bottomx, bottomy? is this possible?

Scruffydrew commented 2 years ago

you managed to figure anything out??? i've been having the same issue

vincenthavinh commented 2 years ago

Could you give the version of imagesearch you are using ?

I got a similar error sometime ago. A bug in region_grabber() function : https://github.com/drov0/python-imagesearch/issues/45 And this function is used in imagesearcharea(), so the bug comes from there.

This bug is present in version 1.2.0 and has been patched in version 1.2.1 But version 1.2.1 has difficulties to autoinstall via pip, I don't really understand why ... see this error : https://github.com/drov0/python-imagesearch/issues/42

So a workaround is to install manually 1.2.1 as explained here, or just to patch region_grabber() function in local package code, as it is done in file python_imagesearch/imagesearch.py in patch 1.2.1

Please do note that reinstalling via pip may wipe those local changes and the problem may reappear. Hopefully one day we'll just be able to get 1.2.1 by pip install.

MarKhan54 commented 1 year ago

However when I put the x1 and y1 cords in at non-zero I start getting problems.

Well, that is half of the answer. The other part is "once program grabs a portion of image, where does it place it on axis?" and the answer is at 0,0, so at the moment imagesearcharea returns coordinates relative to the passed region, which is probably not how it supposed to work (absolute coordinates are expected), and it is really easy to fix, just add x1 and y1 to the result that imagesearcharea returns.