File size: 4,999 Bytes
be666c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
"""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"


@dataclass
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