wanchoi commited on
Commit
4b715e4
·
verified ·
1 Parent(s): 4ed038a

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +75 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import cv2
4
+ import insightface
5
+ from insightface.app import FaceAnalysis
6
+
7
+ # Global variable to store the InsightFace app instance
8
+ # This is loaded once at the start for efficiency
9
+ app = FaceAnalysis(providers=['CPUExecutionProvider'])
10
+ app.prepare(ctx_id=0, det_size=(640, 640))
11
+
12
+ def run_face_verification(selfie_image, id_card_image):
13
+ """
14
+ Compares a selfie image with an ID card image for face verification.
15
+ """
16
+ if selfie_image is None or id_card_image is None:
17
+ return "Please upload both a selfie and an ID card image.", None, None
18
+
19
+ # Step 1: Face Detection & Embedding for Selfie
20
+ faces_selfie = app.get(selfie_image)
21
+ if not faces_selfie:
22
+ return "No face detected in the selfie.", None, None
23
+ face_selfie = faces_selfie[0]
24
+ embedding_selfie = face_selfie.embedding
25
+
26
+ # Step 2: Face Detection & Embedding for ID Card Photo
27
+ faces_id = app.get(id_card_image)
28
+ if not faces_id:
29
+ return "No face detected in the ID card image.", None, None
30
+ face_id = faces_id[0]
31
+ embedding_id = face_id.embedding
32
+
33
+ # Step 3: Facial Matching
34
+ # The dot product of the embeddings gives a similarity score (cosine similarity)
35
+ similarity_score = np.dot(embedding_selfie, embedding_id)
36
+
37
+ # Define a threshold for a positive match. This is a common value for InsightFace.
38
+ confidence_threshold = 0.35
39
+
40
+ # Step 4: Verification Logic
41
+ result_text = f"Similarity Score: {similarity_score:.4f}\n"
42
+
43
+ if similarity_score >= confidence_threshold:
44
+ result_text += "Verdict: Faces match! ✅"
45
+ else:
46
+ result_text += "Verdict: Faces do NOT match! ❌"
47
+
48
+ # Placeholder for future steps
49
+ result_text += "\n\nNote: Liveness and ID text verification are not yet implemented in this demo."
50
+
51
+ return result_text, similarity_score, similarity_score >= confidence_threshold
52
+
53
+ # Define the Gradio interface
54
+ # This creates the visual layout of your app.
55
+ interface = gr.Interface(
56
+ fn=run_face_verification,
57
+ inputs=[
58
+ gr.Image(label="Selfie (Live Face)", type="pil"),
59
+ gr.Image(label="ID Card Photo", type="pil")
60
+ ],
61
+ outputs=[
62
+ gr.Textbox(label="Verification Result"),
63
+ gr.Number(label="Confidence Score"),
64
+ gr.Checkbox(label="Match Found")
65
+ ],
66
+ title="Open-Source Face and ID Verification",
67
+ description="Upload a selfie and an ID photo to check if they match. This is a basic demonstration of face matching using InsightFace. It does **not** include liveness detection or ID text verification.",
68
+ examples=[
69
+ ["examples/selfie.jpg", "examples/id_photo.jpg"],
70
+ ]
71
+ )
72
+
73
+ # Launch the app
74
+ if __name__ == "__main__":
75
+ interface.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ insightface==0.7.3
2
+ onnxruntime==1.22.1
3
+ opencv-python==4.12.0.88
4
+ gradio==1.12.1