hysts HF Staff commited on
Commit
21db550
·
1 Parent(s): 36137cd
Files changed (2) hide show
  1. app.py +92 -0
  2. requirements.txt +9 -0
app.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ from __future__ import annotations
4
+
5
+ import argparse
6
+ import functools
7
+ import pathlib
8
+ import subprocess
9
+
10
+ subprocess.call('pip uninstall -y opencv-python'.split())
11
+ subprocess.call('pip uninstall -y opencv-python-headless'.split())
12
+ subprocess.call('pip install opencv-python-headless==4.5.5.62'.split())
13
+
14
+ import cv2
15
+ import face_alignment
16
+ import gradio as gr
17
+ import numpy as np
18
+ import torch
19
+
20
+ ORIGINAL_REPO_URL = 'https://github.com/1adrianb/face-alignment'
21
+ TITLE = '1adrianb/face-alignment'
22
+ DESCRIPTION = f'A demo for {ORIGINAL_REPO_URL}'
23
+ ARTICLE = ''
24
+
25
+
26
+ def parse_args() -> argparse.Namespace:
27
+ parser = argparse.ArgumentParser()
28
+ parser.add_argument('--device', type=str, default='cpu')
29
+ parser.add_argument('--theme', type=str)
30
+ parser.add_argument('--live', action='store_true')
31
+ parser.add_argument('--share', action='store_true')
32
+ parser.add_argument('--port', type=int)
33
+ parser.add_argument('--disable-queue',
34
+ dest='enable_queue',
35
+ action='store_false')
36
+ parser.add_argument('--allow-flagging', type=str, default='never')
37
+ parser.add_argument('--allow-screenshot', action='store_true')
38
+ return parser.parse_args()
39
+
40
+
41
+ def detect(
42
+ image: np.ndarray,
43
+ detector,
44
+ device: torch.device,
45
+ ) -> np.ndarray:
46
+ preds = detector.get_landmarks(image)
47
+ if len(preds) == 0:
48
+ raise RuntimeError('No face was found')
49
+
50
+ res = image.copy()
51
+ for pts in preds:
52
+ for pt in np.round(pts).astype(int):
53
+ cv2.circle(res, tuple(pt), 2, (0, 255, 0), cv2.FILLED)
54
+ return res
55
+
56
+
57
+ def main():
58
+ gr.close_all()
59
+
60
+ args = parse_args()
61
+ device = torch.device(args.device)
62
+
63
+ detector = face_alignment.FaceAlignment(face_alignment.LandmarksType._2D,
64
+ device=device.type)
65
+
66
+ func = functools.partial(detect, detector=detector, device=device)
67
+ func = functools.update_wrapper(func, detect)
68
+
69
+ image_paths = sorted(pathlib.Path('images').glob('*.jpg'))
70
+ examples = [[path.as_posix()] for path in image_paths]
71
+
72
+ gr.Interface(
73
+ func,
74
+ gr.inputs.Image(type='numpy', label='Input'),
75
+ gr.outputs.Image(type='numpy', label='Output'),
76
+ examples=examples,
77
+ theme=args.theme,
78
+ title=TITLE,
79
+ description=DESCRIPTION,
80
+ article=ARTICLE,
81
+ allow_screenshot=args.allow_screenshot,
82
+ allow_flagging=args.allow_flagging,
83
+ live=args.live,
84
+ ).launch(
85
+ enable_queue=args.enable_queue,
86
+ server_port=args.port,
87
+ share=args.share,
88
+ )
89
+
90
+
91
+ if __name__ == '__main__':
92
+ main()
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ face-alignment==1.3.5
2
+ numba==0.55.1
3
+ numpy==1.21.5
4
+ opencv-python-headless==4.5.5.62
5
+ Pillow==9.0.1
6
+ scikit-image==0.19.2
7
+ scipy==1.8.0
8
+ torch==1.11.0
9
+ torchvision==0.12.0