From 8abc1034018ca52acdc513a72f2b476d081f4e63 Mon Sep 17 00:00:00 2001 From: jetson Date: Thu, 12 Nov 2026 13:17:19 +0700 Subject: [PATCH] fix(viz): draw the calibrated count band instead of the ROI bounds --- src/dashboard.py | 15 +++--- tests/test_pipeline.py | 103 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 110 insertions(+), 8 deletions(-) diff --git a/src/dashboard.py b/src/dashboard.py index bdc96f1..efc566d 100644 --- a/src/dashboard.py +++ b/src/dashboard.py @@ -151,17 +151,18 @@ class DashboardOverlay: y = roi.line_y h, w = frame.shape[:2] # The count window is the calibrated one when the zone has it — the - # counter counts inside that window, not inside the ROI bounds. + # counter counts inside that window, not inside the ROI bounds + # (same fallback as src/pipeline.py:536-541). 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, x1) - ex = min(w, x2) + # Clamp the calibrated window into the frame. A corrupt zone (window + # inverted or outside the frame) counts nothing, so no band is drawn + # at all — drawing one over the ROI bounds would lie about the + # window the counter actually uses. + sx = max(0, min(w, x1)) + ex = max(0, min(w, x2)) if ex <= sx or ey <= sy: return # Blend only the narrow strip region, not the full frame diff --git a/tests/test_pipeline.py b/tests/test_pipeline.py index 0f36ec0..afc76bb 100644 --- a/tests/test_pipeline.py +++ b/tests/test_pipeline.py @@ -1678,8 +1678,109 @@ def test_dashboard_draws_zone_polygons(): ) 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] + + +def test_dashboard_count_band_spans_calibrated_window(): + """The count band follows roi.line_x_start/line_x_end, not the ROI bounds.""" + from src.dashboard import DashboardOverlay + from src.truck_roi import TruckROI + + # ROI bounds are wide; the calibrated count window is a narrow slice + # inside them — the counter only counts in that slice. + roi = TruckROI( + x1=100, y1=100, x2=1000, y2=600, line_y=300, confidence=1.0, + fixed=False, line_x_start=400, line_x_end=800, + ) + frame = np.zeros((720, 1280, 3), dtype=np.uint8) + + out = DashboardOverlay().draw( + frame, [], roi, 0, 0, None, None, + ) + + # Inside the calibrated window → magenta band. + assert out[310, 500].tolist() == [38, 0, 38] + # Inside the ROI bounds but outside the calibrated window → no band. + assert out[310, 200].tolist() == [0, 0, 0] + assert out[310, 900].tolist() == [0, 0, 0] + + +def test_dashboard_draws_no_band_for_inverted_calibrated_window(): + """An inverted calibrated window counts nothing, so no band is drawn.""" + from src.dashboard import DashboardOverlay + from src.truck_roi import TruckROI + + roi = TruckROI( + x1=100, y1=100, x2=1000, y2=600, line_y=300, confidence=1.0, + fixed=False, line_x_start=800, line_x_end=400, + ) + frame = np.zeros((720, 1280, 3), dtype=np.uint8) + + out = DashboardOverlay().draw( + frame, [], roi, 0, 0, None, None, + ) + + # No band anywhere — not over the ROI bounds either. + assert out[310, 200].tolist() == [0, 0, 0] + assert out[310, 500].tolist() == [0, 0, 0] + + +def test_dashboard_draws_polygons_behind_detection_boxes(): + """Polygons go down first, so the detection boxes stay readable.""" + from src.dashboard import DashboardOverlay + + # A polygon edge that runs right along a detection box edge. + counting = Polygon([(383, 33), (383, 200), (500, 200), (500, 33)]) + frame = np.zeros((720, 1280, 3), dtype=np.uint8) + det = Detection( + bbox=(383, 33, 500, 200), confidence=0.9, class_id=0, class_name="sack", + ) + + out = DashboardOverlay().draw( + frame, [det], None, 0, 0, None, None, counting_polygon=counting, + ) + + # The box edge (green top line) sits on top of the polygon edge. + assert out[33, 400].tolist() == [0, 200, 0] + + +def test_pipeline_forwards_zone_polygons_to_dashboard(tmp_path, monkeypatch): + """run_pipeline hands the zone polygons to the dashboard overlay.""" + from src import pipeline as pipe_mod + + _patch_pipeline_fakes(monkeypatch) + + seen: list[dict] = [] + + class _RecordingDashboard: + def draw(self, frame, **kwargs): + seen.append(kwargs) + return frame + + monkeypatch.setattr(pipe_mod, "DashboardOverlay", _RecordingDashboard) + + video_path = str(tmp_path / "t.mp4") + _write_video(video_path) + counting = Polygon([(383, 33), (391, 718), (945, 717), (931, 33)]) + truck = Polygon([(400, 257), (406, 717), (936, 719), (921, 229)]) + + run_pipeline( + video_path=video_path, + model_config=ModelConfig( + filename="test.pt", + path=str(tmp_path / "test.pt"), + stem="test", + known_classes=["sack", "box"], + ), + output_path=str(tmp_path / "out.mp4"), + preview_enabled=False, + counting_polygon=counting, + truck_polygon=truck, + ) + + assert seen, "dashboard.draw() was never called" + assert all(call["counting_polygon"] is counting for call in seen) + assert all(call["truck_polygon"] is truck for call in seen)