Download app.py from Jadebks/face-verification-prototype: direct link, hf CLI and curl.
- Browser
- Download file 4.81 kB
-
https://huggingface.co/Jadebks/face-verification-prototype/resolve/main/app.py
- Command line
-
hf download hf://Jadebks/face-verification-prototype/app.py
-
curl -L -o app.py https://huggingface.co/Jadebks/face-verification-prototype/resolve/main/app.py
4.81 kB
| """Hugging Face Space entry point for the Role 3 Gradio prototype. | |
| Same app as app/app.py in the GitHub repository | |
| (jadebeeks/face-verification-real-world-conditions); this file only adds the | |
| Space setup below. | |
| """ | |
| import subprocess | |
| import sys | |
| from pathlib import Path | |
| # facenet-pytorch (MTCNN face detector) pins old torch / numpy / Pillow versions, | |
| # so it is installed without its dependencies, exactly as in the repository README. | |
| try: | |
| import facenet_pytorch # noqa: F401 | |
| except ImportError: | |
| subprocess.check_call([sys.executable, "-m", "pip", "install", "--no-deps", "facenet-pytorch==2.6.0"]) | |
| sys.path.insert(0, str(Path(__file__).parent / "src")) | |
| import matplotlib | |
| matplotlib.use("Agg") # draw charts off-screen inside the web server | |
| import gradio as gr | |
| from face_verification.conditions import MAX_LEVEL | |
| from face_verification.prototype import ( | |
| CONDITION_LABELS, available_models, get_model, prepare, sweep, sweep_figure, verify, | |
| ) | |
| LABEL_TO_KEY = {v: k for k, v in CONDITION_LABELS.items()} | |
| INTRO = """# Face verification under real-world conditions | |
| Upload a **reference photo** (like a passport photo) and a **second photo** of the same or a different person. | |
| The second photo is degraded with the condition you choose, as if captured at a gate in poor conditions. | |
| The model compares the two faces and decides **same person** or **different people** using a threshold | |
| chosen on validation people. | |
| *Research prototype for a university project (IE University, AI/ML): not an identity-check system. Uploaded photos are used only to compute the comparison and are deleted from the server within an hour. Please only upload photos you have the right to use.*""" | |
| def on_upload(img): | |
| """Detect and crop a face once per upload; keep it in state.""" | |
| if img is None: | |
| return None, None | |
| crop, landmarks, warning = prepare(img) | |
| return {"crop": crop, "landmarks": landmarks}, (warning or "") | |
| def run(state1, state2, model_name, condition_label, level): | |
| if state1 is None or state2 is None: | |
| return None, None, "Upload both photos to compare them.", None | |
| if not model_name: | |
| return None, None, "No trained model found. See README: train the baseline first.", None | |
| model = get_model(model_name) | |
| cond = LABEL_TO_KEY[condition_label] | |
| level = int(level) | |
| r = verify(model, state1["crop"], state2["crop"], cond, level, state2["landmarks"]) | |
| scores = sweep(model, state1["crop"], state2["crop"], cond, state2["landmarks"]) | |
| verdict = "✅ **Same person**" if r["same"] else "❌ **Different people**" | |
| margin = r["score"] - r["threshold"] | |
| text = (f"### {verdict}\n" | |
| f"Score **{r['score']:.3f}** vs threshold **{r['threshold']:.3f}** " | |
| f"(margin {margin:+.3f}) \n" | |
| f"Condition on photo 2: {condition_label}, level {level} ({r['condition_value']}) \n" | |
| f"Model: {model_name}") | |
| fig = sweep_figure(scores, r["threshold"], level, cond) | |
| return state1["crop"], r["degraded"], text, fig | |
| def build() -> gr.Blocks: | |
| models = available_models() | |
| with gr.Blocks(title="Face verification under real-world conditions", | |
| delete_cache=(1800, 3600)) as demo: | |
| gr.Markdown(INTRO) | |
| state1, state2 = gr.State(None), gr.State(None) | |
| with gr.Row(): | |
| with gr.Column(): | |
| img1 = gr.Image(label="Photo 1: reference (kept clean)", type="numpy") | |
| warn1 = gr.Markdown() | |
| with gr.Column(): | |
| img2 = gr.Image(label="Photo 2: live capture (gets the condition)", type="numpy") | |
| warn2 = gr.Markdown() | |
| with gr.Row(): | |
| model_dd = gr.Dropdown(models, value=models[0] if models else None, label="Model") | |
| cond_dd = gr.Dropdown(list(CONDITION_LABELS.values()), value=list(CONDITION_LABELS.values())[0], | |
| label="Condition on photo 2") | |
| level_sl = gr.Slider(0, MAX_LEVEL, value=0, step=1, label="Strength (0 = none, 5 = strongest)") | |
| result = gr.Markdown() | |
| with gr.Row(): | |
| crop1_out = gr.Image(label="Face 1 as the model sees it", height=220) | |
| crop2_out = gr.Image(label="Face 2 with the condition", height=220) | |
| sweep_out = gr.Plot(label="Score at every strength level (above the dashed line = same person)") | |
| inputs = [state1, state2, model_dd, cond_dd, level_sl] | |
| outputs = [crop1_out, crop2_out, result, sweep_out] | |
| img1.change(on_upload, img1, [state1, warn1]).then(run, inputs, outputs) | |
| img2.change(on_upload, img2, [state2, warn2]).then(run, inputs, outputs) | |
| for ctrl in (model_dd, cond_dd, level_sl): | |
| ctrl.change(run, inputs, outputs) | |
| return demo | |
| if __name__ == "__main__": | |
| build().launch() | |