Files
parking_solution/wiki/concepts/zone-anpr-poc-review.md
T
julian dd2dd53d7e wiki: H.264 test on park-lab, event-driven refactor decided
- zone-anpr-poc: H.264 instead of MJPEG (load, delay, a car in the zone,
  staff traffic, bad weather); the relay-less test copy is left running
- event-driven-refactor (new): the user's decisions of 2026-11-23, what
  is not decided, what they touch in today's code; waits for the user's
  guidelines
- passage-confirmation: the 2026-11-19 phase order superseded; where the
  video work lives
- vision-hardening, zone-anpr-poc-review: on-screen label read as a
  plate (moved by the user), day-time oversized box, still pause carried
  over
- pointers on vision-service, camera-live-view, lane-presence-and-anpr-entry,
  dingtian-vs-mqtt, entry gates, standing decisions, open questions,
  overview, index

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RZgP5oZdzvo92Eouc9eRv5
2026-11-23 12:02:18 +01:00

206 lines
13 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
type: concept
tags: [parking, vision, anpr, passage, relay, poc, park-lab, review]
sources: []
updated: 2026-11-23
---
# Zone ANPR PoC — code review (2026-11-21)
A review of `poc/zone-anpr/zone_anpr.py` as it stood on 2026-11-21 (uncommitted), against the
design on [[passage-confirmation]] and the trial record on [[zone-anpr-poc]].
**How it was checked.** The tracking logic (`Camera.step`) was replayed offline with a scripted
detector and a recording relay stub, on the entry and exit zones and lines from the local
`config.json` (2026-11-20). No camera, relay or park-lab data was touched. Each finding says
whether it was **replayed**, found **by reading**, or is a **hypothesis**. The live park-lab
config and its `visits.jsonl`, `relays.jsonl` and clips were not read.
The two cases the night drive judged right replay correctly: a drive-through gives `passed` with
no close, and an approach followed by a reverse gives `abandoned` with a close.
## The close decision (R2)
The verdict is computed from where the tracked car was **last seen**. It is not told **how the
visit ended**. Everything below follows from that.
| Precondition on [[passage-confirmation]] | In the PoC |
|---|---|
| An abandoned verdict | checked |
| Nothing between the line and the boom, nothing approaching | **not checked**: only the tracked car's own share of the zone |
| No new open since | **not checked**: `Relays.last_open` is written and never read |
| The watch is alive (stream up, verdict fresh) | **not checked**: a car that is no longer detected counts as gone |
| Clear on every camera bound to the barrier | **not checked**: the other camera is not asked |
| Vision down ⇒ no close | **inverted**: losing the car produces a close |
The user's rule (2026-11-21) removed the dwell and the window. It did not remove these.
1. **A lost car is judged abandoned (replayed).** A car approaches on side A and stands in the
zone. The detector stops reporting it for 3 s. The visit ends, the verdict is `abandoned`, and
R2 fires with the car standing in front of the boom. On the next frame the car is seen again,
and R1 opens. The table on [[zone-anpr-poc]] promises "unclear" for a lost car; that holds only
when the car was last seen on the line. Item 8 there records 3 s drop-outs of a standing car in
glare at the old 0.4 threshold. How often they still happen at 0.15 is not known.
2. **A track that breaks is judged abandoned (replayed).** If the car's box moves so far between
two frames that it no longer overlaps the last one, the tracker counts misses for 3 s and
ignores the car, although it is detected on every frame on side B. Result: `abandoned`, R2,
then a second R1.
3. **"Zone free" means the tracked car left, not that the zone is empty (replayed).** Car 1
reverses out while car 2 stands in the zone on the far side of the line, waiting to drive out.
R2 fires. One frame (200 ms) later car 2 starts a visit and R1 fires. Each pulse is sent from
its own thread, so the order on the wire is not fixed, and no rule makes the open win.
4. **The track can jump to another vehicle (replayed).** A visit continues on any detection that
scores ≥ 0.15 and overlaps the last box by ≥ 0.15. Car 1 is on the line, car 2 is close behind
on side A, car 1 is missing from one frame: the track moves to car 2 and stays there, although
car 1 is reported again on every later frame. Car 2 backs out, and R2 fires while car 1 is
detected on the line. R1 follows on the next frame.
5. **A car first seen near the line that turns back is "moving away" (replayed).** R1 opened for
it, it reversed, and the boom stays up. `approached` needs the car to have come 3% of the frame
closer after the visit started. At night the visit starts late. This is the free pass the
feature exists to prevent.
6. **Opens by anyone else are invisible (by reading).** The dev machine drives the same board
([[passage-confirmation]], park-lab rig). The PoC cannot tell that R1 was opened by another
controller just before it closes. `Relays.status()` reads the board's relay state and is never
called.
## The lane lock
7. **A second open when the car reaches the far zone late (replayed).** The far camera yields
only if it sees the car **while** the owner's visit is running. The entry visit ends 1 s after
the car leaves the entry zone. A car that reaches the exit zone after that gets a visit and a
second R1.
8. **One weak frame cancels the yield (replayed).** The yield is cleared by a single frame with
no candidate at ≥ 0.4. A car in the exit zone that scores 0.35 for one frame is a new visit on
the next, with a second R1.
9. **A car waiting on the far side is never served (replayed).** A car that stands in the exit
zone while the entry camera owns the lane stays ignored after the owner lets go, for as long as
it stands there: 60 s in the replay, with no open.
## Liveness and timing
10. **Time does not count, only processed frames (replayed).** After a 60 s gap in frames the
visit continues on whatever stands in the same place. A visit has no maximum length, so a
vehicle parked in the zone holds the lane lock until it leaves.
11. **The timers assume 5 detections a second (measured).** On the dev machine on 2026-11-20 the
detector ran at 2.5–2.8 a second for 27 minutes (360–400 ms a detection, `out/metrics.jsonl`).
"Lost for 3 s" was then about 6 s and "zone free for 1 s" about 2 s.
12. **An empty zone spins a core (replayed).** With fewer than three zone points the worker loop
never advances its frame counter: 2.6 million iterations in 2 s, one full core per camera.
`config.example.json` ships with empty zones.
## Bottlenecks
The numbers are from the dev machine on 2026-11-20 (`out.log`, `out/metrics.jsonl`), with both
cameras on their main streams in `stream` mode. park-lab runs the 1280×720 MJPEG streams and the
iGPU ([[zone-anpr-poc]]), so its numbers differ and were not read.
- **Decoding costs more than detecting.** With an empty lane the process used about 2.2 cores and
760 MB. `stream` mode decodes every frame of a 3840×2160 and a 2560×1440 stream to keep the
newest one. `mjpeg` mode decodes only the frames it checks. The local `config.json` still
starts the expensive mode.
- **One detector, one lock, all cameras (by reading).** Each camera's frame waits for the
other's. The measured detection time includes that wait: in the slow period both cameras
reported the same time to the millisecond (357.7 and 357.5 ms).
- **The detector never rests.** Both cameras are checked five times a second with an empty lane.
The design on [[passage-confirmation]] runs nothing until a motion push. A lower rate while no
visit is running would halve the idle load.
- **Disk writes sit on the tracking thread:** one JPEG for every processed frame of a visit, and
the verdict image at the end. A slow disk delays the next frame's verdict.
- **The frame counters stretch** when any of the above slows the loop (finding 11).
## Plate stills
13. **The still opener can die for good, with the message `digest auth failed` (mechanism
replayed; that it is what happened on park-lab is a hypothesis).** The opener is built once
per camera. Python's digest handler counts a retry whenever the authenticated request raises
(a 503 or a timeout), and resets the count only on success. After six such failures in a row
it refuses every later request **without sending credentials**. In the replay against a mock
camera: six failures, then `401 digest auth failed` on every attempt, with the camera healthy
again and no authenticated request reaching it. Plate reads stay dead until the process
restarts, and the back-off does not cure it.
- This fits item 5 on [[zone-anpr-poc]]: 503s first, then `digest auth failed` at the frame
rate.
- A camera that really answers 401 to an authenticated request ends in the same message, so
the park-lab log alone does not tell the two apart. In that case the lockout risk recorded
there stands.
- What tells them apart in the container log: exactly six 503 or timeout failures before the
first `digest auth failed`, and the video stream still logging in at the time.
14. **The back-off carries over to the next car (by reading).** `read_pause_until` is not reset
when a visit ends. A 30 s pause from one car covers the 1–2 s approach of the next, which is
the only time the plate reads at night.
15. **Stills are pulled back to back with no cap (by reading).** A car that stands with an
unreadable plate draws a hi-res still as fast as the camera answers, for the whole stay.
16. **Cause of the night hangs (hypothesis).** [[lpr-camera]] records that a second concurrent
still request draws a 503. The dev machine's app pulls stills for the same car at the same
time. The PoC's own back-to-back pulls add to it.
## Smaller items (by reading)
- **Web page.** It listens on all interfaces with no login. `POST /arm` arms the real open relay,
and `POST /zone` and `POST /line` redraw what opens it.
- **Relays.** The board's reply is stored but not checked. `opened` is set when the pulse was a
dry run or failed.
- **Disk.** Clips are pruned by count of visits, not by size, and a visit has no frame cap.
`saves/` and the `.jsonl` files are never pruned. `/status` reads three whole files every
700 ms for each open page, without the write lock.
- **Clip and verdict images are written on the tracking thread.**
- **Credentials.** `${VAR}` is pasted into the URL unencoded, so a password containing `@`, `/` or
`:` breaks the URL, and `safe_url` does not mask a password containing `/`.
- **RTSP client.** The socket is not closed on an error, no `TEARDOWN` is sent, the body read has
no end-of-stream check, `DESCRIBE` is sent one time too many, and the RTP extension bit is
ignored.
- **Dead code.** `inside()`, the `xs`/`ys` lists in `step()`, `Relays.last_open`,
`Relays.status()`, `onvif.py`.
- **Stale text.** The module docstring and the page heading still say "log only, no barrier
action". `bench-parklab.sh` names containers that no longer exist. `.gitignore` misses
`out.log`, `__pycache__/` and `models/`.
## Status after the 2026-11-22 builds
The PoC was changed three times on 2026-11-22 ([[zone-anpr-poc]], "The 2026-11-22 build" and
"Since the first build"). What is still open is collected on [[vision-hardening]].
| Finding | Now |
|---|---|
| 1 lost car judged abandoned | `unclear`, no close (default; `close_on_lost` reverses it) |
| 2 broken track, 4 track jumps to another car | tracker unchanged; no close can go out while a vehicle is in the zone |
| 3 "zone free" ignores other cars | fixed: busy is any vehicle in the zone, checked again before the close is sent |
| 5 "moving away" leaves the boom up | fixed: an uncrossed open is abandoned |
| 6 opens by others invisible | partly: the board's status is read before every pulse |
| 7–9 lane lock | open |
| 10–12 liveness and timing | open, except that a close needs a frame under 1 s old |
| 13 the still client dies for good | fixed: a new client for every still |
| 14 the back-off carries over to the next car | open; seen live on 2026-11-23 (an 8 s pause from timeouts twenty minutes earlier) |
| 15 stills back to back with no cap | fixed: at most one every 0.5 s, capped while the car stands |
| 16 cause of the still hangs | open; still seen, by day and by night |
| One frame at the end decides the verdict (found by reading, not replayed then) | seen for real on 2026-11-22 night; fixed: a side counts after two looks |
| Relay pulses on unordered threads; dead `last_open`, `status()` | fixed |
| Other smaller items | open |
## What to change, in order
1. **Give the verdict the end reason.** `left the zone`, `left the frame at an edge` and `lost`
are three different endings. Only the first two may give `abandoned`. `lost` is `unclear`.
2. **Close only on an empty zone.** At close time: no detection of any vehicle in the zone on any
camera bound to the barrier, the last frame younger than about 1 s, and no open since the
visit's own. This adds no dwell.
3. **"Moving away" is gone (user, 2026-11-22).** An open that the vehicle does not cross is
abandoned, whatever the vehicle did before: the flow on [[passage-confirmation]]. That
settles finding 5. Changes 1 and 2 are still needed under the new flow, and are listed there
as not settled.
4. **Re-acquire before giving up.** When the overlap match fails, take the nearest detection in
the zone before counting a miss, and refuse a match that jumps to a box already present on the
previous frame.
5. **Add a wall-clock limit beside the frame counts.** Counting in frames was deliberate, so that
a stall does not end a visit. Keep it, and end the visit as `unclear` when the newest frame is
older than a few seconds.
6. **Make the lane lock a short hold after the owner's visit ends,** and clear a yield only after
several empty frames in a row.
7. **Build a fresh opener for each still,** reset the back-off at the start of a visit, and cap
stills at about two a second.
8. **Keep the replay as a regression test** next to the PoC.
To confirm on park-lab data: how many visits ended `abandoned` with a path of only `a` and no
frame of the car leaving the zone (findings 1 and 2), and whether `digest auth failed` lines stop
only at a container restart (finding 13).