I'm new to python and I'm trying to follow a tutorial on object detection, but my code calling find (which calls cv.matchTemplate) this error:
import cv2 as cv
import numpy as np
from time import time
from random import randint
from windowcapture import WindowCapture
from vision import Vision
#points = get_click_points('img2.jpg', 'copper.jpg', threshold=0.55, debug_mode=True)
wincap = WindowCapture('OSBuddy - takeawayeoc')
vision_ore = Vision('orefixed.png')
loop_time = time()
while (True):
screenshot = wincap.capture_window()
#cv.imshow('Computer Vision', screenshot)
vision_ore.find(screenshot, 0.6, 'rectangles')
print('FPS{}'.format(1 / (time() - loop_time)))
loop_time = time()
if(cv.waitKey(1) == ord('q')):
cv.destroyAllWindows()
break
File "c:\Users\Daniel\Documents\Projects\bottests\vision.py", line 28, in find
result = cv.matchTemplate(haystack_img, self.needle_img, self.method)
cv2.error: OpenCV(4.4.0)
C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-h4wtvo23\opencv\modules\imgproc\src\templmatch.cpp:1163: error: (-215:Assertion failed)
(depth == CV_8U || depth == CV_32F) && type == _templ.type() && _img.dims() <= 2 in function 'cv::matchTemplate'
you can see links to the classes in question here (code for find function) and here (window capture).
EDIT: I'm assuming the reason this isn't working is also because a simple call to print(haystack_img) and print(self.needle_img) prints different size arrays, as well as my haystack_img prints to all 0. ie:
print(haystack_img)
...
[0 0 0]
[0 0 0]
[0 0 0]]]
print(self.needle_img)
...
[ 40 76 87 255]
[ 40 76 87 255]
[ 40 76 87 255]]]
This is the code for the window capture function:
def capture_window(self):
#hwnd = None
#get the window image data
wDC = win32gui.GetWindowDC(self.hwnd)
dcObj = win32ui.CreateDCFromHandle(wDC)
cDC = dcObj.CreateCompatibleDC()
dataBitMap = win32ui.CreateBitmap()
dataBitMap.CreateCompatibleBitmap(dcObj, self.w, self.h)
cDC.SelectObject(dataBitMap)
cDC.BitBlt((0, 0), (self.w, self.h), dcObj, (0,0), win32con.SRCCOPY)
# convert the raw data into a format opencv can read
#dataBitMap.SaveBitmapFile(cDC, 'debug.bmp')
signedIntsArray = dataBitMap.GetBitmapBits(True)
img = np.fromstring(signedIntsArray, dtype='uint8')
img.shape = (self.h, self.w, 4)
# free resources
dcObj.DeleteDC()
cDC.DeleteDC()
win32gui.ReleaseDC(self.hwnd, wDC)
win32gui.DeleteObject(dataBitMap.GetHandle())
# drop the alpha channel
img = img[:,:,:3]
# make image C_CONTIGUOUS
img = np.ascontiguousarray(img)
print('IMG DIMS: ', img.shape) #returns 'IMG DIMS: (28, 160, 3) then my error
return img
Am I on the right track? How can I make matchTemplate read my screenshots from a video feed?