Add files
Browse files- .gitattributes +1 -0
- app.py +138 -0
- images/README.md +27 -0
- images/pexels-allan-mas-5368927.jpg +3 -0
- images/pexels-allan-mas-5368976.jpg +3 -0
- images/pexels-ana-bregantin-1930364.jpg +3 -0
- images/pexels-anastasiya-gepp-4456142.jpg +3 -0
- images/pexels-anastasiya-gepp-4456152.jpg +3 -0
- images/pexels-august-de-richelieu-4427813.jpg +3 -0
- images/pexels-cottonbro-5770445.jpg +3 -0
- images/pexels-cottonbro-5770708.jpg +3 -0
- images/pexels-cottonbro-6800242.jpg +3 -0
- images/pexels-dmitriy-ganin-9167601.jpg +3 -0
- images/pexels-haste-leart-v-690598.jpg +3 -0
- images/pexels-jake-_sulli_-swoyer-5835863.jpg +3 -0
- images/pexels-jasmine-carter-812258.jpg +3 -0
- images/pexels-jo-kassis-5567802.jpg +3 -0
- images/pexels-kaichieh-chan-917510.jpg +3 -0
- images/pexels-luis-gallegos-alvarez-1164975.jpg +3 -0
- images/pexels-lukas-rodriguez-3492736.jpg +3 -0
- images/pexels-martin-lopez-2240771.jpg +3 -0
- images/pexels-mehmet-turgut-kirkgoz-11596703.jpg +3 -0
- images/pexels-nataliya-vaitkevich-4943547.jpg +3 -0
- images/pexels-satoshi-hirayama-1325837.jpg +3 -0
- images/pexels-victoria-borodinova-7909580.jpg +3 -0
- images/pexels-yan-krukov-5792907.jpg +3 -0
- images/pexels-yogendra-singh-1701195.jpg +3 -0
- images/pexels-лиза-медведева-8574605.jpg +3 -0
- requirements.txt +5 -0
.gitattributes
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
*.bin filter=lfs diff=lfs merge=lfs -text
|
|
|
1 |
+
*.jpg filter=lfs diff=lfs merge=lfs -text
|
2 |
*.7z filter=lfs diff=lfs merge=lfs -text
|
3 |
*.arrow filter=lfs diff=lfs merge=lfs -text
|
4 |
*.bin filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
|
3 |
+
from __future__ import annotations
|
4 |
+
|
5 |
+
import argparse
|
6 |
+
import functools
|
7 |
+
import os
|
8 |
+
import pathlib
|
9 |
+
import subprocess
|
10 |
+
|
11 |
+
if os.environ.get('SYSTEM') == 'spaces':
|
12 |
+
subprocess.call('pip install insightface==0.6.2'.split())
|
13 |
+
|
14 |
+
import cv2
|
15 |
+
import gradio as gr
|
16 |
+
import huggingface_hub
|
17 |
+
import insightface
|
18 |
+
import numpy as np
|
19 |
+
import onnxruntime as ort
|
20 |
+
|
21 |
+
REPO_URL = 'https://github.com/deepinsight/insightface/tree/master/examples/person_detection'
|
22 |
+
TITLE = 'insightface Person Detection'
|
23 |
+
DESCRIPTION = f'This is a demo for {REPO_URL}.'
|
24 |
+
ARTICLE = None
|
25 |
+
|
26 |
+
TOKEN = os.environ['TOKEN']
|
27 |
+
|
28 |
+
|
29 |
+
def parse_args() -> argparse.Namespace:
|
30 |
+
parser = argparse.ArgumentParser()
|
31 |
+
parser.add_argument('--theme', type=str)
|
32 |
+
parser.add_argument('--live', action='store_true')
|
33 |
+
parser.add_argument('--share', action='store_true')
|
34 |
+
parser.add_argument('--port', type=int)
|
35 |
+
parser.add_argument('--disable-queue',
|
36 |
+
dest='enable_queue',
|
37 |
+
action='store_false')
|
38 |
+
parser.add_argument('--allow-flagging', type=str, default='never')
|
39 |
+
parser.add_argument('--allow-screenshot', action='store_true')
|
40 |
+
return parser.parse_args()
|
41 |
+
|
42 |
+
|
43 |
+
def load_model():
|
44 |
+
path = huggingface_hub.hf_hub_download('hysts/insightface',
|
45 |
+
'models/scrfd_person_2.5g.onnx',
|
46 |
+
use_auth_token=TOKEN)
|
47 |
+
options = ort.SessionOptions()
|
48 |
+
options.intra_op_num_threads = 8
|
49 |
+
options.inter_op_num_threads = 8
|
50 |
+
session = ort.InferenceSession(path,
|
51 |
+
sess_options=options,
|
52 |
+
providers=['CPUExecutionProvider'])
|
53 |
+
model = insightface.model_zoo.retinaface.RetinaFace(model_file=path,
|
54 |
+
session=session)
|
55 |
+
return model
|
56 |
+
|
57 |
+
|
58 |
+
def detect_person(
|
59 |
+
img: np.ndarray, detector: insightface.model_zoo.retinaface.RetinaFace
|
60 |
+
) -> tuple[np.ndarray, np.ndarray]:
|
61 |
+
bboxes, kpss = detector.detect(img)
|
62 |
+
bboxes = np.round(bboxes[:, :4]).astype(np.int)
|
63 |
+
kpss = np.round(kpss).astype(np.int)
|
64 |
+
kpss[:, :, 0] = np.clip(kpss[:, :, 0], 0, img.shape[1])
|
65 |
+
kpss[:, :, 1] = np.clip(kpss[:, :, 1], 0, img.shape[0])
|
66 |
+
vbboxes = bboxes.copy()
|
67 |
+
vbboxes[:, 0] = kpss[:, 0, 0]
|
68 |
+
vbboxes[:, 1] = kpss[:, 0, 1]
|
69 |
+
vbboxes[:, 2] = kpss[:, 4, 0]
|
70 |
+
vbboxes[:, 3] = kpss[:, 4, 1]
|
71 |
+
return bboxes, vbboxes
|
72 |
+
|
73 |
+
|
74 |
+
def visualize(image: np.ndarray, bboxes: np.ndarray,
|
75 |
+
vbboxes: np.ndarray) -> np.ndarray:
|
76 |
+
res = image.copy()
|
77 |
+
for i in range(bboxes.shape[0]):
|
78 |
+
bbox = bboxes[i]
|
79 |
+
vbbox = vbboxes[i]
|
80 |
+
x1, y1, x2, y2 = bbox
|
81 |
+
vx1, vy1, vx2, vy2 = vbbox
|
82 |
+
cv2.rectangle(res, (x1, y1), (x2, y2), (0, 255, 0), 1)
|
83 |
+
alpha = 0.8
|
84 |
+
color = (255, 0, 0)
|
85 |
+
for c in range(3):
|
86 |
+
res[vy1:vy2, vx1:vx2,
|
87 |
+
c] = res[vy1:vy2, vx1:vx2,
|
88 |
+
c] * alpha + color[c] * (1.0 - alpha)
|
89 |
+
cv2.circle(res, (vx1, vy1), 1, color, 2)
|
90 |
+
cv2.circle(res, (vx1, vy2), 1, color, 2)
|
91 |
+
cv2.circle(res, (vx2, vy1), 1, color, 2)
|
92 |
+
cv2.circle(res, (vx2, vy2), 1, color, 2)
|
93 |
+
return res
|
94 |
+
|
95 |
+
|
96 |
+
def detect(image: np.ndarray, detector) -> np.ndarray:
|
97 |
+
image = image[:, :, ::-1] # RGB -> BGR
|
98 |
+
bboxes, vbboxes = detect_person(image, detector)
|
99 |
+
res = visualize(image, bboxes, vbboxes)
|
100 |
+
return res[:, :, ::-1] # BGR -> RGB
|
101 |
+
|
102 |
+
|
103 |
+
def main():
|
104 |
+
gr.close_all()
|
105 |
+
|
106 |
+
args = parse_args()
|
107 |
+
|
108 |
+
detector = load_model()
|
109 |
+
detector.prepare(-1, nms_thresh=0.5, input_size=(640, 640))
|
110 |
+
|
111 |
+
func = functools.partial(detect, detector=detector)
|
112 |
+
func = functools.update_wrapper(func, detect)
|
113 |
+
|
114 |
+
image_dir = pathlib.Path('images')
|
115 |
+
examples = [[path.as_posix()] for path in sorted(image_dir.glob('*.jpg'))]
|
116 |
+
|
117 |
+
gr.Interface(
|
118 |
+
func,
|
119 |
+
gr.inputs.Image(type='numpy', label='Input'),
|
120 |
+
gr.outputs.Image(type='numpy', label='Output'),
|
121 |
+
examples=examples,
|
122 |
+
examples_per_page=30,
|
123 |
+
title=TITLE,
|
124 |
+
description=DESCRIPTION,
|
125 |
+
article=ARTICLE,
|
126 |
+
theme=args.theme,
|
127 |
+
allow_screenshot=args.allow_screenshot,
|
128 |
+
allow_flagging=args.allow_flagging,
|
129 |
+
live=args.live,
|
130 |
+
).launch(
|
131 |
+
enable_queue=args.enable_queue,
|
132 |
+
server_port=args.port,
|
133 |
+
share=args.share,
|
134 |
+
)
|
135 |
+
|
136 |
+
|
137 |
+
if __name__ == '__main__':
|
138 |
+
main()
|
images/README.md
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
These images are from the following public domain:
|
2 |
+
|
3 |
+
https://www.pexels.com/photo/coworkers-taking-a-coffee-break-4427813/
|
4 |
+
https://www.pexels.com/photo/woman-in-brown-sweater-walking-on-the-lakeside-4456152/
|
5 |
+
https://www.pexels.com/photo/woman-in-brown-long-sleeve-sweater-standing-on-beach-4456142/
|
6 |
+
https://www.pexels.com/photo/black-and-white-photo-of-a-person-walking-with-a-child-5835863/
|
7 |
+
https://www.pexels.com/photo/woman-holding-surfboard-walking-on-a-beach-9167601/
|
8 |
+
https://www.pexels.com/photo/backview-of-two-people-walking-near-a-brick-building-11596703/
|
9 |
+
https://www.pexels.com/photo/girl-in-red-jacket-riding-bicycle-5792907/
|
10 |
+
https://www.pexels.com/photo/woman-in-yellow-dress-walking-on-the-side-of-the-street-4943547/
|
11 |
+
https://www.pexels.com/photo/man-and-woman-looking-at-earch-other-812258/
|
12 |
+
https://www.pexels.com/photo/beautiful-woman-in-a-red-dress-wearing-red-lipstick-7909580/
|
13 |
+
https://www.pexels.com/photo/silhouette-of-person-standing-on-beach-during-sunset-3492736/
|
14 |
+
https://www.pexels.com/photo/woman-wearing-a-white-gown-walking-on-grass-field-8574605/
|
15 |
+
https://www.pexels.com/photo/three-geisha-walking-between-buildings-1325837/
|
16 |
+
https://www.pexels.com/photo/people-wearing-backpacks-walking-on-pathway-near-green-leaf-plants-917510/
|
17 |
+
https://www.pexels.com/photo/man-doing-airborne-stunt-1701195/
|
18 |
+
https://www.pexels.com/photo/man-practicing-handstand-on-city-street-5368976/
|
19 |
+
https://www.pexels.com/photo/pretty-woman-doing-backbend-6800242/
|
20 |
+
https://www.pexels.com/photo/woman-balancing-her-body-on-a-handstand-using-one-hand-5770708/
|
21 |
+
https://www.pexels.com/photo/women-in-active-wear-balancing-their-body-while-leaning-by-the-doorway-5770445/
|
22 |
+
https://www.pexels.com/photo/people-watching-acrobats-on-the-street-5567802/
|
23 |
+
https://www.pexels.com/photo/persons-in-black-shirt-and-pants-690598/
|
24 |
+
https://www.pexels.com/photo/woman-jumping-wearing-green-1930364/
|
25 |
+
https://www.pexels.com/photo/smiling-man-standing-and-dancing-near-smiling-woman-surrounded-with-people-2240771/
|
26 |
+
https://www.pexels.com/photo/unrecognizable-ethnic-man-dancing-break-dance-on-embankment-5368927/
|
27 |
+
https://www.pexels.com/photo/photo-of-woman-doing-a-ballet-dance-1164975/
|
images/pexels-allan-mas-5368927.jpg
ADDED
Git LFS Details
|
images/pexels-allan-mas-5368976.jpg
ADDED
Git LFS Details
|
images/pexels-ana-bregantin-1930364.jpg
ADDED
Git LFS Details
|
images/pexels-anastasiya-gepp-4456142.jpg
ADDED
Git LFS Details
|
images/pexels-anastasiya-gepp-4456152.jpg
ADDED
Git LFS Details
|
images/pexels-august-de-richelieu-4427813.jpg
ADDED
Git LFS Details
|
images/pexels-cottonbro-5770445.jpg
ADDED
Git LFS Details
|
images/pexels-cottonbro-5770708.jpg
ADDED
Git LFS Details
|
images/pexels-cottonbro-6800242.jpg
ADDED
Git LFS Details
|
images/pexels-dmitriy-ganin-9167601.jpg
ADDED
Git LFS Details
|
images/pexels-haste-leart-v-690598.jpg
ADDED
Git LFS Details
|
images/pexels-jake-_sulli_-swoyer-5835863.jpg
ADDED
Git LFS Details
|
images/pexels-jasmine-carter-812258.jpg
ADDED
Git LFS Details
|
images/pexels-jo-kassis-5567802.jpg
ADDED
Git LFS Details
|
images/pexels-kaichieh-chan-917510.jpg
ADDED
Git LFS Details
|
images/pexels-luis-gallegos-alvarez-1164975.jpg
ADDED
Git LFS Details
|
images/pexels-lukas-rodriguez-3492736.jpg
ADDED
Git LFS Details
|
images/pexels-martin-lopez-2240771.jpg
ADDED
Git LFS Details
|
images/pexels-mehmet-turgut-kirkgoz-11596703.jpg
ADDED
Git LFS Details
|
images/pexels-nataliya-vaitkevich-4943547.jpg
ADDED
Git LFS Details
|
images/pexels-satoshi-hirayama-1325837.jpg
ADDED
Git LFS Details
|
images/pexels-victoria-borodinova-7909580.jpg
ADDED
Git LFS Details
|
images/pexels-yan-krukov-5792907.jpg
ADDED
Git LFS Details
|
images/pexels-yogendra-singh-1701195.jpg
ADDED
Git LFS Details
|
images/pexels-лиза-медведева-8574605.jpg
ADDED
Git LFS Details
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Cython==0.29.28
|
2 |
+
#insightface==0.6.2
|
3 |
+
numpy==1.22.3
|
4 |
+
onnxruntime==1.11.0
|
5 |
+
opencv-python-headless==4.5.5.64
|