Files
Behavision/tests/test_index.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

45 lines
1.2 KiB
Python

import numpy as np
import pytest
import behavision.gallery.index as index_mod
from behavision.gallery.index import VectorIndex
def _unit(v):
v = np.asarray(v, dtype=np.float32)
return v / np.linalg.norm(v)
@pytest.fixture(params=["numpy", "faiss"])
def make_index(request, monkeypatch):
if request.param == "numpy":
monkeypatch.setattr(index_mod, "_HAVE_FAISS", False)
elif not index_mod._HAVE_FAISS:
pytest.skip("faiss not installed")
return lambda dim=8: VectorIndex(dim)
def test_empty_index_returns_no_matches(make_index):
idx = make_index()
assert idx.search(_unit(np.ones(8))) == []
def test_add_search_finds_nearest(make_index):
idx = make_index()
a = _unit([1, 0, 0, 0, 0, 0, 0, 0])
b = _unit([0, 1, 0, 0, 0, 0, 0, 0])
idx.add([10, 20], np.vstack([a, b]))
results = idx.search(_unit([0.9, 0.1, 0, 0, 0, 0, 0, 0]), k=2)
assert results[0][0] == 10
assert results[0][1] > results[1][1]
assert results[0][1] == pytest.approx(1.0, abs=0.05)
def test_remove(make_index):
idx = make_index()
a = _unit(np.arange(1, 9))
idx.add([7], a.reshape(1, -1))
idx.remove([7])
assert len(idx) == 0
assert idx.search(a) == []