fix: count inside FCP's calibrated count-line x-window (predict.py:701 parity)

This commit is contained in:
jetson committed 2026-10-28 08:41:08 +07:00
1 parent 3d9cce2261
commit 8a82b8b866
5 files changed
+160 -7

No files matched your search

+16 -4
View File
@@ -468,8 +468,14 @@ def run_pipeline(
if roi is not None:
counter.line_y = roi.line_y
counter.line_x_start = roi.x1
counter.line_x_end = roi.x2
# FCP counts inside the calibrated count-line x-window
# (predict.py:702-703), not inside the truck-ROI bounds.
counter.line_x_start = (
roi.line_x_start if roi.line_x_start is not None else roi.x1
)
counter.line_x_end = (
roi.line_x_end if roi.line_x_end is not None else roi.x2
)
# Batch lifecycle
truck_centroid = None
@@ -888,8 +894,14 @@ def run_merged_pipeline(
truck_present = roi is not None and roi.confidence > 0
if roi is not None:
counter.line_y = roi.line_y
counter.line_x_start = roi.x1
counter.line_x_end = roi.x2
# FCP counts inside the calibrated count-line x-window
# (predict.py:702-703), not inside the truck-ROI bounds.
counter.line_x_start = (
roi.line_x_start if roi.line_x_start is not None else roi.x1
)
counter.line_x_end = (
roi.line_x_end if roi.line_x_end is not None else roi.x2
)
truck_centroid = None
if truck_present:
+4
View File
@@ -44,6 +44,10 @@ class TruckROI:
confidence: float
fixed: bool = False # True for zones built from zones.json (never moves)
frozen: bool = False # True when auto ROI is locked after freeze_after hits
# Calibrated count-line x-window (FCP predict.py:702-703). None when the
# ROI has no calibrated line — the counter then falls back to x1/x2.
line_x_start: int | None = None
line_x_end: int | None = None
@property
def width(self) -> int:
+16 -3
View File
@@ -35,9 +35,13 @@ def build_static_roi(zone: dict[str, Any], video_w: int, video_h: int) -> TruckR
- reference_resolution: [w, h] (optional, default [1920, 1080])
Coordinates are stored at reference_resolution and scaled to actual video_w/video_h.
The zone's count_line also yields the calibrated x-window
(line_x_start / line_x_end) the counter counts inside; a zone without a
count_line leaves both None.
"""
truck_pts = zone.get("truck_area", [])
count_line = zone.get("count_line", {})
count_line = zone.get("count_line") or {}
ref_w, ref_h = zone.get("reference_resolution", [1920, 1080])
# Scale factors from reference resolution to actual video
@@ -57,14 +61,23 @@ def build_static_roi(zone: dict[str, Any], video_w: int, video_h: int) -> TruckR
x2, y2 = int(video_w * 0.75), int(video_h * 0.95)
line_y = int(count_line.get("y", (y1 + y2) // 2) * sy)
line_x_start = int(count_line.get("x_start", x1) * sx)
line_x_end = int(count_line.get("x_end", x2) * sx)
if count_line:
# Calibrated count-line x-window (FCP predict.py:702-703). The counter
# counts inside it, not inside the truck-ROI bounds.
line_x_start = int(count_line.get("x_start", x1) * sx)
line_x_end = int(count_line.get("x_end", x2) * sx)
else:
# No calibrated line — the counter falls back to the ROI bounds.
line_x_start = None
line_x_end = None
return TruckROI(
x1=x1, y1=y1, x2=x2, y2=y2,
line_y=line_y,
confidence=1.0,
fixed=True,
line_x_start=line_x_start,
line_x_end=line_x_end,
)
+93
View File
@@ -1208,3 +1208,96 @@ def test_async_writer_surfaces_first_write_error():
assert fake.finish_calls == 0
# Worker keeps draining after the failure (frames 4..6 still written).
assert fake.frames == [1, 2, 4, 5, 6]
# ── Count-line x-window (FCP predict.py:701-703 parity) ──────────────────
def _count_line_zone():
"""A fixed zone whose calibrated count-line window differs from the ROI bounds."""
from src.truck_roi import TruckROI
return TruckROI(
x1=400, y1=256, x2=936, y2=718, # truck-ROI bounds
line_y=220,
confidence=1.0,
fixed=True,
line_x_start=384, # FCP's calibrated count-line window
line_x_end=934,
)
def _model_config(tmp_path):
return ModelConfig(
filename="test.pt",
path=str(tmp_path / "test.pt"),
stem="test",
known_classes=["sack"],
)
def test_pipeline_uses_the_count_line_x_window_for_the_counter(tmp_path, monkeypatch):
"""The counter counts inside the calibrated window, not the truck-ROI bounds."""
counter, _batch = _patch_counting_fakes(monkeypatch, [INSIDE_SACK])
video_path = str(tmp_path / "t.mp4")
_write_video(video_path, frames=2)
run_pipeline(
video_path=video_path,
model_config=_model_config(tmp_path),
output_path=str(tmp_path / "out.mp4"),
preview_enabled=False,
fixed_zone=_count_line_zone(),
)
assert counter.line_x_start == 384
assert counter.line_x_end == 934
def test_merged_pipeline_uses_the_count_line_x_window_for_the_counter(tmp_path, monkeypatch):
"""The merged runner counts inside the calibrated window too."""
from src.pipeline import run_merged_pipeline
counter, _batch = _patch_counting_fakes(monkeypatch, [INSIDE_SACK])
video_path = str(tmp_path / "t.mp4")
_write_video(video_path, frames=2)
run_merged_pipeline(
video_path=video_path,
model_configs=[
ModelConfig(
filename="a.pt",
path=str(tmp_path / "a.pt"),
stem="a",
known_classes=["sack"],
)
],
output_path=str(tmp_path / "merged.mp4"),
preview_enabled=False,
fixed_zone=_count_line_zone(),
)
assert counter.line_x_start == 384
assert counter.line_x_end == 934
def test_pipeline_falls_back_to_the_roi_bounds_without_a_count_line(tmp_path, monkeypatch):
"""An auto-detected ROI has no calibrated line -> the ROI bounds are the window."""
from src.truck_roi import TruckROI
counter, _batch = _patch_counting_fakes(monkeypatch, [INSIDE_SACK])
video_path = str(tmp_path / "t.mp4")
_write_video(video_path, frames=2)
run_pipeline(
video_path=video_path,
model_config=_model_config(tmp_path),
output_path=str(tmp_path / "out.mp4"),
preview_enabled=False,
fixed_zone=TruckROI(
x1=400, y1=256, x2=936, y2=718, line_y=220, confidence=1.0, fixed=True
),
)
assert counter.line_x_start == 400
assert counter.line_x_end == 936
+31
View File
@@ -223,3 +223,34 @@ def test_build_counting_polygon_none_on_non_finite_coordinates():
assert build_counting_polygon(
{"counting_area": [[float("inf"), 0], [10, 10], [20, 0]]}, 1280, 720
) is None
# ── Count-line x-window (FCP predict.py:701-703 parity) ──────────────────
def test_build_static_roi_carries_the_count_line_x_window():
"""The ROI carries FCP's calibrated count-line x-window.
FCP predict.py:701-703 scales the count line to the 1280x720 analysis
frame: x 384...934, y 220.
"""
zone = {
"truck_area": [[600, 385], [609, 1076], [1404, 1078], [1381, 343]],
"count_line": {"x_start": 577, "x_end": 1401, "y": 330},
"reference_resolution": [1920, 1080],
}
roi = build_static_roi(zone, video_w=1280, video_h=720)
assert roi.line_x_start == 384 # int(577 * 1280 / 1920)
assert roi.line_x_end == 934 # int(1401 * 1280 / 1920)
assert roi.line_y == 220 # int(330 * 720 / 1080)
def test_build_static_roi_without_count_line_has_no_x_window():
"""No calibrated line -> no x-window; the counter falls back to the ROI bounds."""
roi = build_static_roi(
{"truck_area": [[600, 385], [609, 1076], [1404, 1078], [1381, 343]]},
video_w=1280,
video_h=720,
)
assert roi.line_x_start is None
assert roi.line_x_end is None