|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING |
| 4 | + |
| 5 | +from attrs import define |
| 6 | + |
| 7 | +from t4_devkit import Tier4 |
| 8 | + |
| 9 | +if TYPE_CHECKING: |
| 10 | + from t4_devkit.dataclass import BoxLike |
| 11 | + |
| 12 | + |
| 13 | +__all__ = ["load_dataset", "FrameGroundTruth", "SceneGroundTruth"] |
| 14 | + |
| 15 | + |
| 16 | +def load_dataset(data_root: str) -> SceneGroundTruth: |
| 17 | + """Load dataset. |
| 18 | +
|
| 19 | + Args: |
| 20 | + data_root (str): Root directory path to the dataset. |
| 21 | +
|
| 22 | + Returns: |
| 23 | + SceneGroundTruth: Loaded container of ground truths. |
| 24 | + """ |
| 25 | + t4 = Tier4("annotation", data_root=data_root, verbose=False) |
| 26 | + |
| 27 | + frames: list[FrameGroundTruth] = [] |
| 28 | + for i, sample in enumerate(t4.sample): |
| 29 | + boxes = list(map(t4.get_box3d, sample.ann_3ds)) |
| 30 | + frames.append(FrameGroundTruth(unix_time=sample.timestamp, frame_index=i, boxes=boxes)) |
| 31 | + |
| 32 | + return SceneGroundTruth(data_root=data_root, frames=frames) |
| 33 | + |
| 34 | + |
| 35 | +@define |
| 36 | +class FrameGroundTruth: |
| 37 | + """A container of boxes at a single frame. |
| 38 | +
|
| 39 | + Attributes: |
| 40 | + unix_time (int): Unix timestamp. |
| 41 | + frame_index (int): Index number of the frame. |
| 42 | + boxes (list[BoxLike]): List of ground truth instances. |
| 43 | + """ |
| 44 | + |
| 45 | + unix_time: int |
| 46 | + frame_index: int |
| 47 | + boxes: list[BoxLike] |
| 48 | + |
| 49 | + |
| 50 | +@define |
| 51 | +class SceneGroundTruth: |
| 52 | + """A container of frame ground truths. |
| 53 | +
|
| 54 | + Attributes: |
| 55 | + data_root (str): Root directory path to the dataset. |
| 56 | + frames (list[FrameGroundTruth]): List of frame ground truths. |
| 57 | + """ |
| 58 | + |
| 59 | + data_root: str |
| 60 | + frames: list[FrameGroundTruth] |
| 61 | + |
| 62 | + def lookup(self, unix_time: int, tolerance: int) -> FrameGroundTruth | None: |
| 63 | + """Lookup the closest set of ground truth. |
| 64 | +
|
| 65 | + Return None if the minimum time difference exceeds `tolerance`. |
| 66 | +
|
| 67 | + Args: |
| 68 | + unix_time (int): Unix timestamp. |
| 69 | + tolerance (int): Time difference tolerance in micro seconds. |
| 70 | +
|
| 71 | + Returns: |
| 72 | + Return frame ground truth if succeeded, otherwise None. |
| 73 | + """ |
| 74 | + closest = min(self.frames, key=lambda f: abs(unix_time - f.unix_time)) |
| 75 | + return closest if abs(unix_time - closest.unix_time) <= tolerance else None |
0 commit comments