hysts HF staff commited on
Commit
d3223c3
·
1 Parent(s): a18b545
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 +34 -62
.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: yellow
5
  colorTo: blue
6
  sdk: gradio
7
- sdk_version: 3.0.5
8
  app_file: app.py
9
  pinned: false
10
  ---
 
4
  colorFrom: yellow
5
  colorTo: blue
6
  sdk: gradio
7
+ sdk_version: 3.19.1
8
  app_file: app.py
9
  pinned: false
10
  ---
app.py CHANGED
@@ -2,16 +2,17 @@
2
 
3
  from __future__ import annotations
4
 
5
- import argparse
6
  import os
7
  import pathlib
 
8
  import subprocess
9
  import tarfile
10
 
11
  if os.environ.get('SYSTEM') == 'spaces':
12
- subprocess.call('pip uninstall -y opencv-python'.split())
13
- subprocess.call('pip uninstall -y opencv-python-headless'.split())
14
- subprocess.call('pip install opencv-python-headless==4.5.5.64'.split())
 
15
 
16
  import gradio as gr
17
  import huggingface_hub
@@ -23,22 +24,8 @@ mp_drawing = mp.solutions.drawing_utils
23
 
24
  TITLE = 'MediaPipe Face Detection'
25
  DESCRIPTION = 'https://google.github.io/mediapipe/'
26
- ARTICLE = '<center><img src="https://visitor-badge.glitch.me/badge?page_id=hysts.mediapipe-face-detection" alt="visitor badge"/></center>'
27
 
28
- TOKEN = os.environ['TOKEN']
29
-
30
-
31
- def parse_args() -> argparse.Namespace:
32
- parser = argparse.ArgumentParser()
33
- parser.add_argument('--theme', type=str)
34
- parser.add_argument('--live', action='store_true')
35
- parser.add_argument('--share', action='store_true')
36
- parser.add_argument('--port', type=int)
37
- parser.add_argument('--disable-queue',
38
- dest='enable_queue',
39
- action='store_false')
40
- parser.add_argument('--allow-flagging', type=str, default='never')
41
- return parser.parse_args()
42
 
43
 
44
  def load_sample_images() -> list[pathlib.Path]:
@@ -51,7 +38,7 @@ def load_sample_images() -> list[pathlib.Path]:
51
  path = huggingface_hub.hf_hub_download(dataset_repo,
52
  name,
53
  repo_type='dataset',
54
- use_auth_token=TOKEN)
55
  with tarfile.open(path) as f:
56
  f.extractall(image_dir.as_posix())
57
  return sorted(image_dir.rglob('*.jpg'))
@@ -72,45 +59,30 @@ def run(image: np.ndarray, model_selection: int,
72
  return res[:, :, ::-1]
73
 
74
 
75
- def main():
76
- args = parse_args()
77
-
78
- model_types = [
79
- 'Short-range model (best for faces within 2 meters)',
80
- 'Full-range model (best for faces within 5 meters)',
81
- ]
82
-
83
- image_paths = load_sample_images()
84
- examples = [[path.as_posix(), model_types[0], 0.5] for path in image_paths]
85
-
86
- gr.Interface(
87
- run,
88
- [
89
- gr.inputs.Image(type='numpy', label='Input'),
90
- gr.inputs.Radio(model_types,
91
- type='index',
92
- default=model_types[0],
93
- label='Model'),
94
- gr.inputs.Slider(0,
95
- 1,
96
- step=0.05,
97
- default=0.5,
98
- label='Minimum Detection Confidence'),
99
- ],
100
- gr.outputs.Image(type='numpy', label='Output'),
101
- examples=examples,
102
- title=TITLE,
103
- description=DESCRIPTION,
104
- article=ARTICLE,
105
- theme=args.theme,
106
- allow_flagging=args.allow_flagging,
107
- live=args.live,
108
- ).launch(
109
- enable_queue=args.enable_queue,
110
- server_port=args.port,
111
- share=args.share,
112
- )
113
-
114
-
115
- if __name__ == '__main__':
116
- main()
 
2
 
3
  from __future__ import annotations
4
 
 
5
  import os
6
  import pathlib
7
+ import shlex
8
  import subprocess
9
  import tarfile
10
 
11
  if os.environ.get('SYSTEM') == 'spaces':
12
+ subprocess.call(shlex.split('pip uninstall -y opencv-python'))
13
+ subprocess.call(shlex.split('pip uninstall -y opencv-python-headless'))
14
+ subprocess.call(
15
+ shlex.split('pip install opencv-python-headless==4.5.5.64'))
16
 
17
  import gradio as gr
18
  import huggingface_hub
 
24
 
25
  TITLE = 'MediaPipe Face Detection'
26
  DESCRIPTION = 'https://google.github.io/mediapipe/'
 
27
 
28
+ HF_TOKEN = os.getenv('HF_TOKEN')
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
 
31
  def load_sample_images() -> list[pathlib.Path]:
 
38
  path = huggingface_hub.hf_hub_download(dataset_repo,
39
  name,
40
  repo_type='dataset',
41
+ use_auth_token=HF_TOKEN)
42
  with tarfile.open(path) as f:
43
  f.extractall(image_dir.as_posix())
44
  return sorted(image_dir.rglob('*.jpg'))
 
59
  return res[:, :, ::-1]
60
 
61
 
62
+ model_types = [
63
+ 'Short-range model (best for faces within 2 meters)',
64
+ 'Full-range model (best for faces within 5 meters)',
65
+ ]
66
+
67
+ image_paths = load_sample_images()
68
+ examples = [[path.as_posix(), model_types[0], 0.5] for path in image_paths]
69
+
70
+ gr.Interface(
71
+ fn=run,
72
+ inputs=[
73
+ gr.Image(label='Input', type='numpy'),
74
+ gr.Radio(label='Model',
75
+ choices=model_types,
76
+ type='index',
77
+ value=model_types[0]),
78
+ gr.Slider(label='Minimum Detection Confidence',
79
+ minimum=0,
80
+ maximum=1,
81
+ step=0.05,
82
+ value=0.5),
83
+ ],
84
+ outputs=gr.Image(label='Output', type='numpy'),
85
+ examples=examples,
86
+ title=TITLE,
87
+ description=DESCRIPTION,
88
+ ).launch(show_api=False)