hysts HF Staff commited on
Commit
5458b42
·
1 Parent(s): 00e4207
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 +20 -51
.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: green
5
  colorTo: gray
6
  sdk: gradio
7
- sdk_version: 3.0.5
8
  app_file: app.py
9
  pinned: false
10
  ---
 
4
  colorFrom: green
5
  colorTo: gray
6
  sdk: gradio
7
+ sdk_version: 3.19.1
8
  app_file: app.py
9
  pinned: false
10
  ---
app.py CHANGED
@@ -2,7 +2,6 @@
2
 
3
  from __future__ import annotations
4
 
5
- import argparse
6
  import functools
7
  import os
8
  import pathlib
@@ -16,22 +15,8 @@ import numpy as np
16
 
17
  TITLE = 'nagadomi/lbpcascade_animeface'
18
  DESCRIPTION = 'This is an unofficial demo for https://github.com/nagadomi/lbpcascade_animeface.'
19
- ARTICLE = '<center><img src="https://visitor-badge.glitch.me/badge?page_id=hysts.lbpcascade_animeface" alt="visitor badge"/></center>'
20
 
21
- TOKEN = os.environ['TOKEN']
22
-
23
-
24
- def parse_args() -> argparse.Namespace:
25
- parser = argparse.ArgumentParser()
26
- parser.add_argument('--theme', type=str)
27
- parser.add_argument('--live', action='store_true')
28
- parser.add_argument('--share', action='store_true')
29
- parser.add_argument('--port', type=int)
30
- parser.add_argument('--disable-queue',
31
- dest='enable_queue',
32
- action='store_false')
33
- parser.add_argument('--allow-flagging', type=str, default='never')
34
- return parser.parse_args()
35
 
36
 
37
  def load_sample_image_paths() -> list[pathlib.Path]:
@@ -41,7 +26,7 @@ def load_sample_image_paths() -> list[pathlib.Path]:
41
  path = huggingface_hub.hf_hub_download(dataset_repo,
42
  'images.tar.gz',
43
  repo_type='dataset',
44
- use_auth_token=TOKEN)
45
  with tarfile.open(path) as f:
46
  f.extractall()
47
  return sorted(image_dir.glob('*'))
@@ -55,47 +40,31 @@ def load_model() -> cv2.CascadeClassifier:
55
  return cv2.CascadeClassifier(path.as_posix())
56
 
57
 
58
- def detect(image, detector: cv2.CascadeClassifier) -> np.ndarray:
59
- image = cv2.imread(image.name)
60
- gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
61
  preds = detector.detectMultiScale(gray,
62
  scaleFactor=1.1,
63
  minNeighbors=5,
64
  minSize=(24, 24))
65
 
66
- res = image.copy()
67
  for x, y, w, h in preds:
68
  cv2.rectangle(res, (x, y), (x + w, y + h), (0, 255, 0), 2)
69
  return res[:, :, ::-1]
70
 
71
 
72
- def main():
73
- args = parse_args()
74
-
75
- image_paths = load_sample_image_paths()
76
- examples = [[path.as_posix()] for path in image_paths]
77
-
78
- detector = load_model()
79
- func = functools.partial(detect, detector=detector)
80
- func = functools.update_wrapper(func, detect)
81
-
82
- gr.Interface(
83
- func,
84
- gr.inputs.Image(type='file', label='Input'),
85
- gr.outputs.Image(type='numpy', label='Output'),
86
- examples=examples,
87
- title=TITLE,
88
- description=DESCRIPTION,
89
- article=ARTICLE,
90
- theme=args.theme,
91
- allow_flagging=args.allow_flagging,
92
- live=args.live,
93
- ).launch(
94
- enable_queue=args.enable_queue,
95
- server_port=args.port,
96
- share=args.share,
97
- )
98
-
99
-
100
- if __name__ == '__main__':
101
- main()
 
2
 
3
  from __future__ import annotations
4
 
 
5
  import functools
6
  import os
7
  import pathlib
 
15
 
16
  TITLE = 'nagadomi/lbpcascade_animeface'
17
  DESCRIPTION = 'This is an unofficial demo for https://github.com/nagadomi/lbpcascade_animeface.'
 
18
 
19
+ HF_TOKEN = os.getenv('HF_TOKEN')
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
 
22
  def load_sample_image_paths() -> list[pathlib.Path]:
 
26
  path = huggingface_hub.hf_hub_download(dataset_repo,
27
  'images.tar.gz',
28
  repo_type='dataset',
29
+ use_auth_token=HF_TOKEN)
30
  with tarfile.open(path) as f:
31
  f.extractall()
32
  return sorted(image_dir.glob('*'))
 
40
  return cv2.CascadeClassifier(path.as_posix())
41
 
42
 
43
+ def detect(image_path: str, detector: cv2.CascadeClassifier) -> np.ndarray:
44
+ image_path = cv2.imread(image_path)
45
+ gray = cv2.cvtColor(image_path, cv2.COLOR_BGR2GRAY)
46
  preds = detector.detectMultiScale(gray,
47
  scaleFactor=1.1,
48
  minNeighbors=5,
49
  minSize=(24, 24))
50
 
51
+ res = image_path.copy()
52
  for x, y, w, h in preds:
53
  cv2.rectangle(res, (x, y), (x + w, y + h), (0, 255, 0), 2)
54
  return res[:, :, ::-1]
55
 
56
 
57
+ image_paths = load_sample_image_paths()
58
+ examples = [[path.as_posix()] for path in image_paths]
59
+
60
+ detector = load_model()
61
+ func = functools.partial(detect, detector=detector)
62
+
63
+ gr.Interface(
64
+ fn=func,
65
+ inputs=gr.Image(label='Input', type='filepath'),
66
+ outputs=gr.Image(label='Output', type='numpy'),
67
+ examples=examples,
68
+ title=TITLE,
69
+ description=DESCRIPTION,
70
+ ).queue().launch(show_api=False)