Files
dora-image-view/dora_image_view/view.py
T
cristhian 00dc4c7b71 rename node to dora-image-view
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.
2026-09-15 03:22:27 -03:00

73 lines
2.0 KiB
Python

"""The display state: the frame, the overlays drawn on it, and the window."""
from dataclasses import dataclass, field
import cv2
import numpy as np
WINDOW = "Dora Node: dora-image-view"
GREEN = (0, 255, 0)
WHITE = (255, 255, 255)
FONT = cv2.FONT_HERSHEY_SIMPLEX
FONT_SCALE = 0.5
@dataclass
class Bboxes:
"""One overlay's worth of boxes, always stored as xyxy corners."""
corners: np.ndarray = field(default_factory=lambda: np.empty((0, 4)))
conf: np.ndarray = field(default_factory=lambda: np.empty(0))
labels: np.ndarray = field(default_factory=lambda: np.empty(0, dtype=object))
@dataclass
class View:
"""Overlay state, plus the window it is displayed in.
`headless` suppresses the window; the node still decodes every input.
"""
width: int | None = None
height: int | None = None
headless: bool = False
bboxes: Bboxes = field(default_factory=Bboxes)
text: str = ""
def show(self, frame: np.ndarray) -> bool:
"""Draw the overlays on `frame`, resize it, and display it.
Returns whether the viewer asked to quit by pressing `q`.
"""
self._draw(frame)
if self.width and self.height:
frame = cv2.resize(frame, (self.width, self.height))
if self.headless:
return False
cv2.imshow(WINDOW, frame)
return cv2.waitKey(1) & 0xFF == ord("q")
def _draw(self, frame: np.ndarray) -> None:
for corners, conf, label in zip(
self.bboxes.corners,
self.bboxes.conf,
self.bboxes.labels,
strict=False,
):
min_x, min_y, max_x, max_y = (int(value) for value in corners)
cv2.rectangle(frame, (min_x, min_y), (max_x, max_y), GREEN, 2)
cv2.putText(
frame,
f"{label}, {conf:0.2f}",
(max_x - 120, max_y - 10),
FONT,
FONT_SCALE,
GREEN,
1,
)
cv2.putText(frame, self.text, (20, 20), FONT, FONT_SCALE, WHITE, 1)