From e9f1db00db0f4cb884e4d8293a63e708766d5f95 Mon Sep 17 00:00:00 2001 From: zhiltsov-max Date: Wed, 18 Dec 2019 19:04:33 +0300 Subject: [PATCH] Add polygon point count checks (#975) --- cvat/apps/annotation/coco.py | 8 ++++++++ datumaro/datumaro/components/extractor.py | 2 ++ 2 files changed, 10 insertions(+) diff --git a/cvat/apps/annotation/coco.py b/cvat/apps/annotation/coco.py index ec80b399..5e41b067 100644 --- a/cvat/apps/annotation/coco.py +++ b/cvat/apps/annotation/coco.py @@ -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, diff --git a/datumaro/datumaro/components/extractor.py b/datumaro/datumaro/components/extractor.py index 31d41644..a91f3c4f 100644 --- a/datumaro/datumaro/components/extractor.py +++ b/datumaro/datumaro/components/extractor.py @@ -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)