Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import face_recognition
|
| 3 |
+
|
| 4 |
+
def face_match(file1, file2, threshold):
|
| 5 |
+
try:
|
| 6 |
+
# Load the images
|
| 7 |
+
image1 = face_recognition.load_image_file(file1)
|
| 8 |
+
image2 = face_recognition.load_image_file(file2)
|
| 9 |
+
|
| 10 |
+
# Encode the faces
|
| 11 |
+
encodings1 = face_recognition.face_encodings(image1)
|
| 12 |
+
encodings2 = face_recognition.face_encodings(image2)
|
| 13 |
+
|
| 14 |
+
if len(encodings1) == 0 and len(encodings2) == 0:
|
| 15 |
+
return {"error": "No faces detected in both images."}
|
| 16 |
+
elif len(encodings1) == 0:
|
| 17 |
+
return {"error": "No face detected in the first image."}
|
| 18 |
+
elif len(encodings2) == 0:
|
| 19 |
+
return {"error": "No face detected in the second image."}
|
| 20 |
+
|
| 21 |
+
# Use the first face encoding from each image
|
| 22 |
+
face_encoding1 = encodings1[0]
|
| 23 |
+
face_encoding2 = encodings2[0]
|
| 24 |
+
|
| 25 |
+
# Calculate the distance
|
| 26 |
+
distance = face_recognition.face_distance([face_encoding1], face_encoding2)[0]
|
| 27 |
+
|
| 28 |
+
# Determine match status
|
| 29 |
+
match_status = "match" if distance <= threshold else "no_match"
|
| 30 |
+
|
| 31 |
+
return {
|
| 32 |
+
"status": match_status,
|
| 33 |
+
"distance": round(distance, 4),
|
| 34 |
+
"threshold": threshold
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
except Exception as e:
|
| 38 |
+
print(e)
|
| 39 |
+
return {"error": f"Unexpected error: {str(e)}"}
|
| 40 |
+
|
| 41 |
+
iface = gr.Interface(
|
| 42 |
+
fn=face_match,
|
| 43 |
+
inputs=[
|
| 44 |
+
gr.Image(type="filepath", label="Image 1"),
|
| 45 |
+
gr.Image(type="filepath", label="Image 2"),
|
| 46 |
+
gr.Number(value=0.6, label="Threshold")
|
| 47 |
+
],
|
| 48 |
+
outputs="json",
|
| 49 |
+
title="Face Match App",
|
| 50 |
+
description="Upload two images and set a threshold to check if the faces match."
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
iface.launch(server_name="0.0.0.0")
|