Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,11 +15,15 @@ def run_face_verification(selfie_image, id_card_image):
|
|
| 15 |
if selfie_image is None or id_card_image is None:
|
| 16 |
return "Please upload both a selfie and an ID card image.", None, None
|
| 17 |
|
| 18 |
-
# Step 1:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
selfie_image_np = np.array(selfie_image)
|
| 20 |
id_card_image_np = np.array(id_card_image)
|
| 21 |
|
| 22 |
-
# Step
|
| 23 |
# Pass the NumPy array to InsightFace
|
| 24 |
faces_selfie = app.get(selfie_image_np)
|
| 25 |
if not faces_selfie:
|
|
@@ -27,7 +31,7 @@ def run_face_verification(selfie_image, id_card_image):
|
|
| 27 |
face_selfie = faces_selfie[0]
|
| 28 |
embedding_selfie = face_selfie.embedding
|
| 29 |
|
| 30 |
-
# Step
|
| 31 |
# Pass the NumPy array to InsightFace
|
| 32 |
faces_id = app.get(id_card_image_np)
|
| 33 |
if not faces_id:
|
|
@@ -35,25 +39,36 @@ def run_face_verification(selfie_image, id_card_image):
|
|
| 35 |
face_id = faces_id[0]
|
| 36 |
embedding_id = face_id.embedding
|
| 37 |
|
| 38 |
-
# Step
|
| 39 |
similarity_score = np.dot(embedding_selfie, embedding_id)
|
| 40 |
|
| 41 |
# Define a threshold for a positive match.
|
| 42 |
confidence_threshold = 100
|
| 43 |
|
| 44 |
-
# Step
|
| 45 |
-
result_text = f"
|
|
|
|
| 46 |
|
| 47 |
if similarity_score >= confidence_threshold:
|
| 48 |
result_text += "Verdict: Faces match! ✅"
|
| 49 |
else:
|
| 50 |
result_text += "Verdict: Faces do NOT match! ❌"
|
| 51 |
|
| 52 |
-
|
| 53 |
-
result_text += "\n\nNote: Liveness and ID text verification are not yet implemented in this demo."
|
| 54 |
|
| 55 |
return result_text, similarity_score, similarity_score >= confidence_threshold
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
# Define the Gradio interface
|
| 58 |
interface = gr.Interface(
|
| 59 |
fn=run_face_verification,
|
|
|
|
| 15 |
if selfie_image is None or id_card_image is None:
|
| 16 |
return "Please upload both a selfie and an ID card image.", None, None
|
| 17 |
|
| 18 |
+
# Step 1: Liveness Detection
|
| 19 |
+
if not liveness_check(selfie_image):
|
| 20 |
+
return "Liveness check failed. This appears to be a spoof attempt.", None, None
|
| 21 |
+
|
| 22 |
+
# Step 2: Convert PIL Images to NumPy arrays
|
| 23 |
selfie_image_np = np.array(selfie_image)
|
| 24 |
id_card_image_np = np.array(id_card_image)
|
| 25 |
|
| 26 |
+
# Step 3: Face Detection & Embedding for Selfie
|
| 27 |
# Pass the NumPy array to InsightFace
|
| 28 |
faces_selfie = app.get(selfie_image_np)
|
| 29 |
if not faces_selfie:
|
|
|
|
| 31 |
face_selfie = faces_selfie[0]
|
| 32 |
embedding_selfie = face_selfie.embedding
|
| 33 |
|
| 34 |
+
# Step 4: Face Detection & Embedding for ID Card Photo
|
| 35 |
# Pass the NumPy array to InsightFace
|
| 36 |
faces_id = app.get(id_card_image_np)
|
| 37 |
if not faces_id:
|
|
|
|
| 39 |
face_id = faces_id[0]
|
| 40 |
embedding_id = face_id.embedding
|
| 41 |
|
| 42 |
+
# Step 5: Facial Matching
|
| 43 |
similarity_score = np.dot(embedding_selfie, embedding_id)
|
| 44 |
|
| 45 |
# Define a threshold for a positive match.
|
| 46 |
confidence_threshold = 100
|
| 47 |
|
| 48 |
+
# Step 6: Verification Logic
|
| 49 |
+
result_text = f"Liveness check: Passed ✅\n"
|
| 50 |
+
result_text += f"Similarity Score: {similarity_score:.4f}\n"
|
| 51 |
|
| 52 |
if similarity_score >= confidence_threshold:
|
| 53 |
result_text += "Verdict: Faces match! ✅"
|
| 54 |
else:
|
| 55 |
result_text += "Verdict: Faces do NOT match! ❌"
|
| 56 |
|
| 57 |
+
result_text += "\n\nNote: ID text verification is not yet implemented in this demo."
|
|
|
|
| 58 |
|
| 59 |
return result_text, similarity_score, similarity_score >= confidence_threshold
|
| 60 |
|
| 61 |
+
def liveness_check(image):
|
| 62 |
+
"""
|
| 63 |
+
Checks if the face in the image is a live person.
|
| 64 |
+
Returns True for live, False for spoof.
|
| 65 |
+
"""
|
| 66 |
+
# Placeholder for a liveness detection model.
|
| 67 |
+
# A real implementation would run a model here to return a score.
|
| 68 |
+
# For a simple check, we can assume any detected face is live.
|
| 69 |
+
# We will expand this function later to include a real model.
|
| 70 |
+
return True
|
| 71 |
+
|
| 72 |
# Define the Gradio interface
|
| 73 |
interface = gr.Interface(
|
| 74 |
fn=run_face_verification,
|