Add polygon point count checks (#975)

main
zhiltsov-max 6 years ago committed by Nikita Manovich
parent 4de8be7b69
commit e9f1db00db

@ -247,6 +247,10 @@ def dump(file_object, annotations):
new_anno['image_id'] = image.frame
new_anno['iscrowd'] = 0
new_anno['segmentation'] = obj['points']
if len(obj['points'][0]) < 6:
raise Exception("Unable to export frame #{}: "
"a polygon has too few points ({})".format(
image.frame, len(obj['points'][0])))
area, bbox = polygon_area_and_bbox(obj['points'], image.height, image.width)
new_anno['area'] = float(np.sum(area))
new_anno['bbox'] = bbox
@ -367,6 +371,10 @@ def load(file_object, annotations):
group = group_idx
for polygon in polygons:
if len(polygon) < 6:
raise Exception("Unable to import annotation #{}: "
"a polygon has too few points ({})".format(
ann['id'], len(polygon)))
annotations.add_shape(annotations.LabeledShape(
type='polygon',
frame=frame_number,

@ -276,6 +276,8 @@ class PolygonObject(ShapeObject):
# pylint: disable=redefined-builtin
def __init__(self, points=None,
label=None, id=None, attributes=None, group=None):
if points is not None:
assert len(points) % 2 == 0 and 3 <= len(points) // 2, "Wrong polygon points: %s" % points
super().__init__(type=AnnotationType.polygon,
points=points, label=label,
id=id, attributes=attributes, group=group)

Loading…
Cancel
Save