martinbadrous's picture
Upload 11 files
8a4d3a7 verified
raw
history blame
1.61 kB
"""
Gradio demo for facial verification.
This script exposes a web interface where users can upload two images and
receive immediate feedback about whether the faces match. It utilises
MTCNN for face detection and InceptionResnetV1 for feature extraction via
the utilities defined in ``src``.
"""
import gradio as gr
from PIL import Image
from src.verify_faces import verify_images
def verify_fn(img1: Image.Image, img2: Image.Image) -> str:
"""Wrap the verification function for Gradio.
Parameters
----------
img1, img2: PIL.Image.Image
Input images from the user interface.
Returns
-------
str
A human‑readable message indicating whether the faces match and the
similarity score.
"""
# Run verification. We rely on CPU to keep the demo accessible on free tier.
similarity, is_same, message = verify_images(img1, img2, threshold=0.8, device="cpu")
if similarity is None:
return message
return f"{message}\nCosine similarity: {similarity:.3f}"
demo = gr.Interface(
fn=verify_fn,
inputs=[gr.Image(type="pil", label="Image 1"), gr.Image(type="pil", label="Image 2")],
outputs=gr.Textbox(label="Result"),
title="Facial Recognition Verification",
description=(
"Upload two face images to verify if they belong to the same person. "
"We use a pretrained FaceNet model to extract 512‑dimensional embeddings "
"and compute their cosine similarity. A similarity above 0.8 indicates a match."
),
allow_flagging="never",
)
if __name__ == "__main__":
demo.launch()