feat(viz): draw the calibrated counting and truck polygons
This commit is contained in:
1 parent
7662890cc5
commit
6664cefd18
3 files changed
+68
-11
No files matched your search
+39
-11
@@ -9,6 +9,7 @@ from __future__ import annotations
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
from shapely.geometry import Polygon
|
||||
|
||||
from src.batch import BatchRecord
|
||||
from src.interfaces import Detection
|
||||
@@ -26,6 +27,8 @@ ORANGE = (0, 165, 255)
|
||||
GRAY = (140, 140, 140)
|
||||
DARK_GREEN = (0, 130, 0)
|
||||
LIGHT_BLUE = (255, 200, 100)
|
||||
# Counting-area polygon — FCP's detection area, drawn behind the boxes.
|
||||
COUNT_ZONE = (235, 99, 37)
|
||||
|
||||
|
||||
# State display labels and colors
|
||||
@@ -54,17 +57,32 @@ class DashboardOverlay:
|
||||
idle_timer: float = 0.0,
|
||||
stabilize_progress: float = 0.0,
|
||||
waiting_duration: float = 0.0,
|
||||
counting_polygon: Polygon | None = None,
|
||||
truck_polygon: Polygon | None = None,
|
||||
) -> np.ndarray:
|
||||
out = frame.copy()
|
||||
# The calibrated zones go down first so the detection boxes stay
|
||||
# readable on top of them (FCP draw() draws the polygons first too).
|
||||
if counting_polygon is not None and counting_polygon.area > 0:
|
||||
pts = np.array(
|
||||
counting_polygon.exterior.coords, dtype=np.int32,
|
||||
).reshape((-1, 1, 2))
|
||||
cv2.polylines(out, [pts], True, COUNT_ZONE, 2)
|
||||
if truck_polygon is not None and truck_polygon.area > 0:
|
||||
pts = np.array(
|
||||
truck_polygon.exterior.coords, dtype=np.int32,
|
||||
).reshape((-1, 1, 2))
|
||||
cv2.polylines(out, [pts], True, ORANGE, 2)
|
||||
if roi is not None:
|
||||
self._draw_roi(frame, roi)
|
||||
self._draw_counting_zone(frame, roi)
|
||||
self._draw_detections(frame, detections)
|
||||
self._draw_stats(frame, loading_count, unloading_count, batch_id, history)
|
||||
self._draw_roi(out, roi)
|
||||
self._draw_counting_zone(out, roi)
|
||||
self._draw_detections(out, detections)
|
||||
self._draw_stats(out, loading_count, unloading_count, batch_id, history)
|
||||
self._draw_system_state(
|
||||
frame, system_state, batch_duration, idle_timer, stabilize_progress,
|
||||
out, system_state, batch_duration, idle_timer, stabilize_progress,
|
||||
waiting_duration,
|
||||
)
|
||||
return frame
|
||||
return out
|
||||
|
||||
def _draw_roi(self, frame: np.ndarray, roi: TruckROI) -> None:
|
||||
if roi.fixed:
|
||||
@@ -132,20 +150,30 @@ class DashboardOverlay:
|
||||
) -> None:
|
||||
y = roi.line_y
|
||||
h, w = frame.shape[:2]
|
||||
# Blend only the narrow strip region, not the full frame
|
||||
# The count window is the calibrated one when the zone has it — the
|
||||
# counter counts inside that window, not inside the ROI bounds.
|
||||
x1 = roi.line_x_start if roi.line_x_start is not None else roi.x1
|
||||
x2 = roi.line_x_end if roi.line_x_end is not None else roi.x2
|
||||
# A corrupt zone can invert the window or push it out of the frame;
|
||||
# fall back to the ROI bounds so the band is always drawn.
|
||||
if not (0 <= x1 < x2 <= w):
|
||||
x1, x2 = roi.x1, roi.x2
|
||||
sy = max(0, y - margin)
|
||||
ey = min(h, y + margin)
|
||||
sx = max(0, roi.x1)
|
||||
ex = min(w, roi.x2)
|
||||
sx = max(0, x1)
|
||||
ex = min(w, x2)
|
||||
if ex <= sx or ey <= sy:
|
||||
return
|
||||
# Blend only the narrow strip region, not the full frame
|
||||
strip = frame[sy:ey, sx:ex].copy()
|
||||
cv2.rectangle(strip, (0, 0), (ex - sx, ey - sy), MAGENTA, -1)
|
||||
cv2.addWeighted(strip, 0.15, frame[sy:ey, sx:ex], 0.85, 0, frame[sy:ey, sx:ex])
|
||||
# Draw center line
|
||||
cv2.line(frame, (roi.x1, y), (roi.x2, y), MAGENTA, 2)
|
||||
cv2.line(frame, (sx, y), (ex, y), MAGENTA, 2)
|
||||
cv2.putText(
|
||||
frame,
|
||||
f"COUNT LINE Y={y} (y1 trigger)",
|
||||
(roi.x1, y - margin - 8),
|
||||
(sx, y - margin - 8),
|
||||
cv2.FONT_HERSHEY_SIMPLEX, 0.5, MAGENTA, 1,
|
||||
)
|
||||
|
||||
|
||||
@@ -597,6 +597,8 @@ def run_pipeline(
|
||||
batch_duration=batch_mgr.batch_duration,
|
||||
stabilize_progress=batch_mgr.stabilize_progress,
|
||||
waiting_duration=batch_mgr.waiting_duration,
|
||||
counting_polygon=counting_polygon,
|
||||
truck_polygon=truck_polygon,
|
||||
)
|
||||
|
||||
# Draw model info overlay
|
||||
@@ -1073,6 +1075,8 @@ def run_merged_pipeline(
|
||||
batch_duration=batch_mgr.batch_duration,
|
||||
stabilize_progress=batch_mgr.stabilize_progress,
|
||||
waiting_duration=batch_mgr.waiting_duration,
|
||||
counting_polygon=counting_polygon,
|
||||
truck_polygon=truck_polygon,
|
||||
)
|
||||
|
||||
# Model info overlay
|
||||
|
||||
@@ -5,6 +5,7 @@ import os
|
||||
import cv2
|
||||
import numpy as np
|
||||
import pytest
|
||||
from shapely.geometry import Polygon
|
||||
from src.counting import MultiClassLineCounter
|
||||
from src.pipeline import apply_class_filter, run_pipeline, PipelineResult
|
||||
from src.interfaces import Detection
|
||||
@@ -1658,3 +1659,27 @@ def test_the_merged_runner_keeps_overlapping_sacks_of_one_model(tmp_path, monkey
|
||||
)
|
||||
|
||||
assert {d.bbox for d in counter.seen} == {_OVERLAP_A.bbox, _OVERLAP_B.bbox}
|
||||
|
||||
|
||||
# ── Zone polygons in the annotated output ───────────────────────────────
|
||||
|
||||
|
||||
def test_dashboard_draws_zone_polygons():
|
||||
"""The overlay draws the calibrated polygons, not just the ROI box."""
|
||||
from src.dashboard import DashboardOverlay
|
||||
|
||||
counting = Polygon([(383, 33), (391, 718), (945, 717), (931, 33)])
|
||||
truck = Polygon([(400, 257), (406, 717), (936, 719), (921, 229)])
|
||||
frame = np.zeros((720, 1280, 3), dtype=np.uint8)
|
||||
|
||||
out = DashboardOverlay().draw(
|
||||
frame, [], None, 0, 0, None, None,
|
||||
counting_polygon=counting, truck_polygon=truck,
|
||||
)
|
||||
|
||||
assert out.shape == frame.shape
|
||||
assert out.any(), "nothing was drawn"
|
||||
# A pixel on the truck polygon edge must be lit in the truck colour.
|
||||
assert out[257, 400].tolist() == [0, 165, 255]
|
||||
# Same for the counting polygon edge.
|
||||
assert out[33, 383].tolist() == [235, 99, 37]
|
||||
Reference in new issue
Block a user