diff --git a/src/counting.py b/src/counting.py index a7c930e..c6e230d 100644 --- a/src/counting.py +++ b/src/counting.py @@ -247,14 +247,19 @@ class LineCrossCounter: def reset_state(self) -> None: """Clear the per-track state only — the totals survive. - FCP parity: predict.py:1020-1032 / :1148-1150 call reset() when a - batch starts, so every batch starts with an empty track state. FCP - writes one row per batch, so dropping its totals is harmless there. - The recounter reports ONE cumulative count per clip, so the totals - must stay. + FCP clears the whole counter after it finalises the batch row + (predict.py:1139-1150) and on a manual mode switch + (predict.py:1020-1032), so every FCP batch starts with an empty + track state. FCP writes one row per batch, so dropping its totals + is harmless there. The recounter reports ONE cumulative count per + clip, so it keeps the totals and clears the per-track state at the + batch start instead — same per-batch isolation, different timing. - ponytail: one cumulative count per clip. Upgrade path: per-batch - result rows, then a full reset() per batch. + ponytail: one cumulative count per clip, and the per-track dedup + state is cleared at every batch start — a sack that physically + crosses the line in two different batches is therefore counted + twice in the clip total. Upgrade path: per-batch result rows, then + a full reset() per batch. """ self._state.clear() self._has_been_above.clear() @@ -363,6 +368,7 @@ class MultiClassLineCounter: return self._box.net_count def reset(self) -> None: + """Full reset — clears the clip totals too. No production callers.""" self._sack.reset() self._box.reset() diff --git a/src/pipeline.py b/src/pipeline.py index 2658983..84ad1ad 100644 --- a/src/pipeline.py +++ b/src/pipeline.py @@ -387,8 +387,13 @@ def run_pipeline( completed_batches += 1 def on_batch_start(batch_id, timestamp): - # FCP parity: predict.py:1020-1032 — every batch starts with an empty - # track state. The recounter keeps its clip-level totals. + # FCP clears the counter after it finalises the batch row + # (predict.py:1139-1150) and on a manual mode switch + # (predict.py:1020-1032). The recounter keeps its clip-level + # totals, so it clears the per-track state at the batch start + # instead — same per-batch isolation, different timing. The + # stabilizer reset matches FCP's manual start + # (predict.py:1021-1022). counter.reset_state() stabilizer.reset() @@ -808,8 +813,13 @@ def run_merged_pipeline( completed_batches += 1 def on_batch_start(batch_id, timestamp): - # FCP parity: predict.py:1020-1032 — every batch starts with an empty - # track state. The recounter keeps its clip-level totals. + # FCP clears the counter after it finalises the batch row + # (predict.py:1139-1150) and on a manual mode switch + # (predict.py:1020-1032). The recounter keeps its clip-level + # totals, so it clears the per-track state at the batch start + # instead — same per-batch isolation, different timing. The + # stabilizer reset matches FCP's manual start + # (predict.py:1021-1022). counter.reset_state() stabilizer.reset() diff --git a/tests/test_batch.py b/tests/test_batch.py index d7d29fa..5ccd99f 100644 --- a/tests/test_batch.py +++ b/tests/test_batch.py @@ -102,3 +102,24 @@ def test_update_batch_state_counts_the_sack_next_to_a_box(): update_batch_state(m, events=[], tracked_sacks=[sack, box], truck_polygon=poly, timestamp=1000.0, loading_count=0, unloading_count=0) assert m.sacks_calls[-1]["sacks_in_area_count"] == 1 + + +def test_on_batch_start_fires_once_when_the_batch_opens(): + """The REAL manager fires the batch-start callback with (batch_id, ts). + + The pipeline hook that clears the per-track state depends on this + callback, so it is exercised here against the real manager — the + pipeline test stubs it out. + """ + m = BatchLifecycleManager(stabilize_seconds=0.0) + calls: list[tuple[int, float]] = [] + m.on_batch_start(lambda batch_id, timestamp: calls.append((batch_id, timestamp))) + + m.update_truck(True, (100.0, 100.0), 1000.0) # truck seen -> batch opens + assert m.state == "COUNTING_SACKS" + assert calls == [(1, 1000.0)] + + # later frames must not fire it again + m.update_sacks(True, 1, 1001.0, 1, 0) + m.update_truck(True, (100.0, 100.0), 1002.0) + assert calls == [(1, 1000.0)]