File size: 6,576 Bytes
0ff5524 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | """Annotation parsing for local ISAT bbox/segmentation labels."""
from __future__ import annotations
import json
from dataclasses import asdict, dataclass
from pathlib import Path
from typing import Any
import h5py
import numpy as np
from e_jepa_ttc.data.evttc import read_manifest
from e_jepa_ttc.data.targets import interpolate_ttc_seconds, load_ttc_csv
from e_jepa_ttc.data.types import DatasetSequence
@dataclass(frozen=True)
class LabelMeasurement:
"""One labeled object measurement aligned to a sequence timestamp."""
sequence_id: str
frame_index: int
timestamp_us: int
category: str
bbox_xyxy: tuple[float, float, float, float]
bbox_area: float
bbox_scale: float
ttc_seconds: float
image_width: int | None = None
image_height: int | None = None
segmentation_xy: tuple[tuple[float, float], ...] | None = None
def to_dict(self) -> dict[str, Any]:
"""Serialize measurement for JSON/YAML outputs."""
return asdict(self)
def _bbox_from_segmentation(segmentation: object) -> tuple[float, float, float, float] | None:
if not isinstance(segmentation, list) or not segmentation:
return None
points: list[tuple[float, float]] = []
for point in segmentation:
if isinstance(point, list) and len(point) >= 2:
points.append((float(point[0]), float(point[1])))
if not points:
return None
xs = [point[0] for point in points]
ys = [point[1] for point in points]
return min(xs), min(ys), max(xs), max(ys)
@dataclass(frozen=True)
class ParsedISATLabel:
"""Largest labeled object and source image dimensions from one ISAT file."""
category: str
bbox_xyxy: tuple[float, float, float, float]
image_width: int | None
image_height: int | None
segmentation_xy: tuple[tuple[float, float], ...] | None
def parse_isat_label_metadata(path: str | Path) -> ParsedISATLabel | None:
"""Parse the largest object bbox and source image size from an ISAT JSON file."""
label_path = Path(path)
data = json.loads(label_path.read_text(encoding="utf-8"))
objects = data.get("objects", [])
if not isinstance(objects, list):
return None
info = data.get("info", {})
image_width = None
image_height = None
if isinstance(info, dict):
raw_width = info.get("width")
raw_height = info.get("height")
if raw_width is not None:
image_width = int(raw_width)
if raw_height is not None:
image_height = int(raw_height)
best: (
tuple[
str,
tuple[float, float, float, float],
float,
tuple[tuple[float, float], ...] | None,
]
| None
) = None
for item in objects:
if not isinstance(item, dict):
continue
raw_segmentation = item.get("segmentation")
bbox = _bbox_from_segmentation(raw_segmentation)
segmentation = (
tuple(
(float(point[0]), float(point[1]))
for point in raw_segmentation
if isinstance(point, list) and len(point) >= 2
)
if isinstance(raw_segmentation, list)
else None
)
if not segmentation:
segmentation = None
raw_bbox = item.get("bbox")
if bbox is None and isinstance(raw_bbox, list) and len(raw_bbox) >= 4:
bbox = (float(raw_bbox[0]), float(raw_bbox[1]), float(raw_bbox[2]), float(raw_bbox[3]))
if bbox is None:
continue
x0, y0, x1, y1 = bbox
area = max(0.0, x1 - x0) * max(0.0, y1 - y0)
category = str(item.get("category", "unknown"))
if best is None or area > best[2]:
best = (category, bbox, area, segmentation)
if best is None:
return None
return ParsedISATLabel(
category=best[0],
bbox_xyxy=best[1],
image_width=image_width,
image_height=image_height,
segmentation_xy=best[3],
)
def parse_isat_label(path: str | Path) -> tuple[str, tuple[float, float, float, float]] | None:
"""Parse the largest object bbox from an ISAT JSON label file."""
parsed = parse_isat_label_metadata(path)
if parsed is None:
return None
return parsed.category, parsed.bbox_xyxy
def load_label_measurements(sequence: DatasetSequence) -> list[LabelMeasurement]:
"""Load object label measurements aligned through Blackfly frame timestamps."""
label_dir = sequence.resolve("label_dir")
event_hdf5 = sequence.resolve("event_hdf5")
ttc_csv = sequence.resolve("ttc_csv")
if label_dir is None or event_hdf5 is None or ttc_csv is None:
return []
if not label_dir.exists():
return []
table = load_ttc_csv(ttc_csv)
measurements: list[LabelMeasurement] = []
with h5py.File(event_hdf5, "r") as h5:
if "blackflys/left/ts" not in h5:
return []
frame_ts = h5["blackflys/left/ts"][:].astype(np.int64)
for label_path in sorted(label_dir.glob("*.json")):
try:
frame_index = int(label_path.stem)
except ValueError:
continue
if frame_index < 0 or frame_index >= len(frame_ts):
continue
parsed = parse_isat_label_metadata(label_path)
if parsed is None:
continue
bbox = parsed.bbox_xyxy
timestamp_us = int(frame_ts[frame_index])
ttc_seconds = interpolate_ttc_seconds(table, timestamp_us)
if ttc_seconds is None:
continue
x0, y0, x1, y1 = bbox
area = max(0.0, x1 - x0) * max(0.0, y1 - y0)
if area <= 0:
continue
measurements.append(
LabelMeasurement(
sequence_id=sequence.sequence_id,
frame_index=frame_index,
timestamp_us=timestamp_us,
category=parsed.category,
bbox_xyxy=bbox,
bbox_area=area,
bbox_scale=float(np.sqrt(area)),
ttc_seconds=float(ttc_seconds),
image_width=parsed.image_width,
image_height=parsed.image_height,
segmentation_xy=parsed.segmentation_xy,
)
)
return measurements
def load_measurements_from_manifest(path: str | Path) -> dict[str, list[LabelMeasurement]]:
"""Load label measurements for every sequence in a manifest."""
return {
sequence.sequence_id: load_label_measurements(sequence) for sequence in read_manifest(path)
}
|