|
| 1 | +# Copyright DB InfraGO AG and contributors |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +from collections.abc import Iterable |
| 7 | +from typing import Any |
| 8 | +from uuid import UUID |
| 9 | + |
| 10 | +import raillabel |
| 11 | + |
| 12 | +from raillabel_providerkit.validation.issue import Issue, IssueIdentifiers, IssueType |
| 13 | + |
| 14 | +# Mapping: |
| 15 | +# - camera: Bbox |
| 16 | +# - lidar: Cuboid |
| 17 | +# - radar: und Cuboid |
| 18 | +_ALLOWED_BY_SENSOR_TYPE: dict[str, tuple[str, ...]] = { |
| 19 | + "camera": ("Bbox",), |
| 20 | + "lidar": ("Cuboid",), |
| 21 | + "radar": ("Bbox", "Cuboid"), |
| 22 | +} |
| 23 | + |
| 24 | + |
| 25 | +def _normalize_anno_type(t: str) -> str: |
| 26 | + t_low = (t or "").strip().lower() |
| 27 | + mapping = { |
| 28 | + "bbox": "Bbox", |
| 29 | + "cuboid": "Cuboid", |
| 30 | + "poly2d": "Poly2d", |
| 31 | + "poly3d": "Poly3d", |
| 32 | + "num": "Num", |
| 33 | + "seg3d": "Seg3d", |
| 34 | + } |
| 35 | + return mapping.get(t_low, t if t else "") |
| 36 | + |
| 37 | + |
| 38 | +def _iter_annotations(scene: raillabel.Scene) -> Iterable[tuple[int, Any]]: |
| 39 | + """Iterate over all annotations in the scene.""" |
| 40 | + frames = getattr(scene, "frames", {}) or {} |
| 41 | + for frame_id, frame in getattr(frames, "items", lambda: frames.items())(): |
| 42 | + annotations = getattr(frame, "annotations", {}) or {} |
| 43 | + for anno in annotations.values(): |
| 44 | + yield frame_id, anno |
| 45 | + |
| 46 | + |
| 47 | +def _get_annotation_id(anno: object) -> UUID | None: |
| 48 | + return getattr(anno, "id", None) |
| 49 | + |
| 50 | + |
| 51 | +def _get_annotation_type(anno: object) -> str | None: |
| 52 | + t = getattr(anno, "type", None) |
| 53 | + if not t: |
| 54 | + return None |
| 55 | + return _normalize_anno_type(str(t)) |
| 56 | + |
| 57 | + |
| 58 | +def _get_sensor_id_from_annotation(anno: object) -> str | None: |
| 59 | + # 1) coordinate.sensor / coordinate.sensor_id |
| 60 | + coord = getattr(anno, "coordinate", None) |
| 61 | + if coord is not None: |
| 62 | + sid = getattr(coord, "sensor", None) or getattr(coord, "sensor_id", None) |
| 63 | + if sid: |
| 64 | + return str(sid) |
| 65 | + |
| 66 | + # 2) anno.sensor / anno.sensor_id |
| 67 | + for attr in ("sensor", "sensor_id"): |
| 68 | + sid = getattr(anno, attr, None) |
| 69 | + if sid: |
| 70 | + return str(sid) |
| 71 | + |
| 72 | + # 3) coordinates-Collection |
| 73 | + coords = getattr(anno, "coordinates", None) |
| 74 | + if isinstance(coords, list | tuple): |
| 75 | + for c in coords: |
| 76 | + sid = getattr(c, "sensor", None) or getattr(c, "sensor_id", None) |
| 77 | + if sid: |
| 78 | + return str(sid) |
| 79 | + |
| 80 | + return None |
| 81 | + |
| 82 | + |
| 83 | +def _sensor_type(scene: raillabel.Scene, sensor_id: str) -> str | None: |
| 84 | + """Return the sensor type ('camera', 'lidar', 'radar') or None if unknown.""" |
| 85 | + sensors = getattr(scene, "sensors", {}) or {} |
| 86 | + sensor = sensors.get(sensor_id) |
| 87 | + if sensor is None and hasattr(sensors, "get"): |
| 88 | + sensor = sensors.get(sensor_id) |
| 89 | + if sensor is None: |
| 90 | + return None |
| 91 | + s_type = getattr(sensor, "type", None) |
| 92 | + return str(s_type).lower() if s_type else None |
| 93 | + |
| 94 | + |
| 95 | +def validate_annotation_type_per_sensor(scene: raillabel.Scene) -> list[Issue]: |
| 96 | + """Validate that each annotation type is compatible with its sensor type. |
| 97 | +
|
| 98 | + Returns: |
| 99 | + List of Issues if mismatches are found. |
| 100 | + """ |
| 101 | + issues: list[Issue] = [] |
| 102 | + |
| 103 | + for frame_id, anno in _iter_annotations(scene): |
| 104 | + a_type = _get_annotation_type(anno) |
| 105 | + if not a_type: |
| 106 | + continue |
| 107 | + |
| 108 | + sensor_id = _get_sensor_id_from_annotation(anno) |
| 109 | + if not sensor_id: |
| 110 | + continue |
| 111 | + |
| 112 | + s_type = _sensor_type(scene, sensor_id) |
| 113 | + if not s_type: |
| 114 | + continue |
| 115 | + |
| 116 | + allowed = _ALLOWED_BY_SENSOR_TYPE.get(s_type, ()) |
| 117 | + if allowed and a_type not in allowed: |
| 118 | + issue = Issue( |
| 119 | + type=IssueType.ANNOTATION_SENSOR_MISMATCH, |
| 120 | + identifiers=IssueIdentifiers( |
| 121 | + frame=frame_id, |
| 122 | + sensor=sensor_id, |
| 123 | + annotation=_get_annotation_id(anno), |
| 124 | + annotation_type=a_type, |
| 125 | + ), |
| 126 | + reason=( |
| 127 | + f"Annotation type '{a_type}' not allowed for sensor type '{s_type}'. " |
| 128 | + f"Allowed types: {', '.join(allowed) if allowed else 'n/a'}." |
| 129 | + ), |
| 130 | + ) |
| 131 | + issues.append(issue) |
| 132 | + |
| 133 | + return issues |
0 commit comments