bug: polygon notation from mask
the algorithm that is used to create the polygon notation from a mask sometimes creates single-point polygons. When using pycocotools' annToMask, this throws an exception:
import imantics as imt
import numpy as np
from pycocotools.coco import COCO
import json
mask = [[0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 0, 0], [0, 1, 1, 1, 0, 0], [1, 1, 1, 0, 0, 0], [1, 1, 0, 0, 1, 0], [1, 0, 0, 0, 0, 0]]
mask = np.array(mask, dtype='uint8')
dataset = imt.Dataset('dataset')
catt = imt.Category('dummy', id=1)
maskk = imt.Mask(mask)
img = np.reshape(np.array([mask, mask, mask]), [6, 6, 3])
imgg = imt.Image(img, path='hello/world.png')
dataset.add(imgg)
ann = imt.Annotation.from_mask(maskk, image=imgg, category=catt)
dataset.add(ann)
with open('dummy.json', 'w') as f:
f.write(json.dumps(dataset.coco()))
coco = COCO('dummy.json')
new_mask = coco.annToMask(coco.anns[1])
by rejecting those single(or two)-point polygons, this can be solved:
just edit the polygon- method in the Mask-Class and replace:
polygons = [polygon.flatten() for polygon in polygons]
with
polygons = [polygon.flatten() for polygon in polygons if polygon.shape[0] > 2]