-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from DagsHub/new/ellipsis
Feature: Ellipse annotations
- Loading branch information
Showing
10 changed files
with
231 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from lxml.etree import ElementBase | ||
|
||
from dagshub_annotation_converter.formats.cvat.context import parse_image_tag | ||
from dagshub_annotation_converter.ir.image import IREllipseImageAnnotation, CoordinateStyle | ||
|
||
|
||
def parse_ellipse(elem: ElementBase, containing_image: ElementBase) -> IREllipseImageAnnotation: | ||
center_x = float(elem.attrib["cx"]) | ||
center_y = float(elem.attrib["cy"]) | ||
radius_x = float(elem.attrib["rx"]) | ||
radius_y = float(elem.attrib["ry"]) | ||
|
||
rotation = float(elem.attrib.get("rotation", 0.0)) | ||
|
||
image_info = parse_image_tag(containing_image) | ||
|
||
return IREllipseImageAnnotation( | ||
categories={str(elem.attrib["label"]): 1.0}, | ||
center_x=round(center_x), | ||
center_y=round(center_y), | ||
radius_x=radius_x, | ||
radius_y=radius_y, | ||
rotation=rotation, | ||
image_width=image_info.width, | ||
image_height=image_info.height, | ||
filename=image_info.name, | ||
coordinate_style=CoordinateStyle.DENORMALIZED, | ||
) |
56 changes: 56 additions & 0 deletions
56
dagshub_annotation_converter/formats/label_studio/ellipselabels.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from typing import List, Sequence | ||
|
||
from dagshub_annotation_converter.formats.label_studio.base import ImageAnnotationResultABC | ||
from dagshub_annotation_converter.ir.image import IRImageAnnotationBase, IREllipseImageAnnotation, CoordinateStyle | ||
from dagshub_annotation_converter.util.pydantic_util import ParentModel | ||
|
||
|
||
class EllipseLabelsAnnotationsValue(ParentModel): | ||
x: float | ||
y: float | ||
radiusX: float | ||
radiusY: float | ||
rotation: float = 0.0 | ||
ellipselabels: List[str] | ||
|
||
|
||
class EllipseLabelsAnnotation(ImageAnnotationResultABC): | ||
value: EllipseLabelsAnnotationsValue | ||
type: str = "ellipselabels" | ||
|
||
def to_ir_annotation(self) -> Sequence[IRImageAnnotationBase]: | ||
res = IREllipseImageAnnotation( | ||
categories={self.value.ellipselabels[0]: 1.0}, | ||
coordinate_style=CoordinateStyle.NORMALIZED, | ||
center_x=self.value.x / 100, | ||
center_y=self.value.y / 100, | ||
radius_x=self.value.radiusX / 100, | ||
radius_y=self.value.radiusY / 100, | ||
rotation=self.value.rotation, | ||
image_width=self.original_width, | ||
image_height=self.original_height, | ||
) | ||
res.imported_id = self.id | ||
return [res] | ||
|
||
@staticmethod | ||
def from_ir_annotation(ir_annotation: IRImageAnnotationBase) -> Sequence["ImageAnnotationResultABC"]: | ||
assert isinstance(ir_annotation, IREllipseImageAnnotation) | ||
|
||
ir_annotation = ir_annotation.normalized() | ||
category = ir_annotation.ensure_has_one_category() | ||
|
||
return [ | ||
EllipseLabelsAnnotation( | ||
original_width=ir_annotation.image_width, | ||
original_height=ir_annotation.image_height, | ||
value=EllipseLabelsAnnotationsValue( | ||
x=ir_annotation.center_x * 100, | ||
y=ir_annotation.center_y * 100, | ||
radiusX=ir_annotation.radius_x * 100, | ||
radiusY=ir_annotation.radius_y * 100, | ||
rotation=ir_annotation.rotation, | ||
ellipselabels=[category], | ||
), | ||
) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
dagshub_annotation_converter/ir/image/annotations/ellipse.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from dagshub_annotation_converter.ir.image import IRImageAnnotationBase | ||
|
||
|
||
class IREllipseImageAnnotation(IRImageAnnotationBase): | ||
center_x: float | ||
center_y: float | ||
radius_x: float | ||
radius_y: float | ||
rotation: float = 0.0 | ||
"""Rotation in degrees (pivot point - top-left)""" | ||
|
||
def _normalize(self): | ||
self.center_x = self.center_x / self.image_width | ||
self.center_y = self.center_y / self.image_height | ||
self.radius_x = self.radius_x / self.image_width | ||
self.radius_y = self.radius_y / self.image_height | ||
|
||
def _denormalize(self): | ||
self.center_x = self.center_x * self.image_width | ||
self.center_y = self.center_y * self.image_height | ||
self.radius_x = self.radius_x * self.image_width | ||
self.radius_y = self.radius_y * self.image_height |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
from typing import Dict | ||
|
||
import pytest | ||
|
||
from dagshub_annotation_converter.formats.label_studio.ellipselabels import EllipseLabelsAnnotation | ||
from dagshub_annotation_converter.formats.label_studio.task import LabelStudioTask, parse_ls_task | ||
from dagshub_annotation_converter.ir.image import IREllipseImageAnnotation, CoordinateStyle | ||
from tests.label_studio.common import generate_annotation, generate_task | ||
|
||
|
||
@pytest.fixture | ||
def ellipse_annotation() -> Dict: | ||
annotation = { | ||
"x": 10, | ||
"y": 20, | ||
"radiusX": 30, | ||
"radiusY": 40, | ||
"ellipselabels": ["dog"], | ||
} | ||
|
||
return generate_annotation(annotation, "ellipselabels", "deadbeef") | ||
|
||
|
||
@pytest.fixture | ||
def ellipse_task(ellipse_annotation) -> str: | ||
return generate_task([ellipse_annotation]) | ||
|
||
|
||
@pytest.fixture | ||
def parsed_ellipse_task(ellipse_task) -> LabelStudioTask: | ||
return parse_ls_task(ellipse_task) | ||
|
||
|
||
def test_ellipse_parsing(parsed_ellipse_task): | ||
actual = parsed_ellipse_task | ||
assert len(actual.annotations) == 1 | ||
assert len(actual.annotations[0].result) == 1 | ||
|
||
ann = actual.annotations[0].result[0] | ||
assert isinstance(ann, EllipseLabelsAnnotation) | ||
assert ann.value.x == 10 | ||
assert ann.value.y == 20 | ||
assert ann.value.radiusX == 30 | ||
assert ann.value.radiusY == 40 | ||
|
||
|
||
def test_ellipse_ir(parsed_ellipse_task): | ||
actual = parsed_ellipse_task.annotations[0].result[0].to_ir_annotation() | ||
|
||
assert len(actual) == 1 | ||
ann = actual[0] | ||
assert isinstance(ann, IREllipseImageAnnotation) | ||
|
||
assert ann.center_x == 0.1 | ||
assert ann.center_y == 0.2 | ||
assert ann.radius_x == 0.3 | ||
assert ann.radius_y == 0.4 | ||
|
||
|
||
def test_ir_ellipse_addition(): | ||
task = LabelStudioTask() | ||
ellipse = IREllipseImageAnnotation( | ||
categories={"dog": 1.0}, | ||
center_x=0.1, | ||
center_y=0.2, | ||
radius_x=0.3, | ||
radius_y=0.4, | ||
rotation=60.0, | ||
image_width=100, | ||
image_height=100, | ||
coordinate_style=CoordinateStyle.NORMALIZED, | ||
) | ||
|
||
task.add_ir_annotation(ellipse) | ||
|
||
assert len(task.annotations) == 1 | ||
assert len(task.annotations[0].result) == 1 | ||
|
||
ann = task.annotations[0].result[0] | ||
assert isinstance(ann, EllipseLabelsAnnotation) | ||
assert ann.value.x == 10 | ||
assert ann.value.y == 20 | ||
assert ann.value.radiusX == 30 | ||
assert ann.value.radiusY == 40 | ||
assert ann.value.rotation == 60.0 |