Spaces:
Sleeping
Sleeping
import os | |
import gradio as gr | |
from deepface import DeepFace | |
# Path to the dataset folder | |
DATASET_PATH = "face_dataset" | |
# Load dataset images (only image files are included) | |
dataset_images = { | |
os.path.splitext(filename)[0]: os.path.join(DATASET_PATH, filename) # Remove file extension | |
for filename in os.listdir(DATASET_PATH) | |
if filename.lower().endswith(('.jpg', '.jpeg', '.png')) | |
} | |
def recognize_face(uploaded_image): | |
""" | |
Compare the uploaded image with dataset images using DeepFace. | |
Returns the matched model name as an Instagram link and its image if a verified match is found. | |
""" | |
if uploaded_image is None: | |
return "No image uploaded.", None | |
# Loop through each image in the dataset | |
for model_name, image_path in dataset_images.items(): | |
try: | |
result = DeepFace.verify( | |
uploaded_image, | |
image_path, | |
model_name="VGG-Face", | |
enforce_detection=False | |
) | |
if result.get("verified"): | |
# Create an Instagram link | |
model_link = f'<a href="https://www.instagram.com/{model_name}/" target="_blank">{model_name}</a>' | |
return model_link, image_path | |
except Exception as e: | |
print(f"Error processing {image_path}: {e}") | |
return "No matching face found.", None | |
# Create a Gradio interface for the app | |
iface = gr.Interface( | |
fn=recognize_face, | |
inputs=gr.Image(type="numpy", label="Upload an Image"), | |
outputs=[ | |
gr.HTML(label="Model Name"), # Use HTML for clickable link | |
gr.Image(label="Matched Model Image") | |
], | |
title="Face Recognition App", | |
description="Upload an image to find the most similar face from the dataset." | |
) | |
if __name__ == "__main__": | |
iface.launch() | |