Skip to content

Commit

Permalink
feat: implement SceneBuilder.add_frame()
Browse files Browse the repository at this point in the history
  • Loading branch information
unexcellent committed Nov 15, 2024
1 parent ab02190 commit 55554d4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ ignore = [
"D107", # __init__ docstrings are not necessary
"D203", # incompatible with D211
"D213", # incompatible with D212
"D413", # rule by convention which looks weird
"D417", # kwargs can not be typed for the filter function

"FBT001", # flags in functions are not bad practice
Expand Down
27 changes: 27 additions & 0 deletions raillabel/scene_builder/scene_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@

from copy import deepcopy
from dataclasses import dataclass
from decimal import Decimal
from uuid import UUID

from raillabel.format import (
Camera,
Frame,
GpsImu,
IntrinsicsPinhole,
IntrinsicsRadar,
Expand Down Expand Up @@ -84,6 +86,31 @@ def add_sensor(self, sensor_id: str) -> SceneBuilder:

return SceneBuilder(scene)

def add_frame(self, frame_id: int | None = None, timestamp: float | None = None) -> SceneBuilder:
"""Add a frame to the scene.
If no frame_id is provided, the frame_id is the lowest number not currently occupied by
another frame_id.
Example:
```python
scene = SceneBuilder.empty().add_frame(frame_id=1).add_frame(frame_id=3).add_frame().result
assert sorted(list(scene.frames.keys())) == [1, 2, 3]
```
"""
scene = deepcopy(self.result)

if frame_id is None:
frame_id = 1
while frame_id in scene.frames:
frame_id += 1

scene.frames[frame_id] = Frame(
timestamp=Decimal(timestamp) if timestamp is not None else None
)

return SceneBuilder(scene)


def _resolve_empty_object_name_or_type(
object_type: str | None, object_name: str | None
Expand Down
27 changes: 27 additions & 0 deletions tests/test_raillabel/scene_builder/test_scene_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from __future__ import annotations
from uuid import UUID
from decimal import Decimal

import pytest

Expand All @@ -19,6 +20,7 @@
OtherSensor,
IntrinsicsPinhole,
IntrinsicsRadar,
Frame,
)


Expand Down Expand Up @@ -173,5 +175,30 @@ def test_add_sensor__other():
)


def test_add_frame():
actual = SceneBuilder.empty().add_frame(1, 1631691173)
assert actual.result == Scene(
metadata=Metadata(schema_version="1.0.0"), frames={1: Frame(timestamp=Decimal(1631691173))}
)


def test_add_frame__no_timestamp():
actual = SceneBuilder.empty().add_frame(1)
assert actual.result == Scene(
metadata=Metadata(schema_version="1.0.0"), frames={1: Frame(timestamp=None)}
)


def test_add_frame__no_frame_id():
actual = SceneBuilder.empty().add_frame().add_frame()
assert actual.result == Scene(
metadata=Metadata(schema_version="1.0.0"),
frames={
1: Frame(),
2: Frame(),
},
)


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

0 comments on commit 55554d4

Please sign in to comment.