hysts HF staff commited on
Commit
b325e7d
1 Parent(s): c84d3f6
Files changed (5) hide show
  1. .gitignore +1 -0
  2. .gitmodules +3 -0
  3. app.py +113 -0
  4. onnx-facial-lmk-detector +1 -0
  5. requirements.txt +3 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
1
+ images
.gitmodules ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ [submodule "onnx-facial-lmk-detector"]
2
+ path = onnx-facial-lmk-detector
3
+ url = https://github.com/atksh/onnx-facial-lmk-detector
app.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
11
+ import cv2
12
+ import gradio as gr
13
+ import huggingface_hub
14
+ import numpy as np
15
+ import onnxruntime as ort
16
+
17
+ TITLE = 'atksh/onnx-facial-lmk-detector'
18
+ DESCRIPTION = 'This is a demo for https://github.com/atksh/onnx-facial-lmk-detector.'
19
+ ARTICLE = None
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
+ parser.add_argument('--allow-screenshot', action='store_true')
35
+ return parser.parse_args()
36
+
37
+
38
+ def load_sample_images() -> list[pathlib.Path]:
39
+ image_dir = pathlib.Path('images')
40
+ if not image_dir.exists():
41
+ image_dir.mkdir()
42
+ dataset_repo = 'hysts/input-images'
43
+ filenames = ['001.tar']
44
+ for name in filenames:
45
+ path = huggingface_hub.hf_hub_download(dataset_repo,
46
+ name,
47
+ repo_type='dataset',
48
+ use_auth_token=TOKEN)
49
+ with tarfile.open(path) as f:
50
+ f.extractall(image_dir.as_posix())
51
+ return sorted(image_dir.rglob('*.jpg'))
52
+
53
+
54
+ def run(image: np.ndarray, sess: ort.InferenceSession) -> np.ndarray:
55
+ # float32, int, int, uint8, int, float32
56
+ # (N,), (N, 4), (N, 5, 2), (N, 224, 224, 3), (N, 106, 2), (N, 2, 3)
57
+ scores, bboxes, keypoints, aligned_images, landmarks, affine_matrices = sess.run(
58
+ None, {'input': image[:, :, ::-1]})
59
+
60
+ res = image[:, :, ::-1].copy()
61
+ for box in bboxes:
62
+ cv2.rectangle(res, tuple(box[:2]), tuple(box[2:]), (0, 255, 0), 1)
63
+ for pts in landmarks:
64
+ for pt in pts:
65
+ cv2.circle(res, tuple(pt), 1, (255, 255, 0), cv2.FILLED)
66
+
67
+ return res[:, :, ::-1], [face[:, :, ::-1] for face in aligned_images]
68
+
69
+
70
+ def main():
71
+ gr.close_all()
72
+
73
+ args = parse_args()
74
+
75
+ options = ort.SessionOptions()
76
+ options.intra_op_num_threads = 8
77
+ options.inter_op_num_threads = 8
78
+ sess = ort.InferenceSession('onnx-facial-lmk-detector/model.onnx',
79
+ sess_options=options,
80
+ providers=['CPUExecutionProvider'])
81
+
82
+ func = functools.partial(run, sess=sess)
83
+ func = functools.update_wrapper(func, run)
84
+
85
+ image_paths = load_sample_images()
86
+ examples = ['onnx-facial-lmk-detector/input.jpg'
87
+ ] + [[path.as_posix()] for path in image_paths]
88
+
89
+ gr.Interface(
90
+ func,
91
+ gr.inputs.Image(type='numpy', label='Input'),
92
+ [
93
+ gr.outputs.Image(type='numpy', label='Output'),
94
+ gr.outputs.Carousel(gr.outputs.Image(type='numpy'),
95
+ label='Aligned Faces'),
96
+ ],
97
+ examples=examples,
98
+ title=TITLE,
99
+ description=DESCRIPTION,
100
+ article=ARTICLE,
101
+ theme=args.theme,
102
+ allow_screenshot=args.allow_screenshot,
103
+ allow_flagging=args.allow_flagging,
104
+ live=args.live,
105
+ ).launch(
106
+ enable_queue=args.enable_queue,
107
+ server_port=args.port,
108
+ share=args.share,
109
+ )
110
+
111
+
112
+ if __name__ == '__main__':
113
+ main()
onnx-facial-lmk-detector ADDED
@@ -0,0 +1 @@
 
1
+ Subproject commit b92419b1cfa726e4105cdf7f0863926dd39690ba
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ numpy==1.22.3
2
+ onnxruntime==1.11.0
3
+ opencv-python-headless==4.5.5.64