File size: 3,458 Bytes
4b715e4
 
 
 
 
 
 
 
 
 
e984ef0
4b715e4
e984ef0
 
4b715e4
e984ef0
 
4b715e4
e984ef0
 
 
 
 
 
 
0785b71
f311802
e984ef0
f311802
e984ef0
 
f311802
 
6329875
e984ef0
f311802
 
 
 
 
 
 
 
 
 
 
e984ef0
 
f311802
4b715e4
f311802
4b715e4
 
 
e984ef0
 
 
0785b71
6329875
4b715e4
 
 
f311802
4b715e4
0785b71
4b715e4
 
6329875
ede3d98
4b715e4
0785b71
e984ef0
0785b71
4b715e4
 
 
e984ef0
4b715e4
 
e984ef0
4b715e4
0785b71
4b715e4
e984ef0
0785b71
4b715e4
 
 
 
f0f598b
3539a90
4b715e4
 
 
 
 
 
 
c35097b
4b715e4
 
 
 
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
import gradio as gr
import numpy as np
import cv2
import insightface
from insightface.app import FaceAnalysis

# Global variable to store the InsightFace app instance
app = FaceAnalysis(providers=['CPUExecutionProvider'])
app.prepare(ctx_id=0, det_size=(640, 640))

def liveness_check_passive(image):
    """
    Checks if the face in the image is a live person.
    Returns True for live, False for spoof.
    """
    # Placeholder for a liveness detection model.
    return True

def liveness_check_active(video_path):
    """
    Performs an active liveness check on a video feed.
    A real implementation would check for user actions like blinks or head turns.
    """
    # Placeholder for active liveness detection logic.
    return True

def run_face_verification(webcam_video, id_card_image):
    """
    Compares a live webcam video with an ID card image for face verification.
    """
    # Check for inputs
    if webcam_video is None or id_card_image is None:
        return "Please provide both a live video and an ID card image.", None, None

    # Step 1: Liveness Detection and Image Capture
    if not liveness_check_active(webcam_video):
        return "Active liveness check failed. This appears to be a spoof attempt.", None, None
    
    # Use the first frame from the video for face matching
    cap = cv2.VideoCapture(webcam_video)
    ret, frame = cap.read()
    if not ret:
        return "Could not capture a frame from the video.", None, None
    webcam_image_np = frame
    cap.release()
    liveness_result = "Active liveness check: Passed ✅"

    # Step 2: Face Detection & Embedding for Selfie
    faces_selfie = app.get(webcam_image_np)
    if not faces_selfie:
        return "No face detected in the video.", None, None
    face_selfie = faces_selfie[0]
    embedding_selfie = face_selfie.embedding

    # Step 3: Convert PIL Images to NumPy arrays for ID
    id_card_image_np = np.array(id_card_image)

    # Step 4: Face Detection & Embedding for ID Card Photo
    faces_id = app.get(id_card_image_np)
    if not faces_id:
        return "No face detected in the ID card image.", None, None
    face_id = faces_id[0]
    embedding_id = faces_id[0].embedding

    # Step 5: Facial Matching
    similarity_score = np.dot(embedding_selfie, embedding_id)
    
    # Define a threshold for a positive match.
    confidence_threshold = 100

    # Step 6: Verification Logic
    result_text = f"{liveness_result}\n"
    result_text += f"Similarity Score: {similarity_score:.4f}\n"

    if similarity_score >= confidence_threshold:
        result_text += "Verdict: Faces match! ✅"
        match_found = True
    else:
        result_text += "Verdict: Faces do NOT match! ❌"
        match_found = False
    
    result_text += "\n\nNote: ID text verification is not yet implemented in this demo."

    return result_text, similarity_score, match_found

# Define the Gradio interface
interface = gr.Interface(
    fn=run_face_verification,
    inputs=[
        gr.Video(label="Live Selfie (Webcam)", sources="webcam"),
        gr.Image(label="ID Card Photo", type="pil")
    ],
    outputs=[
        gr.Textbox(label="Verification Result"),
        gr.Number(label="Confidence Score"),
        gr.Checkbox(label="Match Found")
    ],
    title="Open-Source Face and ID Verification",
    description="Use your webcam for a live check to match your face with an ID photo."
)

if __name__ == "__main__":
    interface.launch()