Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -40,42 +40,36 @@ def detect_person(
|
|
40 |
vbboxes[:, 3] = kpss[:, 4, 1]
|
41 |
return bboxes, vbboxes
|
42 |
|
43 |
-
def visualize(image: np.ndarray, bboxes: np.ndarray, vbboxes: np.ndarray) -> np.ndarray:
|
44 |
-
|
45 |
for i in range(bboxes.shape[0]):
|
46 |
bbox = bboxes[i]
|
47 |
-
vbbox = vbboxes[i]
|
48 |
x1, y1, x2, y2 = bbox
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
cv2.circle(res, (vx1, vy1), 1, color, 2)
|
56 |
-
cv2.circle(res, (vx1, vy2), 1, color, 2)
|
57 |
-
cv2.circle(res, (vx2, vy1), 1, color, 2)
|
58 |
-
cv2.circle(res, (vx2, vy2), 1, color, 2)
|
59 |
-
return res
|
60 |
|
61 |
detector = load_model()
|
62 |
detector.prepare(-1, nms_thresh=0.5, input_size=(640, 640))
|
63 |
|
64 |
-
def detect(image: np.ndarray) -> np.ndarray:
|
65 |
if image is None:
|
66 |
-
return
|
67 |
|
68 |
image = image[:, :, ::-1] # RGB -> BGR
|
69 |
bboxes, vbboxes = detect_person(image, detector)
|
70 |
-
|
71 |
-
return
|
72 |
|
73 |
examples = sorted(pathlib.Path("images").glob("*.jpg"))
|
74 |
|
75 |
demo = gr.Interface(
|
76 |
fn=detect,
|
77 |
inputs=gr.Image(label="Input", type="numpy"),
|
78 |
-
outputs=gr.
|
79 |
examples=examples,
|
80 |
examples_per_page=30,
|
81 |
title=TITLE,
|
|
|
40 |
vbboxes[:, 3] = kpss[:, 4, 1]
|
41 |
return bboxes, vbboxes
|
42 |
|
43 |
+
def visualize(image: np.ndarray, bboxes: np.ndarray, vbboxes: np.ndarray) -> list[np.ndarray]:
|
44 |
+
person_images = []
|
45 |
for i in range(bboxes.shape[0]):
|
46 |
bbox = bboxes[i]
|
|
|
47 |
x1, y1, x2, y2 = bbox
|
48 |
+
person_img = image[y1:y2, x1:x2]
|
49 |
+
|
50 |
+
# Append the cropped person image
|
51 |
+
person_images.append(person_img)
|
52 |
+
|
53 |
+
return person_images
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
detector = load_model()
|
56 |
detector.prepare(-1, nms_thresh=0.5, input_size=(640, 640))
|
57 |
|
58 |
+
def detect(image: np.ndarray) -> list[np.ndarray]:
|
59 |
if image is None:
|
60 |
+
return []
|
61 |
|
62 |
image = image[:, :, ::-1] # RGB -> BGR
|
63 |
bboxes, vbboxes = detect_person(image, detector)
|
64 |
+
person_images = visualize(image, bboxes, vbboxes)
|
65 |
+
return [img[:, :, ::-1] for img in person_images] # BGR -> RGB
|
66 |
|
67 |
examples = sorted(pathlib.Path("images").glob("*.jpg"))
|
68 |
|
69 |
demo = gr.Interface(
|
70 |
fn=detect,
|
71 |
inputs=gr.Image(label="Input", type="numpy"),
|
72 |
+
outputs=gr.Gallery(label="Detected Persons"),
|
73 |
examples=examples,
|
74 |
examples_per_page=30,
|
75 |
title=TITLE,
|