Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -9,6 +9,7 @@ import insightface
|
|
9 |
import numpy as np
|
10 |
import onnxruntime as ort
|
11 |
from PIL import Image
|
|
|
12 |
|
13 |
TITLE = "insightface Person Detection"
|
14 |
DESCRIPTION = "https://github.com/deepinsight/insightface/tree/master/examples/person_detection"
|
@@ -75,19 +76,27 @@ def convert_to_pil_image(image: np.ndarray) -> Image.Image:
|
|
75 |
return Image.fromarray(image)
|
76 |
|
77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
detector = load_model()
|
79 |
detector.prepare(-1, nms_thresh=0.5, input_size=(640, 640))
|
80 |
|
81 |
|
82 |
-
def detect(image: np.ndarray) -> tuple[
|
83 |
image = image[:, :, ::-1] # RGB -> BGR
|
84 |
bboxes, vbboxes = detect_person(image, detector)
|
85 |
res = visualize(image, bboxes, vbboxes)
|
86 |
person_images = extract_persons(image, bboxes)
|
87 |
-
# Convert the images to
|
88 |
-
|
89 |
-
|
90 |
-
return
|
91 |
|
92 |
|
93 |
examples = sorted(pathlib.Path("images").glob("*.jpg"))
|
@@ -95,7 +104,7 @@ examples = sorted(pathlib.Path("images").glob("*.jpg"))
|
|
95 |
demo = gr.Interface(
|
96 |
fn=detect,
|
97 |
inputs=gr.Image(label="Input", type="numpy"),
|
98 |
-
outputs=[gr.Image(label="Processed Image"), gr.Gallery(label="Detected Persons")],
|
99 |
examples=examples,
|
100 |
examples_per_page=30,
|
101 |
title=TITLE,
|
|
|
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"
|
|
|
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[bytes, list[bytes]]:
|
92 |
image = image[:, :, ::-1] # RGB -> BGR
|
93 |
bboxes, vbboxes = detect_person(image, detector)
|
94 |
res = visualize(image, bboxes, vbboxes)
|
95 |
person_images = extract_persons(image, bboxes)
|
96 |
+
# Convert the images to PNG bytes
|
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 |
demo = gr.Interface(
|
105 |
fn=detect,
|
106 |
inputs=gr.Image(label="Input", type="numpy"),
|
107 |
+
outputs=[gr.Image(label="Processed Image", type="bytes"), gr.Gallery(label="Detected Persons", type="bytes")],
|
108 |
examples=examples,
|
109 |
examples_per_page=30,
|
110 |
title=TITLE,
|