hysts HF Staff commited on
Commit
88236cc
·
1 Parent(s): 3557bc1
Files changed (2) hide show
  1. app.py +107 -0
  2. requirements.txt +1 -0
app.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 tarfile
10
+ import urllib
11
+
12
+ import cv2
13
+ import gradio as gr
14
+ import huggingface_hub
15
+ import numpy as np
16
+
17
+ TOKEN = os.environ['TOKEN']
18
+
19
+
20
+ def parse_args() -> argparse.Namespace:
21
+ parser = argparse.ArgumentParser()
22
+ parser.add_argument('--theme', type=str)
23
+ parser.add_argument('--live', action='store_true')
24
+ parser.add_argument('--share', action='store_true')
25
+ parser.add_argument('--port', type=int)
26
+ parser.add_argument('--disable-queue',
27
+ dest='enable_queue',
28
+ action='store_false')
29
+ parser.add_argument('--allow-flagging', type=str, default='never')
30
+ parser.add_argument('--allow-screenshot', action='store_true')
31
+ return parser.parse_args()
32
+
33
+
34
+ def load_sample_image_paths() -> list[pathlib.Path]:
35
+ image_dir = pathlib.Path('images')
36
+ if not image_dir.exists():
37
+ dataset_repo = 'hysts/sample-images-TADNE'
38
+ path = huggingface_hub.hf_hub_download(dataset_repo,
39
+ 'images.tar.gz',
40
+ repo_type='dataset',
41
+ use_auth_token=TOKEN)
42
+ with tarfile.open(path) as f:
43
+ f.extractall()
44
+ return sorted(image_dir.glob('*'))
45
+
46
+
47
+ def load_model() -> cv2.CascadeClassifier:
48
+ url = 'https://raw.githubusercontent.com/nagadomi/lbpcascade_animeface/master/lbpcascade_animeface.xml'
49
+ path = pathlib.Path('lbpcascade_animeface.xml')
50
+ if not path.exists():
51
+ urllib.request.urlretrieve(url, path.as_posix())
52
+ return cv2.CascadeClassifier(path.as_posix())
53
+
54
+
55
+ def detect(image, detector: cv2.CascadeClassifier) -> np.ndarray:
56
+ image = cv2.imread(image.name)
57
+ gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
58
+ preds = detector.detectMultiScale(gray,
59
+ scaleFactor=1.1,
60
+ minNeighbors=5,
61
+ minSize=(24, 24))
62
+
63
+ res = image.copy()
64
+ for x, y, w, h in preds:
65
+ cv2.rectangle(res, (x, y), (x + w, y + h), (0, 255, 0), 2)
66
+ res = cv2.cvtColor(res, cv2.COLOR_BGR2RGB)
67
+ return res
68
+
69
+
70
+ def main():
71
+ gr.close_all()
72
+
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
+ repo_url = 'https://github.com/nagadomi/lbpcascade_animeface'
83
+ title = 'nagadomi/lbpcascade_animeface'
84
+ description = f'A demo for {repo_url}'
85
+ article = None
86
+
87
+ gr.Interface(
88
+ func,
89
+ gr.inputs.Image(type='file', label='Input'),
90
+ gr.outputs.Image(label='Output'),
91
+ theme=args.theme,
92
+ title=title,
93
+ description=description,
94
+ article=article,
95
+ examples=examples,
96
+ allow_screenshot=args.allow_screenshot,
97
+ allow_flagging=args.allow_flagging,
98
+ live=args.live,
99
+ ).launch(
100
+ enable_queue=args.enable_queue,
101
+ server_port=args.port,
102
+ share=args.share,
103
+ )
104
+
105
+
106
+ if __name__ == '__main__':
107
+ main()
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ opencv-python-headless>=4.5.5.62