Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,34 +5,38 @@ from deepface import DeepFace
|
|
5 |
# Path to the dataset folder
|
6 |
DATASET_PATH = "face_dataset"
|
7 |
|
8 |
-
# Load dataset images
|
9 |
dataset_images = {
|
10 |
-
|
11 |
-
for
|
12 |
-
if
|
13 |
}
|
14 |
|
15 |
def recognize_face(uploaded_image):
|
16 |
"""
|
17 |
-
Compare the uploaded image with dataset images
|
|
|
18 |
"""
|
19 |
if uploaded_image is None:
|
20 |
return "No image uploaded.", None
|
21 |
|
|
|
22 |
for model_name, image_path in dataset_images.items():
|
23 |
try:
|
24 |
-
# Perform face verification
|
25 |
result = DeepFace.verify(
|
26 |
-
uploaded_image,
|
|
|
|
|
|
|
27 |
)
|
28 |
-
if result
|
29 |
return model_name, image_path
|
30 |
except Exception as e:
|
31 |
print(f"Error processing {image_path}: {e}")
|
32 |
|
33 |
return "No matching face found.", None
|
34 |
|
35 |
-
# Create a Gradio interface
|
36 |
iface = gr.Interface(
|
37 |
fn=recognize_face,
|
38 |
inputs=gr.Image(type="numpy", label="Upload an Image"),
|
|
|
5 |
# Path to the dataset folder
|
6 |
DATASET_PATH = "face_dataset"
|
7 |
|
8 |
+
# Load dataset images (only image files are included)
|
9 |
dataset_images = {
|
10 |
+
filename: os.path.join(DATASET_PATH, filename)
|
11 |
+
for filename in os.listdir(DATASET_PATH)
|
12 |
+
if filename.lower().endswith(('.jpg', '.jpeg', '.png'))
|
13 |
}
|
14 |
|
15 |
def recognize_face(uploaded_image):
|
16 |
"""
|
17 |
+
Compare the uploaded image with dataset images using DeepFace.
|
18 |
+
Returns the matched model name and its image if a verified match is found.
|
19 |
"""
|
20 |
if uploaded_image is None:
|
21 |
return "No image uploaded.", None
|
22 |
|
23 |
+
# Loop through each image in the dataset
|
24 |
for model_name, image_path in dataset_images.items():
|
25 |
try:
|
|
|
26 |
result = DeepFace.verify(
|
27 |
+
uploaded_image,
|
28 |
+
image_path,
|
29 |
+
model_name="VGG-Face",
|
30 |
+
enforce_detection=False
|
31 |
)
|
32 |
+
if result.get("verified"):
|
33 |
return model_name, image_path
|
34 |
except Exception as e:
|
35 |
print(f"Error processing {image_path}: {e}")
|
36 |
|
37 |
return "No matching face found.", None
|
38 |
|
39 |
+
# Create a Gradio interface for the app
|
40 |
iface = gr.Interface(
|
41 |
fn=recognize_face,
|
42 |
inputs=gr.Image(type="numpy", label="Upload an Image"),
|