feat(anpr): read every frame, settle every plate, debounce per plate
Build & push images / images (push) Successful in 3m9s

Measured on park-2 (i5-9500): a main-stream pull is ~0.1s and /analyze
~0.25s while the host sat under 3% CPU; the loop's fixed 1s sleep was the
limit (one frame per ~1.35s). The camera's picture endpoint is single-
threaded (4 parallel GETs -> two 503s) and the vision service serialises
inference, so no threads: pulls are back-to-back and serial per camera,
~3 fps. ANPR_POLL_MS is now a floor (default 0).

The loop no longer breaks on the first confident plate, and the 12s
camera-level debounce is gone: 40 of 57 plate-driven opens at park-2 had a
push dropped in that blind window, so a tailgating car was never looked at.
Each confident plate is settled once per loop (emit / anpr-skip / decline)
and the loop runs on while the camera keeps pushing, so a second car is
read by the same loop. Debounce is keyed deviceId:plate only; the card/QR
mid-poll guard is per subscriber instead of aborting the loop; the pass
sample is one per plated car. ANPR_POLL_WINDOW_MS=0 is honoured (it used to
fall back to 8s silently).

The camera event model was re-confirmed from 2780 pushes: every push is an
independent VMD/active with activePostCount=1, ~1.2s apart while a car
moves, never inactive. Nothing is held for 30s on the camera side.
This commit is contained in:
2026-10-11 18:17:25 +02:00
parent 7b29331f52
commit d93089a491
5 changed files with 337 additions and 154 deletions
+53 -12
View File
@@ -2,7 +2,7 @@
type: concept
tags: [parking, camera, anpr, subscription, lane, vision]
sources: []
updated: 2026-06-22
updated: 2026-10-11
status: open
---
@@ -32,6 +32,12 @@ here is built on that, and on the camera's hard limits.
event log): while a car MOVES, `active` repeats ~1–3 s apart; while it sits MOTIONLESS, gaps stretch
to ~15–25 s. Crucially the camera has **~no dwell lag** — it goes silent within ~1 s of the car
leaving (last event 16:15:17 vs car-left ~16:15:30).
- **Every push is an independent event — nothing is "held".** Re-confirmed on park-2 (2026-10-11,
2780 pushes over one day, both cameras): every push is `VMD`/`active` with `activePostCount=1`;
median gap 1.2 s while a car moves; never an `inactive`. So the camera does NOT open an event and
release it 30 s later — a second car entering while the first is still there produces its own
pushes. The 30 s figures in this system are OURS (`LANE_BUSY_TTL_MS`, `ANPR_POLL_MAX_MS`). Any
blindness to a second car is in the bridge, not the camera — see the 2026-10-11 block below.
## 1. Lane busy/free (BUILT)
@@ -53,16 +59,16 @@ nothing** (never blocks a ticket or opens a barrier; the standing rule).
**As built:** `hikvision-alarm.ts`, on a `vehicle`/non-`inactive` push from an `anpr`-opted-in
camera, hands the deviceId to `AnprBridge.onVehicleDetected()` (fire-and-forget, never awaited on the
camera's 200). The bridge: debounce (camera-level, pre-snapshot) → `captureSnapshot` (fresh pull, via
the reused `snapshot.ts buildCamera`) → `vision.analyze` → entry confidence floor
(`VISION_ENTRY_MIN_CONFIDENCE`, 0.85) → normalize plate → **`subscriptionFlow.match()` (match BEFORE
emit)** → if a subscriber, `deviceEvents.emitRead({kind:"plate"})`; if not, record an advisory
`anpr-skip` device_event and stop. The existing `onRead → ReadDispatcher → SubscriptionFlow.run()`
camera's 200). The bridge: `captureSnapshot` (fresh pull, via the reused `snapshot.ts buildCamera`) →
`vision.analyze` → entry confidence floor (`VISION_ENTRY_MIN_CONFIDENCE`, 0.85) → normalize plate →
**`subscriptionFlow.match()` (match BEFORE emit)** → if a subscriber, debounce **per plate** →
`deviceEvents.emitRead({kind:"plate"})`; if not, record an advisory `anpr-skip` device_event (once per
plate per loop) and go on. The existing `onRead → ReadDispatcher → SubscriptionFlow.run()`
then does the gated entry/exit + barrier open. Constructed in `server.ts` (the flows were reordered
above the hik-alarm registration so the bridge can take `subscriptionFlow`). Fail-soft throughout —
any snapshot/vision error degrades to the subscriber's card/QR, never throws into the push handler.
Two new env knobs: `VISION_ENTRY_MIN_CONFIDENCE` (0.85), `ANPR_DEBOUNCE_MS` (12_000). Covered by
`anpr-entry.test.ts` + `hikvision-alarm.test.ts` wiring.
Two new env knobs: `VISION_ENTRY_MIN_CONFIDENCE` (0.85), `ANPR_DEBOUNCE_MS` (12_000, keyed by plate
since 2026-10-11). Covered by `anpr-entry.test.ts` + `hikvision-alarm.test.ts` wiring.
**Toggles — two levels (the auto-open is optional).** ANPR auto entry/exit can be turned off without
losing plate recognition:
@@ -83,9 +89,10 @@ losing plate recognition:
> low-confidence misread (observed live on the exit lane: `'111'`@0.20, `'AE18671'`@0.19 …, while
> the manual `test-anpr` on the SAME stopped car read `AA890XX`@1.00). The car then STOPS at the
> barrier waiting for it to open — the stationary, well-framed moment the test reads at ~100%. So
> the bridge now **pulls a FRESH frame every `ANPR_POLL_MS` (1000) and re-runs ANPR until one clears
> the floor, or `ANPR_POLL_WINDOW_MS` (8000) elapses** (car drove off / non-subscriber → give up
> cleanly). One loop per camera (`#polling` set) so the ~1Hz alarm re-fires JOIN it, not spawn N;
> the bridge now **pulls a FRESH frame every `ANPR_POLL_MS` (was 1000; 0 = back-to-back since
> 2026-10-11) and re-runs ANPR until one clears the floor, or `ANPR_POLL_WINDOW_MS` (8000) elapses**
> (car drove off / non-subscriber → give up cleanly). *(Superseded in part by the 2026-10-11 block
> below: the loop no longer stops at the first confident plate.)* One loop per camera (`#polling` set) so the ~1Hz alarm re-fires JOIN it, not spawn N;
> each tick is a fresh `camera.captureSnapshot` (NOT `captureSnapshotShared`, whose TTL would
> re-serve the same bad frame). VERIFIED on hardware: 7 garbage approach frames → `AA890XX`@0.999
> at the barrier → signed `vehicle_exit`. This is what made subscriber **auto-exit** actually work
@@ -106,6 +113,39 @@ losing plate recognition:
> *now*, not the car that started it. (Knobs: `ANPR_POLL_MS`, `ANPR_POLL_WINDOW_MS`,
> `ANPR_POLL_MAX_MS`.)
> **READ EVERY FRAME, SETTLE EVERY PLATE, DEBOUNCE PER PLATE (2026-10-11).** Three defects in the
> loop above, found by measuring park-2 (i5-9500, two `DS-2CD1043G2-LIU`, 48 h of logs):
>
> 1. **The fixed 1 s sleep was the bottleneck, not the hardware.** A main-stream pull is ~0.1 s and a
> `/analyze` ~0.25 s on the booth, so each attempt cost ~0.35 s of work plus a 1 s sleep: one frame
> per ~1.35 s while the host CPU sat under 3 %. Readable moments (a stop at the barrier, a gap in
> headlight glare) are short and were missed between ticks. **Now:** pulls are back-to-back
> (`ANPR_POLL_MS` is a *floor*, default 0) → ~3 frames/s per camera, strictly serial. NOT parallel
> threads: the camera's picture endpoint is single-threaded (4 concurrent GETs → two HTTP 503,
> measured), and the vision service runs inference synchronously on its event loop (4 concurrent
> `/analyze` → each 1.03 s, fully serialised). Two active cameras still queue behind each other in
> the recognizer; making `/analyze` non-blocking (thread pool / 2 workers) is the next step if that
> shows up. Of 557 give-ups in 48 h, 178 had seen a plate below the floor — the ceiling of what
> faster sampling can rescue (~6 % of detections); 379 never saw a plate in any frame.
> 2. **The loop stopped at the first confident plate, then a CAMERA-level debounce dropped every
> push for 12 s.** A tailgater in that window was never looked at: 40 of 57 plate-driven opens had
> a push land 4–12 s after the read. **Now:** the loop runs while pushes keep sliding the deadline
> (or to the hard cap); each confident plate is *settled once per loop* (emitted / `anpr-skip` /
> declined), and the loop goes on — a second car behind the first is read by the same loop. The
> debounce is keyed `deviceId:plate` only; nothing is keyed by camera. The fleet-sub correctness
> argument for the debounce holds unchanged: the same car is the same plate.
> 3. **Credential race, per subscriber.** The mid-poll card/QR guard used to abort the whole loop on
> the first sighted subscriber. It is now per subscription: open-count baselined at first sighting
> of each plate, checked when that plate settles; a moved count declines that plate only.
>
> Side effects: the PASS sample (for the [[vision-review-outbox]]) is one per *plated car* in the loop
> plus one for a plateless loop — before, one per loop however many cars. `ANPR_POLL_WINDOW_MS=0` is
> now honoured (one frame per push; it used to fall back to 8 s silently). Logs: a below-floor plate
> text logs at info once per loop then debug; every loop ends with one `loop done — N frame(s) in M
> ms, P plate(s), E emitted` line. Still open from the same measurement: 59 % of give-ups ended after
> 7–8 frames because the camera went quiet (a motionless car stops re-firing) — extending the window
> when a weak plate has been seen is a candidate follow-up. Tests: `anpr-entry.test.ts` (20).
The goal (narrowed deliberately — see Rejected below): **a subscriber's plate, read by the lane
camera, admits them through the same gated flow a QR/card scan uses.** Scope was cut to subscribers
ONLY — no queue segmentation, no per-car tracking, no make/model, no ticket-button gating.
@@ -137,7 +177,8 @@ snapshot → `vision.analyze` → if a plate clears a **HIGH** confidence floor
present; emitting a read every second would drive REPEAT entries (a fleet sub opens a 2nd
occurrence; a single-car sub spams "already inside") or, on an exit camera, repeat exits
(`sub.refused.noSession` after the first, and FIFO could phantom-close another occurrence). So the
same plate on the same camera within ~10–15 s = ONE credential presentation.
same plate on the same camera within ~10–15 s = ONE credential presentation. (Keyed by PLATE —
the camera-level stamp that also existed until 2026-10-11 made the lane blind to the next car.)
- NB: a direction-bound camera does NOT flip entry↔exit on repeat reads (the barrier's direction
fixes the verb), so the earlier "flip-flop" fear was wrong — but repeat-same-direction is still
bad. Debounce stands.