Spaces:
Sleeping
Sleeping
JANGALA SAKETH commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,59 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
|
|
|
| 3 |
from ultralytics import YOLO
|
|
|
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
def
|
| 8 |
image_np = np.array(image)
|
| 9 |
-
results = model(image_np)
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
|
| 13 |
for r in results:
|
| 14 |
for box, cls, conf in zip(r.boxes.xyxy, r.boxes.cls, r.boxes.conf):
|
| 15 |
|
| 16 |
-
#
|
| 17 |
if int(cls) != 0:
|
| 18 |
continue
|
| 19 |
-
|
| 20 |
if conf < 0.4:
|
| 21 |
continue
|
| 22 |
|
| 23 |
xmin, ymin, xmax, ymax = box.cpu().numpy()
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
iface = gr.Interface(
|
| 42 |
-
fn=
|
| 43 |
inputs=gr.Image(type="pil"),
|
| 44 |
outputs="json"
|
| 45 |
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
+
import cv2
|
| 4 |
from ultralytics import YOLO
|
| 5 |
+
import insightface
|
| 6 |
|
| 7 |
+
# Load models
|
| 8 |
+
yolo = YOLO("yolov8n.pt")
|
| 9 |
+
face_model = insightface.app.FaceAnalysis(name="buffalo_l")
|
| 10 |
+
face_model.prepare(ctx_id=0)
|
| 11 |
|
| 12 |
+
def process_image(image):
|
| 13 |
image_np = np.array(image)
|
|
|
|
| 14 |
|
| 15 |
+
results = yolo(image_np)
|
| 16 |
+
|
| 17 |
+
faces_output = []
|
| 18 |
|
| 19 |
for r in results:
|
| 20 |
for box, cls, conf in zip(r.boxes.xyxy, r.boxes.cls, r.boxes.conf):
|
| 21 |
|
| 22 |
+
# Only detect persons
|
| 23 |
if int(cls) != 0:
|
| 24 |
continue
|
|
|
|
| 25 |
if conf < 0.4:
|
| 26 |
continue
|
| 27 |
|
| 28 |
xmin, ymin, xmax, ymax = box.cpu().numpy()
|
| 29 |
+
xmin, ymin, xmax, ymax = map(int, [xmin, ymin, xmax, ymax])
|
| 30 |
+
|
| 31 |
+
person_crop = image_np[ymin:ymax, xmin:xmax]
|
| 32 |
+
|
| 33 |
+
detected_faces = face_model.get(person_crop)
|
| 34 |
+
|
| 35 |
+
for face in detected_faces:
|
| 36 |
+
embedding = face.embedding.tolist()
|
| 37 |
+
|
| 38 |
+
cx = (xmin + xmax) / 2
|
| 39 |
+
cy = (ymin + ymax) / 2
|
| 40 |
+
|
| 41 |
+
faces_output.append({
|
| 42 |
+
"cx": float(cx),
|
| 43 |
+
"cy": float(cy),
|
| 44 |
+
"box": {
|
| 45 |
+
"xmin": xmin,
|
| 46 |
+
"ymin": ymin,
|
| 47 |
+
"xmax": xmax,
|
| 48 |
+
"ymax": ymax
|
| 49 |
+
},
|
| 50 |
+
"embedding": embedding
|
| 51 |
+
})
|
| 52 |
+
|
| 53 |
+
return faces_output
|
| 54 |
|
| 55 |
iface = gr.Interface(
|
| 56 |
+
fn=process_image,
|
| 57 |
inputs=gr.Image(type="pil"),
|
| 58 |
outputs="json"
|
| 59 |
)
|