I have a pretty basic template match function that takes a global variable, “path”, as input.
def operation(self):
searchIn = cv2.imread(path, cv2.IMREAD_UNCHANGED)
searchFor = cv2.imread('needle.png', cv2.IMREAD_UNCHANGED)
result = cv2.matchTemplate(searchIn, searchFor, cv2.TM_CCOEFF_NORMED)
cv2.imshow('Result', result)
cv2.waitKey()
cv2.destroyAllWindows()
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
print(max_loc)
print(max_val)
if max_val > 0.9:
print("Found it")
However, I want this function to take video input instead of just an image and I’m struggling with the implementation. I have tried something but keep getting an error message (see end of post for details). I don’t really know what I’m doing so if anyone could link me to some resources or explain how to do this, it would be really appreciated!
def operation(self):
cap = cv2.VideoCapture(path)
searchFor = cv2.imread('sampleIMG1.png', cv2.IMREAD_UNCHANGED)
while cap.isOpened():
ret, frame = cap.read()
if not ret:
print("Can't receive frame (stream end?). Exiting ...")
break
result = cv2.matchTemplate(cap, searchFor, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
print(max_loc)
print(max_val)
if max_val > 0.9:
print("Found it")
if cv.waitKey(1) == ord('q'):
break
cap.release()
cv.destroyAllWindows()
Here is the error message which I get:
File "main.py", line 75, in operation
result = cv2.matchTemplate(cap, searchFor, cv2.TM_CCOEFF_NORMED)
cv2.error: OpenCV(4.5.3) :-1: error: (-5:Bad argument) in function 'matchTemplate'
> Overload resolution failed:
> - image is not a numpy array, neither a scalar
> - Expected Ptr<cv::UMat> for argument 'image'