Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,53 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image, ImageDraw
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
draw.text((left + 6, bottom - text_height - 5), name, fill=(0, 0, 0))
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
|
|
|
| 31 |
|
| 32 |
|
| 33 |
# Gradio Interface
|
| 34 |
demo = gr.Interface(
|
| 35 |
-
fn=
|
| 36 |
inputs=gr.Image(type="pil", label="Upload an image"),
|
| 37 |
outputs=gr.Image(label="Detected Faces"),
|
| 38 |
-
title="Face
|
| 39 |
-
description="
|
| 40 |
)
|
| 41 |
|
| 42 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import mediapipe as mp
|
| 4 |
import numpy as np
|
| 5 |
from PIL import Image, ImageDraw
|
| 6 |
|
| 7 |
+
# Initialize MediaPipe Face Detection
|
| 8 |
+
mp_face_detection = mp.solutions.face_detection
|
| 9 |
+
mp_drawing = mp.solutions.drawing_utils
|
| 10 |
|
| 11 |
+
def detect_faces(image):
|
| 12 |
+
# Convert to OpenCV format
|
| 13 |
+
img = np.array(image.convert("RGB"))
|
| 14 |
+
img_rgb = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 15 |
|
| 16 |
+
# Create a face detection object
|
| 17 |
+
with mp_face_detection.FaceDetection(model_selection=0, min_detection_confidence=0.5) as face_detection:
|
| 18 |
+
results = face_detection.process(img)
|
| 19 |
|
| 20 |
+
# Draw results
|
| 21 |
+
if results.detections:
|
| 22 |
+
for detection in results.detections:
|
| 23 |
+
bboxC = detection.location_data.relative_bounding_box
|
| 24 |
+
h, w, _ = img.shape
|
| 25 |
+
x, y, width, height = (
|
| 26 |
+
int(bboxC.xmin * w),
|
| 27 |
+
int(bboxC.ymin * h),
|
| 28 |
+
int(bboxC.width * w),
|
| 29 |
+
int(bboxC.height * h),
|
| 30 |
+
)
|
| 31 |
|
| 32 |
+
# Draw bounding box
|
| 33 |
+
cv2.rectangle(img_rgb, (x, y), (x + width, y + height), (0, 255, 0), 3)
|
| 34 |
|
| 35 |
+
# Add label "Unknown"
|
| 36 |
+
cv2.rectangle(img_rgb, (x, y - 25), (x + 100, y), (0, 255, 0), -1)
|
| 37 |
+
cv2.putText(img_rgb, "Unknown", (x + 5, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 0), 2)
|
|
|
|
| 38 |
|
| 39 |
+
# Convert back to PIL for Gradio
|
| 40 |
+
result_image = Image.fromarray(cv2.cvtColor(img_rgb, cv2.COLOR_BGR2RGB))
|
| 41 |
+
return result_image
|
| 42 |
|
| 43 |
|
| 44 |
# Gradio Interface
|
| 45 |
demo = gr.Interface(
|
| 46 |
+
fn=detect_faces,
|
| 47 |
inputs=gr.Image(type="pil", label="Upload an image"),
|
| 48 |
outputs=gr.Image(label="Detected Faces"),
|
| 49 |
+
title="Simple Face Detection (Lightweight)",
|
| 50 |
+
description="Detects faces using MediaPipe and labels them as 'Unknown'. Future-ready for adding identity recognition.",
|
| 51 |
)
|
| 52 |
|
| 53 |
if __name__ == "__main__":
|