From 41cb80bcccc7d77cd6af9a477f8cc8b021ab9399 Mon Sep 17 00:00:00 2001 From: Simon Edwardsson Date: Thu, 5 Jan 2023 15:01:22 +0000 Subject: [PATCH 1/2] support multi array segments in coco for imports --- darwin/importer/formats/coco.py | 36 ++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/darwin/importer/formats/coco.py b/darwin/importer/formats/coco.py index 493c28d8a..68f51bf0a 100644 --- a/darwin/importer/formats/coco.py +++ b/darwin/importer/formats/coco.py @@ -89,7 +89,7 @@ def parse_json(path: Path, data: Dict[str, Any]) -> Iterator[dt.AnnotationFile]: annotation["segmentation"] if image_id not in image_annotations: image_annotations[image_id] = [] - image_annotations[image_id].append(parse_annotation(annotation, category_lookup_table)) + image_annotations[image_id].extend(parse_annotation(annotation, category_lookup_table)) for image_id in image_annotations.keys(): image = image_lookup_table[int(image_id)] @@ -99,7 +99,7 @@ def parse_json(path: Path, data: Dict[str, Any]) -> Iterator[dt.AnnotationFile]: yield dt.AnnotationFile(path, filename, annotation_classes, annotations, remote_path=remote_path) -def parse_annotation(annotation: Dict[str, Any], category_lookup_table: Dict[str, Any]) -> Optional[dt.Annotation]: +def parse_annotation(annotation: Dict[str, Any], category_lookup_table: Dict[str, Any]) -> List[dt.Annotation]: """ Parses the given ``json`` dictionary into a darwin ``Annotation`` if possible. @@ -121,14 +121,14 @@ def parse_annotation(annotation: Dict[str, Any], category_lookup_table: Dict[str if iscrowd: print("Warning, unsupported RLE, skipping") - return None + return [] if len(segmentation) == 0 and len(annotation["bbox"]) == 4: x, y, w, h = map(int, annotation["bbox"]) - return dt.make_bounding_box(category["name"], x, y, w, h) + return [dt.make_bounding_box(category["name"], x, y, w, h)] elif len(segmentation) == 0 and len(annotation["bbox"]) == 1 and len(annotation["bbox"][0]) == 4: x, y, w, h = map(int, annotation["bbox"][0]) - return dt.make_bounding_box(category["name"], x, y, w, h) + return [dt.make_bounding_box(category["name"], x, y, w, h)] elif isinstance(segmentation, dict): print("warning, converting complex coco rle mask to polygon, could take some time") if isinstance(segmentation["counts"], list): @@ -152,19 +152,23 @@ def parse_annotation(annotation: Dict[str, Any], category_lookup_table: Dict[str except StopIteration: break paths.append(path) - return dt.make_complex_polygon(category["name"], paths) + return [dt.make_complex_polygon(category["name"], paths)] elif isinstance(segmentation, list): - path = [] - points = iter(segmentation[0] if isinstance(segmentation[0], list) else segmentation) - while True: - try: - x, y = next(points), next(points) - path.append({"x": x, "y": y}) - except StopIteration: - break - return dt.make_polygon(category["name"], path) + paths = segmentation if isinstance(segmentation[0], list) else [segmentation] + polygons = [] + for path in paths: + point_path = [] + points = iter(paths) + while True: + try: + x, y = next(points), next(points) + point_path.append({"x": x, "y": y}) + except StopIteration: + break + polygons.append(dt.make_polygon(category["name"], point_path)) + return polygons else: - return None + return [] def _decode_file(current_encoding: str, path: Path): From 3ae7e4c5ccd89f91be59b44c88fd4c925045e336 Mon Sep 17 00:00:00 2001 From: Simon Edwardsson Date: Fri, 6 Jan 2023 09:16:32 +0000 Subject: [PATCH 2/2] fix typo --- darwin/importer/formats/coco.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/darwin/importer/formats/coco.py b/darwin/importer/formats/coco.py index 68f51bf0a..2f47f9fff 100644 --- a/darwin/importer/formats/coco.py +++ b/darwin/importer/formats/coco.py @@ -158,7 +158,7 @@ def parse_annotation(annotation: Dict[str, Any], category_lookup_table: Dict[str polygons = [] for path in paths: point_path = [] - points = iter(paths) + points = iter(path) while True: try: x, y = next(points), next(points)