feat(counting,zones): align corridor filter and default palet zone with FCP main

This commit is contained in:
jetson committed 2026-11-17 09:39:47 +07:00
1 parent f15caf8b7f
commit ff55a733d6
10 files changed
+34 -30

No files matched your search

+2 -2
View File
@@ -21,7 +21,7 @@ Feedmill Recounter: AI video analysis for counting sacks/boxes in feedmill video
- `src/counting.py`: `LineCrossCounter` — the FCP counting gates (single count, corridor, ID switch, unload confirm).
- Zone editor: `GET /calibrate` (`templates/calibrate.html`, `static/calibrate.js`); writes go through `POST /api/zones/<preset>` and `POST /api/zones/<preset>/reset` (localhost only).
- Zone writes go through `POST /api/zones/<preset>` (localhost-only, validated, atomic). Do not hand-edit `zones.json` while the web app runs — the editor merges over the stored preset.
- The counting gates live in `config.yaml` under `counting:` and are ported from the live counter's `feat/interactive-roi-line` branch.
- The counting gates live in `config.yaml` under `counting:` and match FCP `main` (`capture_below_px=100`, `max_step_dy_px=120`, `max_gap_frames=15`, `unload_confirm_frames=3`).
- `tools/calibrate_zone.py` and the `/calibrate` page produce the same geometry; keep their count-line derivation in sync (`x_start=min`, `x_end=max`, `y=round(mean(ys))`).
- Config: `config.yaml` (tunables), `zones.json` (fixed zones, 1280x720 analysis space), `.env` (paths/secrets).
@@ -34,7 +34,7 @@ Feedmill Recounter: AI video analysis for counting sacks/boxes in feedmill video
- Conversion to `.engine` must start from `.pt`; Ultralytics cannot export from ONNX.
- `.engine` conversion may take minutes on Jetson.
- Zones live in `zones.json` in the **1280x720 analysis space**; `reference_resolution` is `[1280, 720]`, so the scale factor is 1.0. Never introduce a second coordinate space (the editor maps client -> source in the same 1280x720 space).
- Two presets ship: `default` (the `feat/interactive-roi-line` branch's `95c031b` recalibration, count line y=182 — the active preset) and `fcp-2026-10-15` (pre-recalibration, count line y=220 — matches what the deployed live counter (commit `7beb820`) counts with today, because its `predict.py` hardcodes the line at y=220 and does not read `zones.json`).
- Two presets ship: `default` (the `feat/interactive-roi-line` branch's `95c031b` recalibration, count line y=182, plus FCP `main` `91d8b48`'s recalibrated `palet` zone `[[408, 1], [893, 1], [893, 178], [405, 178]]` above the truck — the active preset) and `fcp-2026-10-15` (pre-recalibration, count line y=220 — matches what the deployed live counter (commit `7beb820`) counts with today, because its `predict.py` hardcodes the line at y=220 and does not read `zones.json`).
- The unload branch in `src/counting.py` is unreachable by design (parity with the deployed live counter, which runs the same gates — `7beb820` contains the gates commit `8448dcb`): `unloading` is always 0 and `net` equals the gross count. Do not "fix" it into a counted unload.
- Zone writes are localhost-only; remote operators use an SSH tunnel (`ssh -L 9050:127.0.0.1:9050 jetson@<host>`).
- `tools/record_rtsp.sh` records an RTSP stream to a file for live-vs-recounter parity runs; the recounter stays file-based (no RTSP input path).
+9
View File
@@ -84,6 +84,15 @@ version tags are cut in this repo — `main` is the release line).
counter does (horizontal at the mean y of its two endpoints).
### Changed
- **Align corridor filter and default palet zone with FCP main (`91d8b48`)**:
the corridor filter in `src/counting.py` now keeps `_initial_zone` when a
detection sits deep in the truck bed (matching FCP `main`), while
`counting.max_step_dy_px` (120) and `counting.unload_confirm_frames` (3)
remain aligned with FCP's runtime `CountingConfig`
(`src/config_loader.py:104-107`). Recalibrated `presets.default.palet` in
`zones.json` and `DEFAULT_ZONES` to `[[408, 1], [893, 1], [893, 178], [405, 178]]`
(scaled from FCP's 1920×1080 `[[612, 1], [1340, 1], [1340, 267], [608, 267]]`).
`fcp-2026-10-15` stays unchanged.
- **`zones.json` now carries the calibration in the 1280×720 analysis space**
(`reference_resolution: [1280, 720]`, scale factor 1.0), matching the live
counter's geometry. Two presets ship: `default` (the
+1 -2
View File
@@ -144,8 +144,7 @@ recounter --video PATH Input video file
Edit `config.yaml` instead of the code for these values; restart the job / re-run the CLI to apply.
`config.yaml` carries the counting gates ported from the live counter's
`feat/interactive-roi-line` branch, under `counting:`:
`config.yaml` carries the counting gates aligned with FCP `main`, under `counting:`:
- `capture_below_px` (100) — corridor below the line: a sack deep in the truck
bed never counts.
+1 -1
View File
@@ -38,7 +38,7 @@ batch:
truck_gone_tolerance_seconds: 30.0 # truck-gone: in WAITING, N s with no sack and
# no truck signal -> batch ends
# Counting gates (FCP branch feat/interactive-roi-line, src/counting.py).
# Counting gates (FCP main, src/config_loader.py:104-107).
counting:
capture_below_px: 100 # corridor below the line: deep truck bed, never counts
max_step_dy_px: 120 # per-step vertical jump = ID switch -> reset track history
+1 -1
View File
@@ -51,7 +51,7 @@ DEFAULTS: dict = {
"truck_gone_tolerance_seconds": 30.0,
},
"counting": {
# FCP branch feat/interactive-roi-line, src/config_loader.py:101-105.
# FCP main, src/config_loader.py:104-107.
"capture_below_px": 100,
"max_step_dy_px": 120,
"max_gap_frames": 15,
+4 -9
View File
@@ -13,14 +13,13 @@ Counting logic (Low-FPS robust):
3-Layer deduplication:
Layer 1: State guard — must have been ABOVE before counting
Layer 2: Spatial dedup radius — same position can't trigger twice
Layer 3: Track ID — one track_id counts at most once overall
Layer 3: Track ID — one track_id can only be counted once per direction
This approach is immune to low FPS because it doesn't require
detecting the exact frame of crossing. It only needs the track
to have been seen ABOVE the line at ANY point in its lifetime.
FCP counting gates (karung_counter_semarang, branch
feat/interactive-roi-line, src/counting.py):
FCP counting gates (karung_counter_semarang, main, src/counting.py):
- corridor: detections deeper than capture_below px below the line sit
in the truck bed; they never arm a crossing
- teleport / stale gap: a track that jumped too far vertically or
@@ -50,7 +49,7 @@ class LineCrossCounter:
FCP parity: a track_id counts at most once overall, so a track that
loaded is never counted again — the unloading branch stays unreachable
(same as karung_counter_semarang, branch feat/interactive-roi-line).
(same as karung_counter_semarang, main).
"""
def __init__(
@@ -140,7 +139,7 @@ class LineCrossCounter:
Per-track history is kept: a track that was already above the old
line is still "above" the new one. Mirrors FCP's update_line
(branch feat/interactive-roi-line, src/counting.py:122-126).
(FCP main, src/counting.py:315-319).
"""
self.line_y = int(line_y)
self.line_x_start = int(line_x_start)
@@ -194,10 +193,6 @@ class LineCrossCounter:
self._has_been_below.pop(tid, None)
self._state.pop(tid, None)
self._above_streak.pop(tid, None)
# Deviation from FCP: it keeps _initial_zone here, the recounter
# drops it too — a sack that sat deep in the bed must not keep
# an unload candidate origin.
self._initial_zone.pop(tid, None)
continue
# --- TELEPORTATION & STALE GAP GATE ---
+1 -1
View File
@@ -192,7 +192,7 @@ DEFAULT_ZONES: dict[str, Any] = {
"default": {
"truck_area": [[398, 152], [359, 720], [876, 720], [949, 201]],
"counting_area": [[362, 157], [306, 720], [912, 720], [979, 169]],
"palet": [[408, 419], [969, 420], [972, 716], [405, 714]],
"palet": [[408, 1], [893, 1], [893, 178], [405, 178]],
"count_line": {"x_start": 423, "x_end": 939, "y": 182},
"reference_resolution": [1280, 720],
"left_limit": 0.27578,
+9 -12
View File
@@ -72,12 +72,11 @@ def test_reset_state_keeps_totals_but_clears_track_state():
assert [e["direction"] for e in c.update([_det(4, 130)])] == ["loading"]
# ── FCP counting gates (karung_counter_semarang, branch
# feat/interactive-roi-line, src/counting.py) ─────────────────────────
# ── FCP counting gates (karung_counter_semarang, main, src/counting.py) ───
def test_constructor_defaults_match_fcp_config():
"""Defaults mirror FCP's config values (config_loader.py CountingConfig)."""
"""Defaults mirror FCP's runtime config values (config_loader.py CountingConfig)."""
c = LineCrossCounter(line_y=220, line_x_start=0, line_x_end=1280)
assert c._capture_below == 100.0
assert c._max_step_dy == 120.0
@@ -95,19 +94,17 @@ def test_corridor_below_capture_below_never_counts():
assert c.loading_count == 0
def test_corridor_clears_initial_zone():
"""Deliberate deviation: the corridor filter drops the initial zone.
FCP keeps the first valid zone, so a sack that sat deep in the truck bed
keeps its old origin. The recounter clears it, so the track has to earn a
new origin instead of keeping the stale one.
"""
def test_corridor_keeps_initial_zone():
"""FCP parity: the corridor filter preserves the initial zone."""
c = LineCrossCounter(line_y=220, line_x_start=0, line_x_end=1280,
capture_below=100)
c.update([_det(1, 100)]) # above -> initial zone "above"
assert c._initial_zone[1] == "above"
c.update([_det(1, 400)]) # deep in the bed -> corridor filter
assert 1 not in c._initial_zone
assert c._initial_zone[1] == "above"
events = c.update([_det(1, 250)]) # back in corridor -> still no loading (_has_been_above cleared)
assert events == []
assert c.loading_count == 0
def test_id_switch_large_dy_resets_history():
@@ -201,7 +198,7 @@ def test_multiclass_forwards_gates_and_update_line():
def test_counting_config_section_matches_fcp_values():
"""config.yaml + DEFAULTS carry the four counting gates (FCP values)."""
"""config.yaml + DEFAULTS carry the four counting gates (FCP runtime values)."""
cfg = load_config()
counting = cfg["counting"]
assert counting["capture_below_px"] == 100
+5 -1
View File
@@ -311,7 +311,7 @@ def test_shipped_default_preset_matches_fcp_live_calibration():
zone = {
"truck_area": [[398, 152], [359, 720], [876, 720], [949, 201]],
"counting_area": [[362, 157], [306, 720], [912, 720], [979, 169]],
"palet": [[408, 419], [969, 420], [972, 716], [405, 714]],
"palet": [[408, 1], [893, 1], [893, 178], [405, 178]],
"count_line": {"x_start": 423, "x_end": 939, "y": 182},
"reference_resolution": [1280, 720],
}
@@ -403,11 +403,15 @@ def test_repo_zones_json_ships_the_fcp_calibrations():
assert live["count_line"] == {"x_start": 423, "x_end": 939, "y": 182}
assert live["truck_area"] == [[398, 152], [359, 720], [876, 720], [949, 201]]
assert live["counting_area"] == [[362, 157], [306, 720], [912, 720], [979, 169]]
assert live["palet"] == [[408, 1], [893, 1], [893, 178], [405, 178]]
assert DEFAULT_ZONES["default"]["palet"] == [[408, 1], [893, 1], [893, 178], [405, 178]]
legacy = cfg["presets"]["fcp-2026-10-15"]
assert legacy["reference_resolution"] == [1280, 720]
assert legacy["count_line"] == {"x_start": 385, "x_end": 934, "y": 220}
assert legacy["truck_area"] == [[400, 257], [406, 717], [936, 719], [921, 229]]
assert legacy["counting_area"] == [[383, 33], [391, 718], [945, 717], [931, 33]]
assert legacy["palet"] == [[408, 419], [969, 420], [972, 716], [405, 714]]
assert DEFAULT_ZONES["fcp-2026-10-15"]["palet"] == [[408, 419], [969, 420], [972, 716], [405, 714]]
# --- fix round 1: F1 reference_resolution, F2/F3 count_line, F4 truck_area ---
+1 -1
View File
@@ -3,7 +3,7 @@
"default": {
"truck_area": [[398, 152], [359, 720], [876, 720], [949, 201]],
"counting_area": [[362, 157], [306, 720], [912, 720], [979, 169]],
"palet": [[408, 419], [969, 420], [972, 716], [405, 714]],
"palet": [[408, 1], [893, 1], [893, 178], [405, 178]],
"count_line": {"x_start": 423, "x_end": 939, "y": 182},
"reference_resolution": [1280, 720],
"left_limit": 0.27578,