Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,3 @@
|
|
1 |
-
#!/usr/bin/env python
|
2 |
-
|
3 |
from __future__ import annotations
|
4 |
import pathlib
|
5 |
import cv2
|
@@ -9,7 +7,6 @@ import insightface
|
|
9 |
import numpy as np
|
10 |
import onnxruntime as ort
|
11 |
from PIL import Image
|
12 |
-
import io
|
13 |
|
14 |
TITLE = "insightface Person Detection"
|
15 |
DESCRIPTION = "https://github.com/deepinsight/insightface/tree/master/examples/person_detection"
|
@@ -21,7 +18,7 @@ def load_model():
|
|
21 |
options.intra_op_num_threads = 8
|
22 |
options.inter_op_num_threads = 8
|
23 |
session = ort.InferenceSession(
|
24 |
-
path, sess_options=options, providers=["CPUExecutionProvider"
|
25 |
)
|
26 |
model = insightface.model_zoo.retinaface.RetinaFace(model_file=path, session=session)
|
27 |
return model
|
@@ -71,32 +68,16 @@ def extract_persons(image: np.ndarray, bboxes: np.ndarray) -> list[np.ndarray]:
|
|
71 |
return person_images
|
72 |
|
73 |
|
74 |
-
def convert_to_pil_image(image: np.ndarray) -> Image.Image:
|
75 |
-
"""Convert a NumPy image array to a PIL Image."""
|
76 |
-
return Image.fromarray(image)
|
77 |
-
|
78 |
-
|
79 |
-
def convert_to_png(image: np.ndarray) -> bytes:
|
80 |
-
"""Convert a NumPy image array to PNG bytes."""
|
81 |
-
pil_image = convert_to_pil_image(image)
|
82 |
-
buffer = io.BytesIO()
|
83 |
-
pil_image.save(buffer, format="PNG")
|
84 |
-
return buffer.getvalue()
|
85 |
-
|
86 |
-
|
87 |
detector = load_model()
|
88 |
detector.prepare(-1, nms_thresh=0.5, input_size=(640, 640))
|
89 |
|
90 |
|
91 |
-
def detect(image: np.ndarray) -> tuple[
|
92 |
image = image[:, :, ::-1] # RGB -> BGR
|
93 |
bboxes, vbboxes = detect_person(image, detector)
|
94 |
res = visualize(image, bboxes, vbboxes)
|
95 |
-
person_images = extract_persons(
|
96 |
-
|
97 |
-
result_image_bytes = convert_to_png(res[:, :, ::-1]) # BGR -> RGB
|
98 |
-
person_images_bytes = [convert_to_png(person_img[:, :, ::-1]) for person_img in person_images] # BGR -> RGB
|
99 |
-
return result_image_bytes, person_images_bytes
|
100 |
|
101 |
|
102 |
examples = sorted(pathlib.Path("images").glob("*.jpg"))
|
@@ -104,7 +85,7 @@ examples = sorted(pathlib.Path("images").glob("*.jpg"))
|
|
104 |
demo = gr.Interface(
|
105 |
fn=detect,
|
106 |
inputs=gr.Image(label="Input", type="numpy"),
|
107 |
-
outputs=[gr.Image(label="Processed Image", type="
|
108 |
examples=examples,
|
109 |
examples_per_page=30,
|
110 |
title=TITLE,
|
|
|
|
|
|
|
1 |
from __future__ import annotations
|
2 |
import pathlib
|
3 |
import cv2
|
|
|
7 |
import numpy as np
|
8 |
import onnxruntime as ort
|
9 |
from PIL import Image
|
|
|
10 |
|
11 |
TITLE = "insightface Person Detection"
|
12 |
DESCRIPTION = "https://github.com/deepinsight/insightface/tree/master/examples/person_detection"
|
|
|
18 |
options.intra_op_num_threads = 8
|
19 |
options.inter_op_num_threads = 8
|
20 |
session = ort.InferenceSession(
|
21 |
+
path, sess_options=options, providers=["CPUExecutionProvider"]
|
22 |
)
|
23 |
model = insightface.model_zoo.retinaface.RetinaFace(model_file=path, session=session)
|
24 |
return model
|
|
|
68 |
return person_images
|
69 |
|
70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
detector = load_model()
|
72 |
detector.prepare(-1, nms_thresh=0.5, input_size=(640, 640))
|
73 |
|
74 |
|
75 |
+
def detect(image: np.ndarray) -> tuple[np.ndarray, list[np.ndarray]]:
|
76 |
image = image[:, :, ::-1] # RGB -> BGR
|
77 |
bboxes, vbboxes = detect_person(image, detector)
|
78 |
res = visualize(image, bboxes, vbboxes)
|
79 |
+
person_images = extract_persons(res, bboxes) # Extract each person as a separate image
|
80 |
+
return res[:, :, ::-1], person_images # BGR -> RGB
|
|
|
|
|
|
|
81 |
|
82 |
|
83 |
examples = sorted(pathlib.Path("images").glob("*.jpg"))
|
|
|
85 |
demo = gr.Interface(
|
86 |
fn=detect,
|
87 |
inputs=gr.Image(label="Input", type="numpy"),
|
88 |
+
outputs=[gr.Image(label="Processed Image", type="numpy"), gr.Gallery(label="Detected Persons", type="numpy")],
|
89 |
examples=examples,
|
90 |
examples_per_page=30,
|
91 |
title=TITLE,
|