python - 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'
我试图调试代码,它在创建掩码之前抛出了一个错误,但我无法找出问题所在。任何想法?
解决方案
只有当xml
它为真时,才能保证盒子得到一个值。否则必须满足附加条件,但似乎并非如此。
推荐阅读
- javascript - Javascript / Jquery 从部分选择描述符中提取整个 CLASS 描述符
- javascript - 附加标签未出现在图表 d3.js 中
- python - 检查元素是否已经在列表中
- python - 针对不同平台交叉编译python库
- nuxt.js - Cloudinary Nuxt 模块:未知元素 cld-image
- javascript - 如何防止机器人向 API 发送垃圾邮件试图攻击易受攻击的端点
- javascript - 使用 lodash 删除子元素
- game-maker-studio-2 - 如何在 GameMaker Studio 2 代码中反转精灵位置?
- javascript - 为什么jQuery点击滚动没有添加适当的活动类?
- python - 排序python元组包含不可比较的元素