Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,43 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
import numpy as np
|
| 4 |
|
|
|
|
|
|
|
|
|
|
| 5 |
def detect_faces(image):
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
encodings = face_recognition.face_encodings(image, locations)
|
| 9 |
|
| 10 |
faces = []
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
return faces
|
| 27 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import mediapipe as mp
|
| 3 |
import numpy as np
|
| 4 |
|
| 5 |
+
mp_face = mp.solutions.face_detection
|
| 6 |
+
face_detection = mp_face.FaceDetection(model_selection=0, min_detection_confidence=0.5)
|
| 7 |
+
|
| 8 |
def detect_faces(image):
|
| 9 |
+
image_np = np.array(image)
|
| 10 |
+
results = face_detection.process(image_np)
|
|
|
|
| 11 |
|
| 12 |
faces = []
|
| 13 |
+
|
| 14 |
+
if results.detections:
|
| 15 |
+
for detection in results.detections:
|
| 16 |
+
bbox = detection.location_data.relative_bounding_box
|
| 17 |
+
|
| 18 |
+
h, w, _ = image_np.shape
|
| 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 |
+
xmax = xmin + width
|
| 26 |
+
ymax = ymin + height
|
| 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 |
|
| 42 |
return faces
|
| 43 |
|