首页 > 解决方案 > Python - 分配错误之前引用的局部变量

问题描述

我正在尝试对我的数据集采用 mask_rcnn 原始代码。原始代码采用 xml 文件,我只是修改了我的代码以接受 json 格式。这是代码


   def load_mask(self, image_id, xml=False):
        # get details of image
        info = self.image_info[image_id]

        if xml:
            # define anntation  file location
            path = info['annotation']
            # load XML
            boxes, w, h = self.extract_boxes(path, xml=True)
        else:
            with open('D:\Mask_RCNN\Mask_RCNN\dataset\\annots\\annotations.json', 'r') as f:
                annots = json.load(f)
                found = False
                for value in annots.values():
                    if 'image' in value and value['instance_list']:
                        if value['image']['original_filename'][:-3] == info['id']:
                            boxes, w, h = self.extract_boxes(value)
                            found = True
                            break

        if found:
            # create one array for all masks, each on a different channel
            masks = zeros([h, w, len(boxes)], dtype='uint8')
        else:
            stop = "here"

                if found:
            # create one array for all masks, each on a different channel
            masks = zeros([h, w, len(boxes)], dtype='uint8')
        else:
            stop = "here"

        # create masks
        class_ids = list()
        for i in range(len(boxes)):
            box = boxes[i]
            row_s, row_e = box[1], box[3]
            col_s, col_e = box[0], box[2]
            masks[row_s:row_e, col_s:col_e, i] = 1
            class_ids.append(self.class_names.index('Penquins'))
        return masks, asarray(class_ids, dtype='int32')

    # load an image reference
        #"""Return the path of the image."""
    def image_reference(self, image_id):
            info = self.image_info[image_id]
            print(info)
            return info['path']

它给了我错误

文件“C:/PycharmProjects/Mask_RCNN/Mask_RCNN/objects.py”,第 197 行,在 load_mask
for i in range(len(boxes)) 中:UnboundLocalError:分配前引用的局部变量 'boxes'

我试图调试代码,它在创建掩码之前抛出了一个错误,但我无法找出问题所在。任何想法?

标签: pythonopencvdeep-learningopencv3.0faster-rcnn

解决方案


只有当xml它为真时,才能保证盒子得到一个值。否则必须满足附加条件,但似乎并非如此。


推荐阅读