Skip to content

Commit

Permalink
feat: uai warning duplicate uid
Browse files Browse the repository at this point in the history
  • Loading branch information
tklockau committed Sep 18, 2023
1 parent d4e067f commit a09b9c2
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
24 changes: 24 additions & 0 deletions raillabel/format/understand_ai/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
# SPDX-License-Identifier: Apache-2.0

import typing as t
import uuid
from dataclasses import dataclass
from decimal import Decimal

from ..._util._warning import _warning
from ._annotation import _Annotation
from ._translation import translate_class_id, translate_sensor_id
from .bounding_box_2d import BoundingBox2d
Expand Down Expand Up @@ -38,6 +40,8 @@ class Frame:
polyline_2ds: t.Dict[str, Polyline2d]
segmentation_3ds: t.Dict[str, Segmentation3d]

_annotation_uids: t.Set[str] = None

@property
def annotations(self) -> dict:
"""Return all annotations of this frame in one dict."""
Expand Down Expand Up @@ -96,6 +100,8 @@ def fromdict(cls, data_dict: dict) -> "Frame":
Converted frame.
"""

cls._annotation_uids = set()

return Frame(
id=int(data_dict["frameId"]),
timestamp=Decimal(data_dict["timestamp"]),
Expand Down Expand Up @@ -136,8 +142,26 @@ def to_raillabel(self) -> dict:
def _annotation_fromdict(
cls, data_dict: dict, annotation_class: t.Type[_Annotation]
) -> t.Dict[str, t.Type[_Annotation]]:

annotations = {}
for annotation_dict in data_dict:
annotation_dict["id"] = cls._check_duplicate_annotation_uid(annotation_dict["id"])
annotations[annotation_dict["id"]] = annotation_class.fromdict(annotation_dict)

return {ann["id"]: annotation_class.fromdict(ann) for ann in data_dict}

@classmethod
def _check_duplicate_annotation_uid(cls, uid: str) -> str:

if uid in cls._annotation_uids:
_warning(
f"Annotation uid {uid} is contained more than once. A new uid will be assigned."
)
return str(uuid.uuid4())

cls._annotation_uids.add(uid)
return uid

def _frame_properties_to_raillabel(self) -> dict:

streams_dict = {}
Expand Down
27 changes: 27 additions & 0 deletions tests/test_raillabel/format/understand_ai/test_uai_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
sys.path.insert(1, str(Path(__file__).parent.parent.parent.parent.parent))

import raillabel.format.understand_ai as uai_format
from raillabel._util._warning import _WarningsLogger

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

Expand Down Expand Up @@ -181,6 +182,32 @@ def test_to_raillabel(
}
}


def test_warning_duplicate_annotation_id(
bounding_box_2d_uai_dict, polyline_2d_uai_dict,
sensor_lidar_uai_dict
):
polyline_2d_uai_dict["id"] = bounding_box_2d_uai_dict["id"]

frame_dict = {
"frameId": "000",
"timestamp": sensor_lidar_uai_dict["timestamp"],
"annotations": {
"2D_BOUNDING_BOX": [bounding_box_2d_uai_dict],
"2D_POLYLINE": [polyline_2d_uai_dict],
"2D_POLYGON": [],
"3D_BOUNDING_BOX": [],
"3D_SEGMENTATION": [],
}
}

with _WarningsLogger() as logger:
uai_format.Frame.fromdict(frame_dict)

assert len(logger.warnings) == 1
assert bounding_box_2d_uai_dict["id"] in logger.warnings[0]


if __name__ == "__main__":
import os
os.system("clear")
Expand Down

0 comments on commit a09b9c2

Please sign in to comment.