Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[V7-3952] Support multi array coco segmentation import #511

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions darwin/importer/formats/coco.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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.

Expand All @@ -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):
Expand All @@ -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(path)
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):
Expand Down