不断收到错误 w, h = small_image.shape[:-1] AttributeError: 'NoneType' 对象没有属性 'shape'

如何解决不断收到错误 w, h = small_image.shape[:-1] AttributeError: 'NoneType' 对象没有属性 'shape'

我的知识很少,正在尝试学习 Python,并且一直做得很好。

直到!

当试图匹配图像时,我得到 w,h = small_image.shape[:2]

AttributeError: 'nonetype' 对象没有属性 'shape'

我的图片在正确的路径中,但一直收到上面的错误,已经调试了很多次并在互联网上搜索了答案。

我找到的唯一答案是检查我拥有的路径,它们似乎是正确的。

请原谅我所知甚少。我只是想学习任何指针,将不胜感激。

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,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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?