hysts HF staff commited on
Commit
516d9b1
1 Parent(s): 7187f16
.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

  • SHA256: 1f7e71063ae685ce07e852d865214ab953f3a7ac177d0b7aef1b871e050f153f
  • Pointer size: 132 Bytes
  • Size of remote file: 1.29 MB
images/pexels-allan-mas-5368976.jpg ADDED

Git LFS Details

  • SHA256: e1fff29e72471249f8f7f6bcca616110e7ebde91ab348c249ccaeaf612f5a285
  • Pointer size: 132 Bytes
  • Size of remote file: 1.09 MB
images/pexels-ana-bregantin-1930364.jpg ADDED

Git LFS Details

  • SHA256: 645a3d7a1faa01a84e8757b16640ab16ba59e491e8f4628afb4c283e93ea8060
  • Pointer size: 131 Bytes
  • Size of remote file: 769 kB
images/pexels-anastasiya-gepp-4456142.jpg ADDED

Git LFS Details

  • SHA256: c4c2d18eac65bc5efdea5a3b82edf6690639bbd3a0d2d75043bc63a1779c9aae
  • Pointer size: 131 Bytes
  • Size of remote file: 465 kB
images/pexels-anastasiya-gepp-4456152.jpg ADDED

Git LFS Details

  • SHA256: 951fa56defaff817e84bfbe2873a2e6cb637f57b8184ad1aa429fdd6be87485f
  • Pointer size: 131 Bytes
  • Size of remote file: 450 kB
images/pexels-august-de-richelieu-4427813.jpg ADDED

Git LFS Details

  • SHA256: 8ac717e71960822105cf143dadd33dd1f64dcb3556facb8b95f7a16b56e58831
  • Pointer size: 131 Bytes
  • Size of remote file: 382 kB
images/pexels-cottonbro-5770445.jpg ADDED

Git LFS Details

  • SHA256: b4548cd4a16238f559a149670c6ad2606b3b2147c92e5a2a380dd12fd922f276
  • Pointer size: 131 Bytes
  • Size of remote file: 379 kB
images/pexels-cottonbro-5770708.jpg ADDED

Git LFS Details

  • SHA256: 951720e6bb6053756ef555e5fcae4b54927582c4974e5908ea1984a9f14d7843
  • Pointer size: 131 Bytes
  • Size of remote file: 478 kB
images/pexels-cottonbro-6800242.jpg ADDED

Git LFS Details

  • SHA256: 4c2d9b5c0d319b28b9a570448a808c406fd8ff1ee6c9b8e5bf498495ba718391
  • Pointer size: 131 Bytes
  • Size of remote file: 638 kB
images/pexels-dmitriy-ganin-9167601.jpg ADDED

Git LFS Details

  • SHA256: a252eb80d56dbcc617f9a95ffc1608b918c0fb6920b68f666645b8e379b50402
  • Pointer size: 131 Bytes
  • Size of remote file: 713 kB
images/pexels-haste-leart-v-690598.jpg ADDED

Git LFS Details

  • SHA256: 90009cbaceb3c3802d0df460862434e446e5cfad7892986444146ce73a02f61c
  • Pointer size: 131 Bytes
  • Size of remote file: 329 kB
images/pexels-jake-_sulli_-swoyer-5835863.jpg ADDED

Git LFS Details

  • SHA256: 29ced69fdd9e9423f1d75fae0642d44fa55dbe10a429f679e88a4605fe4d44c0
  • Pointer size: 131 Bytes
  • Size of remote file: 585 kB
images/pexels-jasmine-carter-812258.jpg ADDED

Git LFS Details

  • SHA256: 6781bc53cdacae742b514dec3794a5a31472744b24e01304f23737dc50927214
  • Pointer size: 132 Bytes
  • Size of remote file: 1.02 MB
images/pexels-jo-kassis-5567802.jpg ADDED

Git LFS Details

  • SHA256: c4b65df7e312c086b571143a6a7443c7873afa8a51ad1c1299223ce9f8a2ee57
  • Pointer size: 131 Bytes
  • Size of remote file: 319 kB
images/pexels-kaichieh-chan-917510.jpg ADDED

Git LFS Details

  • SHA256: 394c428578e1e919aa3c3905b6ea8d151669e1a7271ce45f68961c863a8688ee
  • Pointer size: 131 Bytes
  • Size of remote file: 681 kB
images/pexels-luis-gallegos-alvarez-1164975.jpg ADDED

Git LFS Details

  • SHA256: 05cb7605dbac48915eee1b6ef0de3aba386abb7ab06ef27d58c092df2c76a176
  • Pointer size: 131 Bytes
  • Size of remote file: 553 kB
images/pexels-lukas-rodriguez-3492736.jpg ADDED

Git LFS Details

  • SHA256: 48576b2149bb5e4258c4d0bcea19e37c5365413c8b7d63795102fdf0f7f94966
  • Pointer size: 131 Bytes
  • Size of remote file: 625 kB
images/pexels-martin-lopez-2240771.jpg ADDED

Git LFS Details

  • SHA256: 93ab345cfc87577ce8610d23d21ae2393e5db7f85d471cd2a5277f8f68d432bc
  • Pointer size: 131 Bytes
  • Size of remote file: 582 kB
images/pexels-mehmet-turgut-kirkgoz-11596703.jpg ADDED

Git LFS Details

  • SHA256: 4512c64b43e70adf13a7641d0a097ab7f99b801a71fc676c98a39c61d9508f3e
  • Pointer size: 131 Bytes
  • Size of remote file: 426 kB
images/pexels-nataliya-vaitkevich-4943547.jpg ADDED

Git LFS Details

  • SHA256: 3cdb6c92b9dc95e3c933e29f38a2f6ef4aa981b33625d329472f0d389d2ad594
  • Pointer size: 132 Bytes
  • Size of remote file: 1.1 MB
images/pexels-satoshi-hirayama-1325837.jpg ADDED

Git LFS Details

  • SHA256: 5ac7a2cff79c6e9174ed2b127f63886ed3e661c9fe85269d4cd00f07adc8236d
  • Pointer size: 131 Bytes
  • Size of remote file: 451 kB
images/pexels-victoria-borodinova-7909580.jpg ADDED

Git LFS Details

  • SHA256: c05ceaf9c468dd21d24977f2c50e3f3b9b1ba83474d93180f66496635216b573
  • Pointer size: 131 Bytes
  • Size of remote file: 279 kB
images/pexels-yan-krukov-5792907.jpg ADDED

Git LFS Details

  • SHA256: 0500121b9044cb1d4c7913e48ebe5e2374848d57d6a2905f3b7c9469f959f2fe
  • Pointer size: 131 Bytes
  • Size of remote file: 648 kB
images/pexels-yogendra-singh-1701195.jpg ADDED

Git LFS Details

  • SHA256: d4d2b51bd8db1b464512ea9d41debd666b81c1ce873febe7267c082b0dd2fe6c
  • Pointer size: 131 Bytes
  • Size of remote file: 152 kB
images/pexels-лиза-медведева-8574605.jpg ADDED

Git LFS Details

  • SHA256: 85cf4db499f0c5b11397af648e66178a4e40e6d478f1e6b31ade35e225ff6ceb
  • Pointer size: 131 Bytes
  • Size of remote file: 816 kB
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