#!/usr/bin/env python3 """ Evaluate the same face / liveness gate used in production (DeepFace Facenet + anti_spoofing when supported). Uses **your own labeled images** (not database seed data): dataset_root/ live/ # images you label as genuine live capture spoof/ # images you label as attack (print, phone screen, mask, wrong person, etc.) A sample is counted as **predicted "live accepted"** when `services.face_service._get_face_embedding` returns an embedding (face found + passes anti-spoof when DeepFace exposes `is_real`). Run from the `backend` directory (use a REAL path, not the example): python scripts/evaluate_face_liveness.py --dataset D:\\my_eval\\face_dataset # or any folder names you like: python scripts/evaluate_face_liveness.py --live D:\\my_eval\\real --spoof D:\\my_eval\\attacks Optional second check (OpenCV Haar, same idea as `_face_check` sampling): python scripts/evaluate_face_liveness.py --dataset ... --haar-face-present """ from __future__ import annotations import argparse import sys from pathlib import Path # Resolve imports as when running from backend/ _BACKEND = Path(__file__).resolve().parent.parent if str(_BACKEND) not in sys.path: sys.path.insert(0, str(_BACKEND)) IMG_EXT = {".jpg", ".jpeg", ".png", ".bmp", ".webp"} def _list_images(folder: Path) -> list[Path]: if not folder.is_dir(): return [] out: list[Path] = [] for p in sorted(folder.iterdir()): if p.is_file() and p.suffix.lower() in IMG_EXT: out.append(p) return out def _confusion_counts(y_true_live: list[bool], y_pred_live: list[bool]) -> tuple[int, int, int, int]: """Returns TP, TN, FP, FN where "positive" class = live (real).""" tp = tn = fp = fn = 0 for t, p in zip(y_true_live, y_pred_live): if t and p: tp += 1 elif t and not p: fn += 1 elif not t and p: fp += 1 else: tn += 1 return tp, tn, fp, fn def _metrics(tp: int, tn: int, fp: int, fn: int) -> dict: n = tp + tn + fp + fn acc = (tp + tn) / n if n else 0.0 prec = tp / (tp + fp) if (tp + fp) else 0.0 rec = tp / (tp + fn) if (tp + fn) else 0.0 spec = tn / (tn + fp) if (tn + fp) else 0.0 f1 = (2 * prec * rec / (prec + rec)) if (prec + rec) else 0.0 return { "n": n, "accuracy": acc, "precision_live": prec, "recall_live": rec, "specificity_spoof": spec, "f1_live": f1, } def _print_matrix(tp: int, tn: int, fp: int, fn: int) -> None: print() print("Confusion matrix (rows = actual, cols = predicted)") print(" pred NOT live pred live") print(f" actual NOT live (spoof) {tn:5d} {fp:5d}") print(f" actual live {fn:5d} {tp:5d}") print() print("Legend:") print(" TP live & accepted as live") print(" TN spoof & rejected (no embedding / blocked)") print(" FP spoof but accepted as live (security risk)") print(" FN live but rejected (bad UX / false block)") print() def _haar_face_present(image_path: Path) -> bool: import cv2 frame = cv2.imread(str(image_path)) if frame is None: return False cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml") gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = cascade.detectMultiScale(gray, 1.1, 4, minSize=(60, 60)) return len(faces) >= 1 def main() -> int: ap = argparse.ArgumentParser( description="Face liveness / embedding gate - confusion matrix on labeled folders" ) ap.add_argument( "--dataset", type=Path, default=None, help="Root folder that contains subfolders live/ and spoof/ (or use --live + --spoof instead)", ) ap.add_argument( "--live", type=Path, default=None, help="Folder of genuine live-capture images (alternative to dataset/live/)", ) ap.add_argument( "--spoof", type=Path, default=None, help="Folder of spoof / attack images (alternative to dataset/spoof/)", ) ap.add_argument( "--haar-face-present", action="store_true", help="Also run OpenCV Haar 'face present' check (different from DeepFace liveness)", ) args = ap.parse_args() live_dir: Path | None = None spoof_dir: Path | None = None if args.live is not None and args.spoof is not None: live_dir = args.live.expanduser().resolve() spoof_dir = args.spoof.expanduser().resolve() elif args.live is not None or args.spoof is not None: print( "ERROR: use both --live