Skip to content

Commit

Permalink
feat: implement Poly2d.from_json()
Browse files Browse the repository at this point in the history
  • Loading branch information
tklockau committed Nov 11, 2024
1 parent e27755b commit a2d6e24
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 42 deletions.
2 changes: 1 addition & 1 deletion raillabel/format/bbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Bbox:
"The uid of the sensor, this annotation is labeled in."

attributes: dict[str, float | bool | str | list]
"Additional information associated with the bbox."
"Additional information associated with the annotation."

@classmethod
def from_json(cls, json: JSONBbox, object_uid: UUID) -> Bbox:
Expand Down
2 changes: 1 addition & 1 deletion raillabel/format/cuboid.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Cuboid:
"The uid of the sensor, this annotation is labeled in."

attributes: dict[str, float | bool | str | list]
"Additional information associated with the bbox."
"Additional information associated with the annotation."

@classmethod
def from_json(cls, json: JSONCuboid, object_uid: UUID) -> Cuboid:
Expand Down
69 changes: 32 additions & 37 deletions raillabel/format/poly2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,43 @@
from __future__ import annotations

from dataclasses import dataclass
from uuid import UUID

from ._object_annotation import _ObjectAnnotation
from raillabel.json_format import JSONPoly2d

from ._attributes import _attributes_from_json
from .point2d import Point2d


@dataclass
class Poly2d(_ObjectAnnotation):
"""Sequence of 2D points. Can either be a polygon or polyline.
Parameters
----------
uid: str
This a string representing the unique universal identifier for the annotation.
points: list of raillabel.format.Point2d
List of the 2d points that make up the polyline.
closed: bool
This parameter states, whether the polyline represents a closed shape (a polygon) or an
open line.
mode: str, optional
Mode of the polyline list of values: "MODE_POLY2D_ABSOLUTE" determines that the poly2d list
contains the sequence of (x, y) values of all points of the polyline. "MODE_POLY2D_RELATIVE"
specifies that only the first point of the sequence is defined with its (x, y) values, while
all the rest are defined relative to it. "MODE_POLY2D_SRF6DCC" specifies that SRF6DCC chain
code method is used. "MODE_POLY2D_RS6FCC" specifies that the RS6FCC method is used. Default
is 'MODE_POLY2D_ABSOLUTE'.
object: raillabel.format.Object
A reference to the object, this annotation belongs to.
sensor: raillabel.format.Sensor
A reference to the sensor, this annotation is labeled in. Default is None.
attributes: dict, optional
Attributes of the annotation. Dict keys are the name str of the attribute, values are the
attribute values. Default is {}.
Properties (read-only)
----------------------
name: str
Name of the annotation used by the VCD player for indexing in the object data pointers.
"""
class Poly2d:
"""Sequence of 2D points. Can either be a polygon or polyline."""

points: list[Point2d]
closed: bool
mode: str = "MODE_POLY2D_ABSOLUTE"
"List of the 2d points that make up the polyline."

OPENLABEL_ID = "poly2d"
closed: bool
"If True, this object represents a polygon and if False, it represents a polyline."

object: UUID
"The uid of the object, this annotation belongs to."

sensor: str
"The uid of the sensor, this annotation is labeled in."

attributes: dict[str, float | bool | str | list]
"Additional information associated with the annotation."

@classmethod
def from_json(cls, json: JSONPoly2d, object_uid: UUID) -> Poly2d:
"""Construct an instant of this class from RailLabel JSON data."""
return Poly2d(
points=[
Point2d(x=float(json.val[i]), y=float(json.val[i + 1]))
for i in range(0, len(json.val), 2)
],
closed=json.closed,
object=object_uid,
sensor=json.coordinate_system,
attributes=_attributes_from_json(json.attributes),
)
2 changes: 1 addition & 1 deletion tests/test_raillabel/format/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .test_frame_interval import frame_interval, frame_interval_json
from .test_intrinsics_pinhole import intrinsics_pinhole, intrinsics_pinhole_json
from .test_intrinsics_radar import intrinsics_radar, intrinsics_radar_json
from .test_point2d import point2d, point2d_json
from .test_point2d import point2d, point2d_json, another_point2d, another_point2d_json
from .test_point3d import point3d, point3d_json
from .test_quaternion import quaternion, quaternion_json
from .test_size2d import size2d, size2d_json
Expand Down
19 changes: 17 additions & 2 deletions tests/test_raillabel/format/test_point2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,25 @@


@pytest.fixture
def point2d_json() -> dict:
def point2d_json() -> tuple[float, float]:
return [1.5, 222]


@pytest.fixture
def point2d() -> dict:
def point2d() -> Point2d:
return Point2d(1.5, 222)


@pytest.fixture
def another_point2d_json() -> tuple[float, float]:
return [1.7, 222.2]


@pytest.fixture
def another_point2d() -> Point2d:
return Point2d(1.7, 222.2)


# == Tests ============================


Expand All @@ -28,5 +38,10 @@ def test_from_json(point2d, point2d_json):
assert actual == point2d


def test_from_json__another(another_point2d, another_point2d_json):
actual = Point2d.from_json(another_point2d_json)
assert actual == another_point2d


if __name__ == "__main__":
pytest.main([__file__, "-v"])
62 changes: 62 additions & 0 deletions tests/test_raillabel/format/test_poly2d.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: Apache-2.0

from __future__ import annotations

from uuid import UUID

import pytest

from raillabel.format import Poly2d
from raillabel.json_format import JSONPoly2d

# == Fixtures =========================


@pytest.fixture
def poly2d_json(
point2d_json,
another_point2d_json,
attributes_multiple_types_json,
) -> JSONPoly2d:
return JSONPoly2d(
uid="78f0ad89-2750-4a30-9d66-44c9da73a714",
name="lidar__poly2d__person",
closed=True,
mode="MODE_POLY2D_ABSOLUTE",
val=point2d_json + another_point2d_json,
coordinate_system="lidar",
attributes=attributes_multiple_types_json,
)


@pytest.fixture
def poly2d(
point2d,
another_point2d,
attributes_multiple_types,
) -> Poly2d:
return Poly2d(
points=[point2d, another_point2d],
closed=True,
sensor="lidar",
attributes=attributes_multiple_types,
object=UUID("b40ba3ad-0327-46ff-9c28-2506cfd6d934"),
)


# == Tests ============================


def test_from_json(poly2d, poly2d_json):
actual = Poly2d.from_json(poly2d_json, object_uid=UUID("b40ba3ad-0327-46ff-9c28-2506cfd6d934"))
assert actual == poly2d


# def test_name(poly2d):
# actual = poly2d.name("person")
# assert actual == "lidar__poly2d__person"


if __name__ == "__main__":
pytest.main([__file__, "-v"])

0 comments on commit a2d6e24

Please sign in to comment.