hysts HF Staff commited on
Commit
6ec8547
·
1 Parent(s): 2b9d0e6
Files changed (3) hide show
  1. .gitignore +1 -0
  2. app.py +147 -0
  3. requirements.txt +3 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ images
app.py ADDED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
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
18
+ import mediapipe as mp
19
+ import numpy as np
20
+
21
+ mp_drawing = mp.solutions.drawing_utils
22
+ mp_drawing_styles = mp.solutions.drawing_styles
23
+ mp_face_mesh = mp.solutions.face_mesh
24
+
25
+ TITLE = 'MediaPipe Face Mesh'
26
+ DESCRIPTION = 'https://google.github.io/mediapipe/'
27
+ ARTICLE = None
28
+
29
+ TOKEN = os.environ['TOKEN']
30
+
31
+
32
+ def parse_args() -> argparse.Namespace:
33
+ parser = argparse.ArgumentParser()
34
+ parser.add_argument('--theme', type=str)
35
+ parser.add_argument('--live', action='store_true')
36
+ parser.add_argument('--share', action='store_true')
37
+ parser.add_argument('--port', type=int)
38
+ parser.add_argument('--disable-queue',
39
+ dest='enable_queue',
40
+ action='store_false')
41
+ parser.add_argument('--allow-flagging', type=str, default='never')
42
+ parser.add_argument('--allow-screenshot', action='store_true')
43
+ return parser.parse_args()
44
+
45
+
46
+ def load_sample_images() -> list[pathlib.Path]:
47
+ image_dir = pathlib.Path('images')
48
+ if not image_dir.exists():
49
+ image_dir.mkdir()
50
+ dataset_repo = 'hysts/input-images'
51
+ filenames = ['001.tar', '005.tar']
52
+ for name in filenames:
53
+ path = huggingface_hub.hf_hub_download(dataset_repo,
54
+ name,
55
+ repo_type='dataset',
56
+ use_auth_token=TOKEN)
57
+ with tarfile.open(path) as f:
58
+ f.extractall(image_dir.as_posix())
59
+ return sorted(image_dir.rglob('*.jpg'))
60
+
61
+
62
+ def run(
63
+ image: np.ndarray,
64
+ max_num_faces: int,
65
+ min_detection_confidence: float,
66
+ show_tesselation: bool,
67
+ show_contours: bool,
68
+ show_irises: bool,
69
+ ) -> np.ndarray:
70
+ with mp_face_mesh.FaceMesh(
71
+ static_image_mode=True,
72
+ max_num_faces=max_num_faces,
73
+ refine_landmarks=True,
74
+ min_detection_confidence=min_detection_confidence) as face_mesh:
75
+ results = face_mesh.process(image)
76
+
77
+ res = image[:, :, ::-1].copy()
78
+ if results.multi_face_landmarks is not None:
79
+ for face_landmarks in results.multi_face_landmarks:
80
+ if show_tesselation:
81
+ mp_drawing.draw_landmarks(
82
+ image=res,
83
+ landmark_list=face_landmarks,
84
+ connections=mp_face_mesh.FACEMESH_TESSELATION,
85
+ landmark_drawing_spec=None,
86
+ connection_drawing_spec=mp_drawing_styles.
87
+ get_default_face_mesh_tesselation_style())
88
+ if show_contours:
89
+ mp_drawing.draw_landmarks(
90
+ image=res,
91
+ landmark_list=face_landmarks,
92
+ connections=mp_face_mesh.FACEMESH_CONTOURS,
93
+ landmark_drawing_spec=None,
94
+ connection_drawing_spec=mp_drawing_styles.
95
+ get_default_face_mesh_contours_style())
96
+ if show_irises:
97
+ mp_drawing.draw_landmarks(
98
+ image=res,
99
+ landmark_list=face_landmarks,
100
+ connections=mp_face_mesh.FACEMESH_IRISES,
101
+ landmark_drawing_spec=None,
102
+ connection_drawing_spec=mp_drawing_styles.
103
+ get_default_face_mesh_iris_connections_style())
104
+
105
+ return res[:, :, ::-1]
106
+
107
+
108
+ def main():
109
+ args = parse_args()
110
+
111
+ image_paths = load_sample_images()
112
+ examples = [[path.as_posix(), 5, 0.5, True, True, True]
113
+ for path in image_paths]
114
+
115
+ gr.Interface(
116
+ run,
117
+ [
118
+ gr.inputs.Image(type='numpy', label='Input'),
119
+ gr.inputs.Slider(
120
+ 0, 10, step=1, default=5, label='Max Number of Faces'),
121
+ gr.inputs.Slider(0,
122
+ 1,
123
+ step=0.05,
124
+ default=0.5,
125
+ label='Minimum Detection Confidence'),
126
+ gr.inputs.Checkbox(default=True, label='Show Tesselation'),
127
+ gr.inputs.Checkbox(default=True, label='Show Contours'),
128
+ gr.inputs.Checkbox(default=True, label='Show Irises'),
129
+ ],
130
+ gr.outputs.Image(type='numpy', label='Output'),
131
+ examples=examples,
132
+ title=TITLE,
133
+ description=DESCRIPTION,
134
+ article=ARTICLE,
135
+ theme=args.theme,
136
+ allow_screenshot=args.allow_screenshot,
137
+ allow_flagging=args.allow_flagging,
138
+ live=args.live,
139
+ ).launch(
140
+ enable_queue=args.enable_queue,
141
+ server_port=args.port,
142
+ share=args.share,
143
+ )
144
+
145
+
146
+ if __name__ == '__main__':
147
+ main()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ mediapipe==0.8.9.1
2
+ numpy==1.22.3
3
+ opencv-python-headless==4.5.5.64