Skip to content

Commit

Permalink
feat: implement Bbox.to_json()
Browse files Browse the repository at this point in the history
  • Loading branch information
unexcellent committed Nov 13, 2024
1 parent 8e48908 commit b1ebe26
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
12 changes: 11 additions & 1 deletion raillabel/format/bbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from raillabel.json_format import JSONBbox

from ._attributes import _attributes_from_json
from ._attributes import _attributes_from_json, _attributes_to_json
from .point2d import Point2d
from .size2d import Size2d

Expand Down Expand Up @@ -43,6 +43,16 @@ def from_json(cls, json: JSONBbox, object_uid: UUID) -> Bbox:
attributes=_attributes_from_json(json.attributes),
)

def to_json(self, uid: UUID, object_type: str) -> JSONBbox:
"""Export this object into the RailLabel JSON format."""
return JSONBbox(
name=self.name(object_type),
val=list(self.pos.to_json()) + list(self.size.to_json()),
coordinate_system=self.sensor,
uid=uid,
attributes=_attributes_to_json(self.attributes),
)

def name(self, object_type: str) -> str:
"""Return the name of the annotation used for indexing in the object data pointers."""
return f"{self.sensor}__bbox__{object_type}"
5 changes: 5 additions & 0 deletions tests/test_raillabel/format/test_bbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,10 @@ def test_name(bbox):
assert actual == "rgb_middle__bbox__person"


def test_to_json(bbox, bbox_json, bbox_uid):
actual = bbox.to_json(bbox_uid, object_type="person")
assert actual == bbox_json


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

0 comments on commit b1ebe26

Please sign in to comment.