feat(review): sample every detected car, not only ticketed entries
Build & push images / images (push) Successful in 5m20s
Build & push images / images (push) Successful in 5m20s
The entry stream for the reviewer was fed from the session-tied read only (ticket printed / exit signed). park-2 ran an afternoon with its barriers up: hundreds of camera vehicle pushes, zero tickets, zero samples. - anpr-entry: keep the best-framed vehicle of each poll loop (highest body-type confidence with a box), store that frame as a snapshot (identity pass:<id>, no session) and emit a vehicle read with the camera id — plate or no plate. One sample per detection, fire-and-forget, never on the ANPR path. - snapshot.ts: normalizedBoxes() shared by the session-tied read and the bridge; vehicle reads now carry deviceId. - review-outbox: sampleEntry(deviceId) dedups per camera for 30 s so the pass sample and a ticket entry's read (same car, seconds apart) go once. - wiki: outbox page (entry stream, troubleshooting checklist for 'nothing arrives': the month-long silence was a missing Netbird policy, not code), park-2 camera port-forward procedure (site-device-installation §7), log.
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { devices, deviceEvents as deviceEventsTable, eq, siteConfig, type Db, type DeviceRow } from "@parking/db";
|
||||
import { devices, deviceEvents as deviceEventsTable, eq, siteConfig, snapshots, type Db, type DeviceRow } from "@parking/db";
|
||||
import type { Snapshot } from "@parking/devices";
|
||||
import type { FastifyBaseLogger } from "fastify";
|
||||
import { deviceEvents, type DeviceReadEvent } from "./device-events.js";
|
||||
import { directionOf, type FlowDirection } from "./device-resolve.js";
|
||||
import { buildCamera } from "./snapshot.js";
|
||||
import { buildCamera, encodeForStorage, normalizedBoxes } from "./snapshot.js";
|
||||
import type { SubscriptionFlow } from "./subscription-flow.js";
|
||||
import type { VisionClient } from "./vision-client.js";
|
||||
import type { VisionClient, VisionResult } from "./vision-client.js";
|
||||
|
||||
// The ANPR "bridge": a subscriber's plate, read from the lane camera, admits them through
|
||||
// the SAME gated SubscriptionFlow a QR/card scan uses. It is the one missing wire between
|
||||
@@ -191,6 +192,9 @@ export class AnprBridge {
|
||||
// pushes) is read from #pollDeadline each tick but never allowed past this cap.
|
||||
const hardCap = Date.now() + this.#pollMaxMs;
|
||||
let attempts = 0;
|
||||
// The best-framed vehicle of this loop (highest body-type confidence with a box):
|
||||
// recorded once per detection as a PASS sample, whatever happens to the plate.
|
||||
let best: { shot: Snapshot; r: VisionResult } | null = null;
|
||||
try {
|
||||
// DO-while: a detection always analyzes AT LEAST ONE frame, however loaded the
|
||||
// host — a plain while could zero-iterate if the window elapsed between setting
|
||||
@@ -201,6 +205,7 @@ export class AnprBridge {
|
||||
attempts++;
|
||||
const shot = await camera.captureSnapshot({ direction });
|
||||
const r = await this.#vision.analyze(shot.bytes, shot.contentType);
|
||||
if (r?.vehicle?.bbox && (!best || r.vehicle.confidence > best.r.vehicle!.confidence)) best = { shot, r };
|
||||
|
||||
// Identify the subscriber from ANY readable plate (even below the barrier floor),
|
||||
// and baseline their open count once — so we can detect a credential beating us.
|
||||
@@ -238,6 +243,7 @@ export class AnprBridge {
|
||||
} finally {
|
||||
this.#polling.delete(deviceId);
|
||||
this.#pollDeadline.delete(deviceId);
|
||||
if (best) void this.#recordPass(deviceId, direction, best);
|
||||
}
|
||||
|
||||
if (!result || !result.plate) {
|
||||
@@ -302,6 +308,40 @@ export class AnprBridge {
|
||||
this.#lastFire.set(key, Date.now());
|
||||
}
|
||||
|
||||
/** EVERY car the camera flagged becomes one stored frame + one vehicle read on the bus —
|
||||
* plate or no plate, ticket or no ticket. The read is what the Car Wash module samples
|
||||
* for the reviewer (wiki/concepts/vision-review-outbox.md §entry stream); until this,
|
||||
* only session-tied reads were sampled, so a lot with its barriers up (nobody takes a
|
||||
* ticket) sent nothing however many cars the camera saw. One sample per poll loop, i.e.
|
||||
* per detection, from its best-framed vehicle. Fire-and-forget: never on the ANPR path,
|
||||
* a failure is a log line. Identity `pass:<snapshotId>` — no session to tie it to. */
|
||||
async #recordPass(deviceId: string, direction: FlowDirection, best: { shot: Snapshot; r: VisionResult }): Promise<void> {
|
||||
try {
|
||||
const vehicle = best.r.vehicle;
|
||||
if (!vehicle) return;
|
||||
const { vehicleBox, plateBox } = await normalizedBoxes(best.shot.bytes, best.r);
|
||||
if (!vehicleBox) return;
|
||||
const stored = await encodeForStorage(best.shot, this.#logger);
|
||||
const id = randomUUID();
|
||||
const identity = `pass:${id}`;
|
||||
this.#db
|
||||
.insert(snapshots)
|
||||
.values({ id, direction, deviceId, identity, contentType: stored.contentType, bytes: stored.bytes, capturedAt: best.shot.capturedAt })
|
||||
.run();
|
||||
// plateBox whenever the detector saw a plate, confident or not: it is what the
|
||||
// review crop blurs — privacy, not identity.
|
||||
deviceEvents.emitVehicleRead({
|
||||
identity,
|
||||
direction,
|
||||
deviceId,
|
||||
read: { bodyType: vehicle.bodyType, confidence: vehicle.confidence, snapshotId: id, box: vehicleBox, plateBox },
|
||||
});
|
||||
this.#logger.debug(`anpr-bridge: pass sample ${id} '${vehicle.bodyType}' (${vehicle.confidence.toFixed(3)}) from ${deviceId}`);
|
||||
} catch (err) {
|
||||
this.#logger.warn(`anpr-bridge: pass sample failed for ${deviceId}: ${(err as Error).message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/** Advisory telemetry: a plate was read at the lane but matched no subscription. Not a
|
||||
* read on the bus — just a breadcrumb so the operator can see ANPR is working. */
|
||||
#recordSkip(deviceId: string, plate: string, confidence: number): void {
|
||||
|
||||
Reference in New Issue
Block a user