File size: 1,606 Bytes
8a4d3a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
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()