Files
Behavision/tests/test_detector_concurrency.py
Suriyakumarvijayanayagam dad04e8cda Behavision: face recognition for retail, edge to head office
Five components that ship as one product:

- behavision/  the recognition engine. RTSP ingest, YuNet detection, IoU
               tracking, ArcFace embeddings, a FAISS/SQLite gallery, and a
               FastAPI dashboard. Identity is decided once per TRACK from an
               average of at least three embeddings, never per frame.
- agent/       the Go edge agent: supervises the engine, holds a durable
               spool, and drains it to MQTT. Nothing is acked before the
               broker confirms.
- desktop/     the shop PC application (Wails + React + tray).
- server/      the cloud API, MQTT consumer, reports and assistant.
- web/         platform.loyaly.ai, the head-office app, embedded in the
               server binary.

The gallery stores 512-float embeddings and timestamps - no images unless
`app.store_faces` is switched on. Those embeddings are biometric personal
data under GDPR and India's DPDP: template inversion reconstructs a
recognisable face from an ArcFace vector, so data/behavision.db is treated
as a biometric database and DELETE /api/visitors/{id} is a real erasure.

CLAUDE.md carries the reasoning behind every non-obvious decision here,
including the ones that were measured and the ones that were wrong first.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HViLj9gYNRtSr7YVZmW5sn
2026-09-04 11:14:18 +05:30

56 lines
1.7 KiB
Python

"""cv2.FaceDetectorYN caches its input size and is not thread-safe, so camera
workers must not share one. Skipped when the model is absent, matching the
faiss-optional pattern in test_index.py — the suite stays runnable with no
models installed."""
import threading
from pathlib import Path
import numpy as np
import pytest
from behavision.config import Config
from behavision.detection import YUNET_FILENAME, FaceDetector
MODELS = Path(__file__).resolve().parent.parent / "models"
pytestmark = pytest.mark.skipif(not (MODELS / YUNET_FILENAME).exists(),
reason="YuNet model not installed")
def _detector():
d = Config().detection
return FaceDetector(MODELS, d.score_threshold, d.nms_threshold,
d.max_faces, d.min_face_px)
def _hammer(det, size, errors, n=40):
w, h = size
frame = np.zeros((h, w, 3), dtype=np.uint8)
for _ in range(n):
try:
det.detect(frame)
except Exception as exc: # noqa: BLE001 - cv2 raises on size mismatch
errors.append(exc)
return
def _race(det_a, det_b):
errors = []
threads = [threading.Thread(target=_hammer, args=(det_a, (1280, 720), errors)),
threading.Thread(target=_hammer, args=(det_b, (640, 480), errors))]
for t in threads:
t.start()
for t in threads:
t.join()
return errors
def test_a_shared_detector_really_does_race():
"""Guards the premise: if this ever stops failing, the test below is
proving nothing and the per-camera split can be revisited."""
shared = _detector()
assert _race(shared, shared), "expected a shared detector to race"
def test_per_camera_detectors_do_not_race():
assert _race(_detector(), _detector()) == []