Fix NameError in test-lpr endpoint (PLATERECOGNIZER_KEY scope)

PLATERECOGNIZER_KEY is defined in watcher.py, not main.py. Reference it
via watcher.PLATERECOGNIZER_KEY inside the executor closure, and wrap
_run() in a top-level try/except so exceptions always return JSON instead
of a 500 HTML page.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
perco
2026-06-03 16:44:15 +02:00
co-authored by Claude Sonnet 4.6
parent c43652d187
commit d6f95131bc
+12 -4
View File
@@ -325,17 +325,25 @@ async def test_lpr(event_id: str, frame_path: str = Form(""), bbox: str = Form("
raise HTTPException(404, "Frame introuvable")
def _run():
try:
return _run_inner()
except Exception as e:
logging.exception("test-lpr unhandled error")
return {"error": str(e)}
def _run_inner():
frame = cv2.imread(frame_abs)
if frame is None:
return {"error": "Impossible de lire l'image"}
result: dict = {"has_pr": bool(PLATERECOGNIZER_KEY)}
pr_key = watcher.PLATERECOGNIZER_KEY
result: dict = {"has_pr": bool(pr_key)}
if PLATERECOGNIZER_KEY:
if pr_key:
from lpr import call_platerecognizer
from watcher import _normalize_plate
p, c = call_platerecognizer(frame, PLATERECOGNIZER_KEY)
p, c = call_platerecognizer(frame, pr_key)
if p:
from watcher import _normalize_plate
result["platerecognizer"] = {
"plate": _normalize_plate(p) or p,
"raw": p,