Download src/face_verification/prototype.py from Jadebks/face-verification-prototype: direct link, hf CLI and curl.
- Browser
- Download file 5 kB
-
https://huggingface.co/Jadebks/face-verification-prototype/resolve/main/src/face_verification/prototype.py
- Command line
-
hf download hf://Jadebks/face-verification-prototype/src/face_verification/prototype.py
-
curl -L -o prototype.py https://huggingface.co/Jadebks/face-verification-prototype/resolve/main/src/face_verification/prototype.py
5 kB
| """Role 3 — logic behind the Gradio prototype (the UI itself is app/app.py). | |
| Flow, matching the experiments (docs/04_role3_baseline_prototype.md): | |
| photo 1 -> preprocess -> crop 1 (kept clean: the "passport photo") | |
| photo 2 -> preprocess -> crop 2 -> apply_condition(level) (the "live capture") | |
| model.score(crop 1, crop 2) vs the model's validation threshold -> decision | |
| Models are registered in MODEL_REGISTRY. A model is listed in the app only when | |
| its files exist, so Roles 4 and 5 can add the triplet model and the FaceNet | |
| reference by adding one entry each. | |
| """ | |
| from __future__ import annotations | |
| from dataclasses import dataclass | |
| from typing import Callable | |
| import numpy as np | |
| from .conditions import LEVELS, MAX_LEVEL, apply_condition, describe, preprocess_with_info | |
| from .lfw_io import ROOT | |
| # Friendly names for the app (keys are Role 2's condition names). | |
| CONDITION_LABELS = { | |
| "brightness_low": "Low light (darker)", | |
| "brightness_high": "Overexposed (brighter)", | |
| "contrast_low": "Low contrast (haze)", | |
| "occlusion_eyes": "Eyes covered (e.g. sunglasses)", | |
| "occlusion_lower": "Lower face covered (e.g. mask)", | |
| "occlusion_patch": "Random patch covered", | |
| } | |
| assert set(CONDITION_LABELS) == set(LEVELS), "update CONDITION_LABELS when conditions change" | |
| class ModelEntry: | |
| name: str | |
| files: tuple # paths that must exist for the model to be listed | |
| load: Callable # () -> object with .score(crop1, crop2) and .threshold | |
| def available(self) -> bool: | |
| return all((ROOT / f).exists() for f in self.files) | |
| def _load_baseline(): | |
| from .models.baseline import HogSvmBaseline | |
| return HogSvmBaseline.load(ROOT / "results/models/baseline_fold0.joblib") | |
| MODEL_REGISTRY: list[ModelEntry] = [ | |
| ModelEntry("HOG + SVM baseline (fold 0)", ("results/models/baseline_fold0.joblib",), _load_baseline), | |
| # Role 4: ModelEntry("Triplet ResNet-18 (fold 0)", (...,), _load_triplet), | |
| # Role 5: ModelEntry("FaceNet reference (CASIA-WebFace)", (...,), _load_facenet), | |
| ] | |
| _LOADED: dict[str, object] = {} | |
| def available_models() -> list[str]: | |
| return [m.name for m in MODEL_REGISTRY if m.available()] | |
| def get_model(name: str): | |
| if name not in _LOADED: | |
| entry = next(m for m in MODEL_REGISTRY if m.name == name) | |
| _LOADED[name] = entry.load() | |
| return _LOADED[name] | |
| # --------------------------------------------------------------------------- | |
| def prepare(img: np.ndarray) -> tuple[np.ndarray, list | None, str | None]: | |
| """Detect and crop a face. Returns (crop, landmarks, warning or None).""" | |
| crop, info = preprocess_with_info(np.asarray(img)) | |
| warning = None | |
| if info["fallback"]: | |
| warning = "No face detected: a centre crop is used instead, so the result is unreliable." | |
| elif info["n_faces"] > 1: | |
| warning = f"{info['n_faces']} faces found: the one closest to the centre is used." | |
| return crop, info["landmarks"], warning | |
| def degrade(crop: np.ndarray, condition: str, level: int, landmarks=None) -> np.ndarray: | |
| return apply_condition(crop, condition, int(level), landmarks=landmarks) | |
| def verify(model, crop1, crop2, condition: str, level: int, landmarks2=None) -> dict: | |
| """Score crop 1 (clean) against crop 2 under `condition` at `level`.""" | |
| degraded = degrade(crop2, condition, level, landmarks2) | |
| score = float(model.score(crop1, degraded)) | |
| return {"score": score, "threshold": float(model.threshold), | |
| "same": score >= model.threshold, "degraded": degraded, | |
| "condition_value": describe(condition, int(level))} | |
| def sweep(model, crop1, crop2, condition: str, landmarks2=None) -> list[float]: | |
| """Score at every severity level 0..MAX_LEVEL of one condition.""" | |
| return [float(model.score(crop1, degrade(crop2, condition, lv, landmarks2))) | |
| for lv in range(MAX_LEVEL + 1)] | |
| def sweep_figure(scores: list[float], threshold: float, level: int, condition: str): | |
| """Line chart of score vs severity with the decision threshold.""" | |
| import matplotlib.pyplot as plt | |
| blue, ink2, grid = "#2a78d6", "#52514e", "#e4e3df" | |
| levels = list(range(len(scores))) | |
| fig, ax = plt.subplots(figsize=(9, 3.4), dpi=110) | |
| ax.axhline(threshold, color=ink2, lw=1.2, ls="--") | |
| ax.text(levels[-1], threshold, " threshold", va="bottom", ha="right", fontsize=8, color=ink2) | |
| ax.plot(levels, scores, color=blue, lw=2, marker="o", ms=6) | |
| ax.plot([level], [scores[level]], marker="o", ms=11, mfc="none", mec=blue, mew=2) | |
| ax.set_xticks(levels, [f"L{l}\n{describe(condition, l)}" for l in levels], fontsize=8) | |
| ax.set_ylabel("Similarity score", color=ink2) | |
| ax.set_xlabel(CONDITION_LABELS[condition], color=ink2, fontsize=9) | |
| for s in ("top", "right"): | |
| ax.spines[s].set_visible(False) | |
| for s in ("left", "bottom"): | |
| ax.spines[s].set_color(grid) | |
| ax.tick_params(colors=ink2) | |
| ax.grid(axis="y", color=grid, lw=0.6) | |
| fig.tight_layout() | |
| return fig | |