[英]Python - Local variable referenced before assignment error
我正在尝试将 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”在赋值前被引用
我试图调试代码,它在创建掩码之前抛出错误,但我无法弄清楚出了什么问题。 任何的想法?
只有当xml
为真时,才能保证 box 获得一个值。 否则必须满足附加条件,这似乎并非如此。
在load_mask
的第一个if else
块中的一些场景中, boxes
被初始化,这意味着当你到达if found:
时它可能不存在。 一开始就声明
def load_mask(self, image_id, xml=False):
# get details of image
info = self.image_info[image_id]
boxes = []
这意味着您正在尝试遍历boxes
的数量,但是上面的 if 语句的构造方式使得在执行此语句时boxes
可能不会退出。
试试这个简单的例子:
user_input = "no"
if user_input =="yes":
boxes = [1,2,3,4]
else:
pass
print(boxes)
这将导致与您所看到的相同的错误。 您的问题是这两个 if 语句都必须在定义boxes
之前通过:
if 'image' in value and value['instance_list']:
if value['image']['original_filename'][:-3] == info['id']:
您可能需要额外的逻辑来确保仅在满足某些条件时才会进行迭代。 或者,如果boxes
是一个列表,您可以通过定义boxes=[]
来开始您的代码。 在这种情况下,迭代将简单地跳过而不做任何事情,因为在这种情况下boxes
的长度为零
问题是在此 for 循环的行中未声明框,因此在函数的第一部分中将框定义为空列表
def load_mask(self, image_id, xml=False):
boxes = []
#do all the other stuff here
然后当你执行 for 循环时,它现在应该被声明(除非其中一个函数不返回框并给你其他东西)
执行 for 循环时的快速提示,您可以执行
for i, box in enumerate(boxes):
...
这不会让你在循环内定义框(我将是 0 到 len(boxes)-1 并且框将是具有该索引的框)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.