lycandown
lycandown

Reputation: 1

python Opencv template match error with win32api

So i have been trying to use this code to get a screenshot from a window, with this screenshot i will use opencv to match template, but every time that i try to match template i always get the same error, i have tried with a few other things like image grab from pillow, pyautogui and i am now trying with windowsAPI. i will say in advance this code is not mine.

Can anyone help me with this?

the error:

test = cv.matchTemplate(screenshot, target, cv.TM_CCORR_NORMED)
cv2.error: OpenCV(4.6.0) :-1: error: (-5:Bad argument) in function 'matchTemplate'
> Overload resolution failed:
>  - templ is not a numpy array, neither a scalar
>  - Expected Ptr<cv::UMat> for argument 'templ'

the source code:

import cv2 as cv
import numpy as np
import pyautogui
from time import time
import win32gui, win32ui, win32con
from PIL import ImageGrab
game = 'LOST ARK (64-bit, DX9) v.2.5.3.1'
target = 'box.png'



def list_window_names():
        def winEnumHandler(hwnd, ctx):
            if win32gui.IsWindowVisible(hwnd):
                print(hex(hwnd), win32gui.GetWindowText(hwnd))
        win32gui.EnumWindows(winEnumHandler, None)

def windowcapture():
    hwnd = win32gui.FindWindow(None, game)
    rect = win32gui.GetWindowRect(hwnd)
    w = rect[2] - rect[0]
    h = rect[3] - rect[1]
    #hwnd = win32gui.FindWindow(None, windowname)
    wDc = win32gui.GetWindowDC(hwnd)
    dcObj = win32ui.CreateDCFromHandle(wDc)
    cDC = dcObj.CreateCompatibleDC()
    dataBitMap = win32ui.CreateBitmap()
    dataBitMap.CreateCompatibleBitmap(dcObj, w, h)
    cDC.SelectObject(dataBitMap)
    cDC.BitBlt((0, 0), (w, h), dcObj, (0, 0), win32con.SRCCOPY)


    dataBitMap.SaveBitmapFile(cDC, 'debug.bmp')
    signedIntsArray = dataBitMap.GetBitmapBits(True)
    img = np.fromstring(signedIntsArray, dtype='uint8')
    img.shape = (h, w, 4)

    dcObj.DeleteDC()
    cDC.DeleteDC()
    win32gui.ReleaseDC(hwnd, wDc)
    win32gui.DeleteObject(dataBitMap.GetHandle())

    return img


list_window_names()
loop_time = time()
while True:
    #screenshot = ImageGrab.grab(bbox = (w,h,x,y))
    #screenshot = pyautogui.screenshot()
    #screenshot = np.array(screenshot)
    #screenshot = cv.cvtColor(screenshot, cv.COLOR_RGB2BGR)
    #print('top:{} , left:{}, w:{} ,h: {}'.format(w,h,x,y))
    #screenshot.show()

    #screenshot = np.array(screenshot)

    #screenshot = screenshot[:, :, ::-1].copy()
    #screenshot = screenshot[..., :3].copy()
    screenshot = windowcapture()
    cv.imshow("computer vision",screenshot)
    method = cv.TM_CCORR
    result = cv.matchTemplate(screenshot, target, method)

    # debug the loop rate
    print('FPS {}'.format(1 / (time() - loop_time)))
    loop_time = time()
    if cv.waitKey(1) == ord('q'):
        cv.destroyAllWindows()
        break

print('Done.')

Upvotes: 0

Views: 212

Answers (0)

Related Questions