Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import face_recognition
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image, ImageDraw
|
| 5 |
+
|
| 6 |
+
def recognize_faces(image):
|
| 7 |
+
# Convert uploaded image to RGB
|
| 8 |
+
image_rgb = np.array(image.convert("RGB"))
|
| 9 |
+
|
| 10 |
+
# Detect face locations and encodings
|
| 11 |
+
face_locations = face_recognition.face_locations(image_rgb)
|
| 12 |
+
face_encodings = face_recognition.face_encodings(image_rgb, face_locations)
|
| 13 |
+
|
| 14 |
+
# Draw bounding boxes
|
| 15 |
+
pil_image = Image.fromarray(image_rgb)
|
| 16 |
+
draw = ImageDraw.Draw(pil_image)
|
| 17 |
+
|
| 18 |
+
for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
|
| 19 |
+
name = "Unknown" # Placeholder — can later be replaced with real matching logic
|
| 20 |
+
|
| 21 |
+
# Draw box
|
| 22 |
+
draw.rectangle(((left, top), (right, bottom)), outline=(0, 255, 0), width=3)
|
| 23 |
+
|
| 24 |
+
# Label below the face
|
| 25 |
+
text_width, text_height = draw.textsize(name)
|
| 26 |
+
draw.rectangle(((left, bottom - text_height - 10), (right, bottom)), fill=(0, 255, 0))
|
| 27 |
+
draw.text((left + 6, bottom - text_height - 5), name, fill=(0, 0, 0))
|
| 28 |
+
|
| 29 |
+
del draw
|
| 30 |
+
return pil_image
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
# Gradio Interface
|
| 34 |
+
demo = gr.Interface(
|
| 35 |
+
fn=recognize_faces,
|
| 36 |
+
inputs=gr.Image(type="pil", label="Upload an image"),
|
| 37 |
+
outputs=gr.Image(label="Detected Faces"),
|
| 38 |
+
title="Face Recognition (Future-Ready)",
|
| 39 |
+
description="Uploads an image, detects faces, and labels them as Unknown. Can be extended for identity recognition later.",
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
if __name__ == "__main__":
|
| 43 |
+
demo.launch()
|