2021-07-30

Keep getting an error w, h = small_image.shape[:-1] AttributeError: 'NoneType' object has no attribute 'shape'

I have very little knowledge and am trying to learn python, and have been doing pretty well.

Until!

When trying to match the image's, I get w, h = small_image.shape[:2]

AttributeError: 'NoneType' object has no attribute 'shape'

I have the image in the correct path, but keep receiving the error above, have debugged it many times and searched all over the internet for a answer.

The only answer I found was check the path's which i have and they seem to be correct.

Please excuse how little knowledge I have. I am just trying to learn any pointers would be appreciated.

def __init__(self, handle=None):
        self._handle = handle
        self._item_memory = {}
        self._item_area = (525, 227, 80, 275)

def match_image(self, largeImg, smallImg, threshold=0.1, debug=False):
      
        method = cv2.TM_SQDIFF_NORMED

        # Read the images from the file
        small_image = cv2.imread(smallImg)
        large_image = cv2.imread(largeImg)
        w, h = small_image.shape[:2]

        result = cv2.matchTemplate(small_image, large_image, method)

        # We want the minimum squared difference
        mn, _, mnLoc, _ = cv2.minMaxLoc(result)

        if (mn >= threshold):
            return False

        # Extract the coordinates of our best match
        x, y = mnLoc

        if debug:
            # Draw the rectangle:
            # Get the size of the template. This is the same size as the match.
            trows, tcols = small_image.shape[:2]

            # Draw the rectangle on large_image
            cv2.rectangle(large_image, (x, y),
                          (x+tcols, y+trows), (0, 0, 255), 2)

            # Display the original image with the rectangle around the match.
            cv2.imshow('output', large_image)

            # The image is only displayed if we call this
            cv2.waitKey(0)

        # Return coordinates to center of match
        return (x + (w * 0.5), y + (h * 0.5))`

    def find_item(self, item_name, threshold=0.15, max_tries=1, recapture=True):

        self.set_active()
        tries = 0
        res = False
        while not res and tries < max_tries:
            tries += 1

            if tries > 1:
                # Wait 1 second before re-trying
                self.wait(0.5)
                recapture = True

            if recapture:
                self.mouse_out_of_area(self._item_area)
                self.screenshot('item_area.png', region=self._item_area)

            res = self.match_image(
                'item_area.png', ('items/' + item_name + '.png'), threshold)

        if res is not False:
            x, y = res
            offset_x, offset_y = self._item_area[:2]
            item_pos = (offset_x + x, offset_y + y)
            # Remember location
            self._item_memory[item_name] = item_pos
            return item_pos
        else:
            return False


from Recent Questions - Stack Overflow https://ift.tt/3ibPbdt
https://ift.tt/eA8V8J

No comments:

Post a Comment