The repository is dora-plot, but the package, the entrypoint, the manifest name and the window title were all opencv-plot. Consumers wire the node by `path:`, so the split name was visible in every dataflow. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018WLVZCFFcCH1EpTXGfS9aD
38 lines
1.2 KiB
Python
38 lines
1.2 KiB
Python
"""Parsing of the `bbox` input: an Arrow struct carrying boxes and labels."""
|
|
|
|
import numpy as np
|
|
|
|
from dora_plot.plot 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()
|