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

Update coco.py to support complex polygons #570

Open
wants to merge 1 commit 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
25 changes: 17 additions & 8 deletions darwin/importer/formats/coco.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,6 @@ def parse_annotation(annotation: Dict[str, Any], category_lookup_table: Dict[str
"""
category = category_lookup_table[annotation["category_id"]]
segmentation = annotation["segmentation"]
iscrowd = annotation.get("iscrowd") == 1

if iscrowd:
print("Warning, unsupported RLE, skipping")
return None

if len(segmentation) == 0 and len(annotation["bbox"]) == 4:
x, y, w, h = map(int, annotation["bbox"])
Expand All @@ -140,18 +135,32 @@ def parse_annotation(annotation: Dict[str, Any], category_lookup_table: Dict[str
_labels, external, _internal = find_contours(mask)
paths = []
for external_path in external:
new_path = []
# skip paths with less than 2 points
if len(external_path) // 2 <= 2:
continue
path = []
points = iter(external_path)
while True:
try:
x, y = next(points), next(points)
path.append({"x": x, "y": y})
new_path.append({"x": x, "y": y})
except StopIteration:
break
paths.append(new_path)

for internal_path in _internal:
new_path = []
# skip paths with less than 2 points
if len(internal_path) // 2 <= 2:
continue
points = iter(internal_path)
while True:
try:
x, y = next(points), next(points)
new_path.append({"x": x, "y": y})
except StopIteration:
break
paths.append(path)
paths.append(new_path)
return dt.make_complex_polygon(category["name"], paths)
elif isinstance(segmentation, list):
path = []
Expand Down