The repository, the Python package, the entrypoint and the manifest name all now agree. `plot` was carried over from the upstream opencv-plot node and no longer described anything here, so the module, the class and the environment variables follow the node: dora_plot/ -> dora_image_view/ dora_plot/plot.py -> dora_image_view/view.py Plot -> View PLOT_WIDTH/HEIGHT -> VIEW_WIDTH/HEIGHT --plot-width/-height -> --view-width/-height Breaking for anyone wiring `path: dora-plot` or setting PLOT_WIDTH; nothing is tagged yet.
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_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()
|