Trying to template match on screen

Viewed 126

i am currently making a object finder on screen but this is the thing my code seems to not work i get error AttributeError: 'numpy.ndarray' object has no attribute 'read'

My code for the object finder:

from PIL import ImageGrab
import cv2
import time
import numpy as np


def find(file):
    while True:
        Screen = np.array(ImageGrab.grab(bbox=(13, 32,805, 623)))
        gray = cv2.cvtColor(Screen, cv2.COLOR_BGR2GRAY)
        
    
    
        img_rgb = Screen.read()
        img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
        template = cv2.imread(file,0)
        w, h = template.shape[::-1]
        
        res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
        threshold = 0.8
        loc = np.where( res >= threshold)
        for pt in zip(*loc[::-1]):
            cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
        
        
        
        k = cv2.waitKey(30) & 0xff
    
        if k == 27:
            cv2.destroyAllWindows()
            break
        
        
        cv2.imshow('Frame', gray)

find('13.png')

is there any reason why this is happening?

1 Answers

I'm not sure what you are trying to accomplish with Screen.read, but it doesn't seem necessary. You can pass Screen directly into cv2.cvtColor.

img_gray = cv2.cvtColor(Screen, cv2.COLOR_BGR2GRAY)

A numpy array doesn't have the method (or attribute) read as the error mentions.

Related