I'm trying to use template matching to detect when the input from my screen no longer matches the template. The code below puts a rectangle around the area I want to detect a change in. Is there any way I can, lets say, print some text when that rectangle is no longer there?
import numpy as np
import cv2
from PIL import ImageGrab as ig
import pyautogui as gui
import glob
template_data=[]
files1 = glob.glob("C:\\Users\\aaaa\\template images\\template*.jpg")
for myfile in files1:
image = cv2.imread(myfile,0)
template_data.append(image)
while(True):
source = ig.grab(bbox=(700,400,1200,700))
screen_colour = cv2.cvtColor(np.array(source), cv2.COLOR_BGR2RGB)
screen = cv2.cvtColor(np.array(screen_colour), cv2.COLOR_RGB2GRAY)
for tmp in template_data:
(h, w) = tmp.shape[:2]
result = cv2.matchTemplate(np.array(screen), tmp, cv2.TM_CCOEFF_NORMED)
threshhold = 0.92
loc = np.where(result >= threshhold)
for pt in zip(*loc[::-1]):
cv2.rectangle(screen, pt, (pt[0]+w, pt[1]+h), (0,255,255),2)
print("yes")
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
cv2.imshow('Result',np.array(screen))