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

[IO-1607][internal] Hotfix for Yolov8 #649

Merged
merged 3 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
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
41 changes: 30 additions & 11 deletions darwin/exporter/formats/yolo_segmented.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from pathlib import Path
from typing import Iterable, List

from darwin.datatypes import AnnotationFile, VideoAnnotation
from darwin.datatypes import Annotation, AnnotationFile, JSONType, VideoAnnotation
from darwin.exceptions import DarwinException
from darwin.exporter.formats.helpers.yolo_class_builder import (
ClassIndex,
build_class_index,
Expand Down Expand Up @@ -78,14 +79,25 @@ class YoloSegmentedAnnotationType(Enum):
POLYGON = auto()


def _determine_annotation_type(data: dict, annotation_index: int) -> YoloSegmentedAnnotationType:
if "x" in data and "y" in data and "w" in data and "h" in data:
return YoloSegmentedAnnotationType.BOUNDING_BOX
elif "points" in data:
if isinstance(data["points"][0], list):
logger.warn(f"Skipped annotation at index {annotation_index} because it's a complex polygon'")
return YoloSegmentedAnnotationType.UNKNOWN
def _determine_annotation_type(annotation: Annotation) -> YoloSegmentedAnnotationType:
"""
Determines the annotation type

Parameters
----------
annotation : Annotation
The annotation to be determined.

Returns
-------
YoloSegmentedAnnotationType
The annotation type.
"""
type = annotation.annotation_class.annotation_type

if type == "bounding_box":
return YoloSegmentedAnnotationType.BOUNDING_BOX
elif type == "polygon":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is complex_polygon/circle/other annotation types not applicable to YOLO?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Largely speaking, Yolo has historically been bounding boxes, and now supports polygons too, but ellipses, points, and other types are not supported - with many (2D plane) types, we could still process the bounding box and include it, but this wasn't in scope of the feature, and it was decided to include only bbox and simple polys for now.

return YoloSegmentedAnnotationType.POLYGON
else:
return YoloSegmentedAnnotationType.UNKNOWN
Expand Down Expand Up @@ -159,7 +171,14 @@ def _handle_polygon(data: dict, im_w: int, im_h: int, annotation_index: int, poi

last_point = None
try:
for point_index, point in enumerate(data["points"]):
if "path" in data:
path_data = data["path"]
elif "points" in data:
# continuing to support old version, in case anything relies on it.
path_data = data["points"]
else:
raise DarwinException from ValueError("No path data found in annotation.")
for point_index, point in enumerate(path_data):
last_point = point_index
x = point["x"] / im_w
y = point["y"] / im_h
Expand Down Expand Up @@ -227,7 +246,7 @@ def _build_text(annotation_file: AnnotationFile, class_index: ClassIndex) -> str

# Process annotations

annotation_type = _determine_annotation_type(annotation.data, annotation_index)
annotation_type = _determine_annotation_type(annotation)
if annotation_type == YoloSegmentedAnnotationType.UNKNOWN:
continue

Expand Down Expand Up @@ -259,4 +278,4 @@ def _build_text(annotation_file: AnnotationFile, class_index: ClassIndex) -> str
# Create the line for the annotation
yolo_line = f"{class_index[annotation.annotation_class.name]} {' '.join([f'{p.x} {p.y}' for p in points])}"
yolo_lines.append(yolo_line)
return "\n".join(yolo_lines)
return "\n".join(yolo_lines) + "\n"
22 changes: 22 additions & 0 deletions tests/darwin/exporter/formats/export_yolo_segmented_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def annotation_classes() -> List[AnnotationClass]:
return [
AnnotationClass(name="class1", annotation_type="bounding_box"),
AnnotationClass(name="class2", annotation_type="polygon"),
AnnotationClass(name="class3", annotation_type="polygon"),
]


Expand All @@ -35,6 +36,25 @@ def annotations(annotation_classes: List[AnnotationClass]) -> List[Annotation]:
),
Annotation(
annotation_class=annotation_classes[1],
data={
# Polygon
"path": [
{ "x": 0, "y": 0, },
{ "x": 0, "y": 100, },
{ "x": 50, "y": 150, },
{ "x": 100, "y": 100, },
{ "x": 0, "y": 100, },
{ "x": 0, "y": 0 },
]
},
subs=[],
slot_names=[
"1",
],
),
# Unexpected case we should still handle
Annotation(
annotation_class=annotation_classes[2],
data={
# Polygon
"points": [
Expand Down Expand Up @@ -80,6 +100,8 @@ def test_export_yolo_segmented(annotation_files: List[AnnotationFile], tmp_path:
if CLOSE_VERTICES:
assert output_lines[0] == "0 0.02 0.03 0.27 0.16 0.27 0.03 0.02 0.16 0.02 0.03"
assert output_lines[1] == "1 0.0 0.0 0.0 0.1 0.05 0.15 0.1 0.1 0.0 0.1 0.0 0.0 0.0 0.0"
assert output_lines[2] == "2 0.0 0.0 0.0 0.1 0.05 0.15 0.1 0.1 0.0 0.1 0.0 0.0 0.0 0.0"
else:
assert output_lines[0] == "0 0.02 0.03 0.27 0.16 0.27 0.03 0.02 0.16"
assert output_lines[1] == "1 0.0 0.0 0.0 0.1 0.05 0.15 0.1 0.1 0.0 0.1 0.0 0.0"
assert output_lines[2] == "2 0.0 0.0 0.0 0.1 0.05 0.15 0.1 0.1 0.0 0.1 0.0 0.0"
Loading