From 8fb021bc2b3f1896dcdd3a4fa04df7ea696bc541 Mon Sep 17 00:00:00 2001 From: perco Date: Wed, 3 Jun 2026 17:15:56 +0200 Subject: [PATCH] Fix snapshot NameError and crop zone bbox for PlateRecognizer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add missing subprocess import in main.py (snapshot was crashing). When a zone is defined, crop each frame to the zone's bounding box before sending to PlateRecognizer — smaller payload, faster upload, recognition focused on the relevant area only. Co-Authored-By: Claude Sonnet 4.6 --- app/main.py | 1 + app/watcher.py | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index 3917ad8..29f185d 100644 --- a/app/main.py +++ b/app/main.py @@ -5,6 +5,7 @@ import logging import threading import tempfile import asyncio +import subprocess from contextlib import asynccontextmanager from fastapi import FastAPI, Request, Query, HTTPException, UploadFile, File, Form from fastapi.responses import HTMLResponse, RedirectResponse diff --git a/app/watcher.py b/app/watcher.py index a48deaf..1c1634f 100644 --- a/app/watcher.py +++ b/app/watcher.py @@ -282,10 +282,23 @@ def _process_clip(event_id: str, event_dir: str, clip_path: str, camera_name: st # LPR: PlateRecognizer API (top 3 frames) → fallback to local plate, conf, plate_bbox, plate_corrected = ("", 0.0, None, None) + + # If a zone is defined, crop frames to its bounding box before sending to API + # → smaller payload, faster transfer, better focus for the recognizer + def _zone_crop(f: np.ndarray) -> np.ndarray: + if not zone_pts: + return f + fh, fw = f.shape[:2] + xs = [int(x * fw) for x, y in zone_pts] + ys = [int(y * fh) for x, y in zone_pts] + x1c, x2c = max(0, min(xs)), min(fw, max(xs)) + y1c, y2c = max(0, min(ys)), min(fh, max(ys)) + return f[y1c:y2c, x1c:x2c] if x2c > x1c and y2c > y1c else f + if PLATERECOGNIZER_KEY: from lpr import call_platerecognizer for _, frame, _ in scored[:3]: - p, c = call_platerecognizer(frame, PLATERECOGNIZER_KEY) + p, c = call_platerecognizer(_zone_crop(frame), PLATERECOGNIZER_KEY) p = _normalize_plate(p) if p and c > conf: plate, conf = p, c