hysts HF staff commited on
Commit
08ba7c3
1 Parent(s): b4f1daa
Files changed (4) hide show
  1. .pre-commit-config.yaml +35 -0
  2. .style.yapf +5 -0
  3. README.md +1 -1
  4. app.py +23 -53
.pre-commit-config.yaml ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ repos:
2
+ - repo: https://github.com/pre-commit/pre-commit-hooks
3
+ rev: v4.2.0
4
+ hooks:
5
+ - id: check-executables-have-shebangs
6
+ - id: check-json
7
+ - id: check-merge-conflict
8
+ - id: check-shebang-scripts-are-executable
9
+ - id: check-toml
10
+ - id: check-yaml
11
+ - id: double-quote-string-fixer
12
+ - id: end-of-file-fixer
13
+ - id: mixed-line-ending
14
+ args: ['--fix=lf']
15
+ - id: requirements-txt-fixer
16
+ - id: trailing-whitespace
17
+ - repo: https://github.com/myint/docformatter
18
+ rev: v1.4
19
+ hooks:
20
+ - id: docformatter
21
+ args: ['--in-place']
22
+ - repo: https://github.com/pycqa/isort
23
+ rev: 5.12.0
24
+ hooks:
25
+ - id: isort
26
+ - repo: https://github.com/pre-commit/mirrors-mypy
27
+ rev: v0.991
28
+ hooks:
29
+ - id: mypy
30
+ args: ['--ignore-missing-imports']
31
+ - repo: https://github.com/google/yapf
32
+ rev: v0.32.0
33
+ hooks:
34
+ - id: yapf
35
+ args: ['--parallel', '--in-place']
.style.yapf ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ [style]
2
+ based_on_style = pep8
3
+ blank_line_before_nested_class_or_def = false
4
+ spaces_before_comment = 2
5
+ split_before_logical_operator = true
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 😻
4
  colorFrom: blue
5
  colorTo: yellow
6
  sdk: gradio
7
- sdk_version: 3.0.5
8
  app_file: app.py
9
  pinned: false
10
  ---
 
4
  colorFrom: blue
5
  colorTo: yellow
6
  sdk: gradio
7
+ sdk_version: 3.19.1
8
  app_file: app.py
9
  pinned: false
10
  ---
app.py CHANGED
@@ -2,14 +2,14 @@
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
@@ -20,34 +20,21 @@ import onnxruntime as ort
20
 
21
  TITLE = 'insightface Person Detection'
22
  DESCRIPTION = 'This is an unofficial demo for https://github.com/deepinsight/insightface/tree/master/examples/person_detection.'
23
- ARTICLE = '<center><img src="https://visitor-badge.glitch.me/badge?page_id=hysts.insightface-person-detection" alt="visitor badge"/></center>'
24
 
25
- TOKEN = os.environ['TOKEN']
26
-
27
-
28
- def parse_args() -> argparse.Namespace:
29
- parser = argparse.ArgumentParser()
30
- parser.add_argument('--theme', type=str)
31
- parser.add_argument('--live', action='store_true')
32
- parser.add_argument('--share', action='store_true')
33
- parser.add_argument('--port', type=int)
34
- parser.add_argument('--disable-queue',
35
- dest='enable_queue',
36
- action='store_false')
37
- parser.add_argument('--allow-flagging', type=str, default='never')
38
- return parser.parse_args()
39
 
40
 
41
  def load_model():
42
  path = huggingface_hub.hf_hub_download('hysts/insightface',
43
  'models/scrfd_person_2.5g.onnx',
44
- use_auth_token=TOKEN)
45
  options = ort.SessionOptions()
46
  options.intra_op_num_threads = 8
47
  options.inter_op_num_threads = 8
48
- session = ort.InferenceSession(path,
49
- sess_options=options,
50
- providers=['CPUExecutionProvider'])
 
51
  model = insightface.model_zoo.retinaface.RetinaFace(model_file=path,
52
  session=session)
53
  return model
@@ -98,36 +85,19 @@ def detect(image: np.ndarray, detector) -> np.ndarray:
98
  return res[:, :, ::-1] # BGR -> RGB
99
 
100
 
101
- def main():
102
- args = parse_args()
103
-
104
- detector = load_model()
105
- detector.prepare(-1, nms_thresh=0.5, input_size=(640, 640))
106
-
107
- func = functools.partial(detect, detector=detector)
108
- func = functools.update_wrapper(func, detect)
109
-
110
- image_dir = pathlib.Path('images')
111
- examples = [[path.as_posix()] for path in sorted(image_dir.glob('*.jpg'))]
112
-
113
- gr.Interface(
114
- func,
115
- gr.inputs.Image(type='numpy', label='Input'),
116
- gr.outputs.Image(type='numpy', label='Output'),
117
- examples=examples,
118
- examples_per_page=30,
119
- title=TITLE,
120
- description=DESCRIPTION,
121
- article=ARTICLE,
122
- theme=args.theme,
123
- allow_flagging=args.allow_flagging,
124
- live=args.live,
125
- ).launch(
126
- enable_queue=args.enable_queue,
127
- server_port=args.port,
128
- share=args.share,
129
- )
130
 
 
 
131
 
132
- if __name__ == '__main__':
133
- main()
 
 
 
 
 
 
 
 
2
 
3
  from __future__ import annotations
4
 
 
5
  import functools
6
  import os
7
  import pathlib
8
+ import shlex
9
  import subprocess
10
 
11
+ if os.getenv('SYSTEM') == 'spaces':
12
+ subprocess.call(shlex.split('pip install insightface==0.6.2'))
13
 
14
  import cv2
15
  import gradio as gr
 
20
 
21
  TITLE = 'insightface Person Detection'
22
  DESCRIPTION = 'This is an unofficial demo for https://github.com/deepinsight/insightface/tree/master/examples/person_detection.'
 
23
 
24
+ HF_TOKEN = os.getenv('HF_TOKEN')
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
 
27
  def load_model():
28
  path = huggingface_hub.hf_hub_download('hysts/insightface',
29
  'models/scrfd_person_2.5g.onnx',
30
+ use_auth_token=HF_TOKEN)
31
  options = ort.SessionOptions()
32
  options.intra_op_num_threads = 8
33
  options.inter_op_num_threads = 8
34
+ session = ort.InferenceSession(
35
+ path,
36
+ sess_options=options,
37
+ providers=['CPUExecutionProvider', 'CUDAExecutionProvider'])
38
  model = insightface.model_zoo.retinaface.RetinaFace(model_file=path,
39
  session=session)
40
  return model
 
85
  return res[:, :, ::-1] # BGR -> RGB
86
 
87
 
88
+ detector = load_model()
89
+ detector.prepare(-1, nms_thresh=0.5, input_size=(640, 640))
90
+ func = functools.partial(detect, detector=detector)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
 
92
+ image_dir = pathlib.Path('images')
93
+ examples = [[path.as_posix()] for path in sorted(image_dir.glob('*.jpg'))]
94
 
95
+ gr.Interface(
96
+ fn=func,
97
+ inputs=gr.Image(label='Input', type='numpy'),
98
+ outputs=gr.Image(label='Output', type='numpy'),
99
+ examples=examples,
100
+ examples_per_page=30,
101
+ title=TITLE,
102
+ description=DESCRIPTION,
103
+ ).launch(show_api=False)