Skip to content

Commit

Permalink
Add tests for ellipses
Browse files Browse the repository at this point in the history
  • Loading branch information
kbolashev committed Oct 6, 2024
1 parent db08911 commit f67f1dc
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 2 deletions.
30 changes: 28 additions & 2 deletions tests/cvat/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
parse_box,
parse_polygon,
parse_points,
parse_skeleton,
parse_skeleton, parse_ellipse,
)
from dagshub_annotation_converter.ir.image import (
CoordinateStyle,
IRBBoxImageAnnotation,
IRSegmentationImageAnnotation,
IRSegmentationPoint,
IRPosePoint,
IRPoseImageAnnotation,
IRPoseImageAnnotation, IREllipseImageAnnotation,
)


Expand Down Expand Up @@ -159,3 +159,29 @@ def test_skeleton():
)

assert expected == actual


def test_ellipse():
data = """
<ellipse label="circle1" source="manual" occluded="0" cx="392.23" cy="322.84" rx="205.06" ry="202.84" z_order="0">
</ellipse>
"""

image, annotation = to_xml(data)

actual = parse_ellipse(annotation, image)

expected = IREllipseImageAnnotation(
filename="000.png",
categories={"circle1": 1.0},
image_width=1920,
image_height=1200,
coordinate_style=CoordinateStyle.DENORMALIZED,
center_x=392,
center_y=323,
radius_x=205.06,
radius_y=202.84,
rotation=0.0,
)

assert actual == expected
85 changes: 85 additions & 0 deletions tests/label_studio/test_ellipse.py
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

0 comments on commit f67f1dc

Please sign in to comment.