def mask_to_mscoco(alpha, annotations, img_id, mode='rle'):
if mode == 'rle':
in_ = np.reshape(np.asfortranarray(alpha), (alpha.shape[0], alpha.shape[1], 1))
in_ = np.asfortranarray(in_)
rle = mask.encode(in_)
segmentation = rle[0]
else:
raise ValueError('Unknown mask mode "{}"'.format(mode))
for idx, c in enumerate(np.unique(alpha)):
area = mask.area(rle).tolist()
if isinstance(area, list):
area = area[0]
bbox = mask.toBbox(rle).tolist()
if isinstance(bbox[0], list):
bbox = bbox[0]
annotation = {
'area': area,
'bbox': bbox,
'category_id': c,
'id': len(annotations)+idx,
'image_id': img_id,
'iscrowd': 0,
'segmentation': segmentation}
annotations.append(annotation)
return annotations
评论列表
文章目录