"""Parsing of the `bbox` input: an Arrow struct carrying boxes and labels.""" import numpy as np from dora_image_view.view import Bboxes def parse(value, bbox_format: str) -> Bboxes: """Read a `bbox` payload. `bbox_format` is the `format` metadata parameter.""" fields = value[0] corners = _flat(fields, "bbox").reshape(-1, 4) if bbox_format == "xyxy": pass elif bbox_format == "xywh": # xywh is centre-based: x and y are the box centre, not a corner. x, y, w, h = corners.T corners = np.stack([x - w / 2, y - h / 2, x + w / 2, y + h / 2], axis=1) else: raise RuntimeError(f"unsupported bbox format: {bbox_format}") return Bboxes( corners=corners, conf=_flat(fields, "conf"), labels=_flat(fields, "labels", numeric=False), ) def _flat(fields, name: str, numeric: bool = True) -> np.ndarray: """Read a struct field as a flat numpy array. Senders wrap each field in a single-element list -- that is what `pa.array([{...}])` produces from numpy arrays -- so unwrap it when present. """ column = fields[name] column = getattr(column, "values", column) return column.to_numpy(zero_copy_only=False) if not numeric else column.to_numpy()