Spaces:
Running
Running
Update src/app.py
Browse files- src/app.py +64 -33
src/app.py
CHANGED
|
@@ -1,53 +1,84 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import face_recognition
|
|
|
|
| 3 |
|
| 4 |
-
def
|
| 5 |
try:
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
image2 = face_recognition.load_image_file(file2)
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
|
| 14 |
-
if len(
|
| 15 |
-
return {"error": "No
|
| 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 |
-
|
| 22 |
-
face_encoding1 = encodings1[0]
|
| 23 |
-
face_encoding2 = encodings2[0]
|
| 24 |
|
| 25 |
-
|
| 26 |
-
distance = face_recognition.face_distance([face_encoding1], face_encoding2)[0]
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 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=
|
| 43 |
inputs=[
|
| 44 |
-
gr.
|
| 45 |
-
gr.
|
| 46 |
-
gr.
|
| 47 |
],
|
| 48 |
outputs="json",
|
| 49 |
-
title="Face Match App",
|
| 50 |
-
description="Upload
|
| 51 |
)
|
| 52 |
|
| 53 |
-
iface.launch(server_name="0.0.0.0")
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import face_recognition
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
def face_match_batch(original_file, threshold, files):
|
| 6 |
try:
|
| 7 |
+
if not files or len(files) == 0:
|
| 8 |
+
return {"error": "At least one file is required to match against the original."}
|
|
|
|
| 9 |
|
| 10 |
+
# Load the original image
|
| 11 |
+
original_image = face_recognition.load_image_file(original_file)
|
| 12 |
+
original_encodings = face_recognition.face_encodings(original_image)
|
| 13 |
|
| 14 |
+
if len(original_encodings) == 0:
|
| 15 |
+
return {"error": "No face detected in the original image."}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
original_encoding = original_encodings[0]
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
response = []
|
|
|
|
| 20 |
|
| 21 |
+
# Iterate through the files to match
|
| 22 |
+
for file in files:
|
| 23 |
+
try:
|
| 24 |
+
# Load the target image
|
| 25 |
+
target_image = face_recognition.load_image_file(file)
|
| 26 |
+
target_encodings = face_recognition.face_encodings(target_image)
|
| 27 |
|
| 28 |
+
# Extract the file name from the full path
|
| 29 |
+
file_name = os.path.basename(file)
|
| 30 |
+
|
| 31 |
+
if len(target_encodings) == 0:
|
| 32 |
+
# No face found in the target image
|
| 33 |
+
response.append({
|
| 34 |
+
"status": "no_face",
|
| 35 |
+
"distance": 1.0,
|
| 36 |
+
"confidence": 0.0,
|
| 37 |
+
"file": file_name
|
| 38 |
+
})
|
| 39 |
+
else:
|
| 40 |
+
# Use the first face encoding from the target image
|
| 41 |
+
target_encoding = target_encodings[0]
|
| 42 |
+
|
| 43 |
+
# Calculate the distance
|
| 44 |
+
distance = face_recognition.face_distance([original_encoding], target_encoding)[0]
|
| 45 |
+
|
| 46 |
+
# Confidence percentage
|
| 47 |
+
confidence = (1 - distance) * 100
|
| 48 |
+
|
| 49 |
+
# Determine match status
|
| 50 |
+
match_status = "match" if distance <= threshold else "no_match"
|
| 51 |
+
|
| 52 |
+
response.append({
|
| 53 |
+
"status": match_status,
|
| 54 |
+
"distance": round(distance, 4),
|
| 55 |
+
"confidence": round(confidence, 2),
|
| 56 |
+
"file": file_name
|
| 57 |
+
})
|
| 58 |
+
|
| 59 |
+
except Exception as e:
|
| 60 |
+
response.append({
|
| 61 |
+
"status": "error",
|
| 62 |
+
"distance": 1.0,
|
| 63 |
+
"confidence": 0.0,
|
| 64 |
+
"file": file_name
|
| 65 |
+
})
|
| 66 |
+
|
| 67 |
+
return response
|
| 68 |
|
| 69 |
except Exception as e:
|
|
|
|
| 70 |
return {"error": f"Unexpected error: {str(e)}"}
|
| 71 |
|
| 72 |
iface = gr.Interface(
|
| 73 |
+
fn=face_match_batch,
|
| 74 |
inputs=[
|
| 75 |
+
gr.File(file_types=[".jpg", ".jpeg", ".png"], type="filepath", label="Original Image"),
|
| 76 |
+
gr.Number(value=0.6, label="Threshold"),
|
| 77 |
+
gr.File(file_types=[".jpg", ".jpeg", ".png"], type="filepath", label="Images to Match", file_count="multiple")
|
| 78 |
],
|
| 79 |
outputs="json",
|
| 80 |
+
title="Batch Face Match App",
|
| 81 |
+
description="Upload an original image, set a threshold, and upload multiple images to match against the original."
|
| 82 |
)
|
| 83 |
|
| 84 |
+
iface.launch(server_name="0.0.0.0")
|