File size: 2,115 Bytes
c875782
 
3badfec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc5e1ec
05b02db
3badfec
bc5e1ec
3badfec
 
 
 
0f57432
 
3badfec
 
0f57432
3badfec
4c31883
 
0f57432
 
3badfec
05b02db
 
 
 
 
 
 
0f57432
 
4c31883
 
0f57432
05b02db
0f57432
05b02db
 
 
 
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
import gradio as gr
from deepface import DeepFace
import cv2
import numpy as np

def crop_face(image_path):
    # Load the Haarcascade classifier for face detection
    face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')

    # Read the image
    img = cv2.imread(image_path)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # Detect faces in the image
    faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)

    # If a face is detected, crop it
    if len(faces) > 0:
        (x, y, w, h) = faces[0]  # Take the first detected face
        cropped_face = img[y:y+h, x:x+w]
        return cropped_face
    else:
        raise ValueError("No face detected in the image.")

def calculate_similarity(image1, image2):
    # Crop faces from both images
    try:
        face1 = crop_face(image1)
        face2 = crop_face(image2)

        # Convert cropped images to RGB format for DeepFace
        face1_rgb = cv2.cvtColor(face1, cv2.COLOR_BGR2RGB)
        face2_rgb = cv2.cvtColor(face2, cv2.COLOR_BGR2RGB)

        # Analyze the two cropped images for facial similarity
        result = DeepFace.verify(face1_rgb, face2_rgb, model_name='VGG-Face', enforce_detection=False)
        similarity_percentage = (1 - result['distance']) * 100  # Convert distance to similarity percentage
        
        return similarity_percentage, face1_rgb, face2_rgb
    except Exception as e:
        return str(e), None, None

# Create a Gradio interface
iface = gr.Interface(
    fn=calculate_similarity,
    inputs=[
        gr.inputs.Image(type="filepath", label="Image 1"),
        gr.inputs.Image(type="filepath", label="Image 2")
    ],
    outputs=[
        gr.outputs.Textbox(label="Similarity Percentage"),
        gr.outputs.Image(label="Cropped Face 1", type="numpy"),
        gr.outputs.Image(label="Cropped Face 2", type="numpy"),
    ],
    title="Face Similarity Checker",
    description="Upload two images of faces to check their similarity. The faces will be detected and cropped automatically."
)

# Launch the interface
iface.launch()