Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,33 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import mediapipe as mp
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
|
| 8 |
def detect_faces(image):
|
| 9 |
image_np = np.array(image)
|
| 10 |
-
results = face_detection.process(image_np)
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
if results.detections:
|
| 15 |
-
for detection in results.detections:
|
| 16 |
-
bbox = detection.location_data.relative_bounding_box
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
xmin = int(bbox.xmin * w)
|
| 21 |
-
ymin = int(bbox.ymin * h)
|
| 22 |
-
width = int(bbox.width * w)
|
| 23 |
-
height = int(bbox.height * h)
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
|
|
|
|
|
|
|
| 28 |
cx = (xmin + xmax) / 2
|
| 29 |
cy = (ymin + ymax) / 2
|
| 30 |
|
| 31 |
faces.append({
|
| 32 |
-
"cx": cx,
|
| 33 |
-
"cy": cy,
|
| 34 |
"box": {
|
| 35 |
-
"xmin": xmin,
|
| 36 |
-
"ymin": ymin,
|
| 37 |
-
"xmax": xmax,
|
| 38 |
-
"ymax": ymax
|
| 39 |
}
|
| 40 |
})
|
| 41 |
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
+
from ultralytics import YOLO
|
| 4 |
|
| 5 |
+
# Use lightweight face model
|
| 6 |
+
model = YOLO("yolov8n.pt") # lightweight model
|
| 7 |
|
| 8 |
def detect_faces(image):
|
| 9 |
image_np = np.array(image)
|
|
|
|
| 10 |
|
| 11 |
+
results = model(image_np)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
faces = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
for r in results:
|
| 16 |
+
boxes = r.boxes.xyxy.cpu().numpy()
|
| 17 |
|
| 18 |
+
for box in boxes:
|
| 19 |
+
xmin, ymin, xmax, ymax = box
|
| 20 |
cx = (xmin + xmax) / 2
|
| 21 |
cy = (ymin + ymax) / 2
|
| 22 |
|
| 23 |
faces.append({
|
| 24 |
+
"cx": float(cx),
|
| 25 |
+
"cy": float(cy),
|
| 26 |
"box": {
|
| 27 |
+
"xmin": float(xmin),
|
| 28 |
+
"ymin": float(ymin),
|
| 29 |
+
"xmax": float(xmax),
|
| 30 |
+
"ymax": float(ymax)
|
| 31 |
}
|
| 32 |
})
|
| 33 |
|