test app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import pathlib
|
| 6 |
+
|
| 7 |
+
import gradio as gr
|
| 8 |
+
import mediapipe as mp
|
| 9 |
+
import numpy as np
|
| 10 |
+
|
| 11 |
+
mp_drawing = mp.solutions.drawing_utils
|
| 12 |
+
mp_drawing_styles = mp.solutions.drawing_styles
|
| 13 |
+
mp_face_mesh = mp.solutions.face_mesh
|
| 14 |
+
|
| 15 |
+
TITLE = "MediaPipe Face Mesh"
|
| 16 |
+
DESCRIPTION = "https://google.github.io/mediapipe/"
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def run(
|
| 20 |
+
image: np.ndarray,
|
| 21 |
+
max_num_faces: int,
|
| 22 |
+
min_detection_confidence: float,
|
| 23 |
+
show_tesselation: bool,
|
| 24 |
+
show_contours: bool,
|
| 25 |
+
show_irises: bool,
|
| 26 |
+
) -> np.ndarray:
|
| 27 |
+
with mp_face_mesh.FaceMesh(
|
| 28 |
+
static_image_mode=True,
|
| 29 |
+
max_num_faces=max_num_faces,
|
| 30 |
+
refine_landmarks=True,
|
| 31 |
+
min_detection_confidence=min_detection_confidence,
|
| 32 |
+
) as face_mesh:
|
| 33 |
+
results = face_mesh.process(image)
|
| 34 |
+
|
| 35 |
+
res = image[:, :, ::-1].copy()
|
| 36 |
+
if results.multi_face_landmarks is not None:
|
| 37 |
+
for face_landmarks in results.multi_face_landmarks:
|
| 38 |
+
if show_tesselation:
|
| 39 |
+
mp_drawing.draw_landmarks(
|
| 40 |
+
image=res,
|
| 41 |
+
landmark_list=face_landmarks,
|
| 42 |
+
connections=mp_face_mesh.FACEMESH_TESSELATION,
|
| 43 |
+
landmark_drawing_spec=None,
|
| 44 |
+
connection_drawing_spec=mp_drawing_styles.get_default_face_mesh_tesselation_style(),
|
| 45 |
+
)
|
| 46 |
+
if show_contours:
|
| 47 |
+
mp_drawing.draw_landmarks(
|
| 48 |
+
image=res,
|
| 49 |
+
landmark_list=face_landmarks,
|
| 50 |
+
connections=mp_face_mesh.FACEMESH_CONTOURS,
|
| 51 |
+
landmark_drawing_spec=None,
|
| 52 |
+
connection_drawing_spec=mp_drawing_styles.get_default_face_mesh_contours_style(),
|
| 53 |
+
)
|
| 54 |
+
if show_irises:
|
| 55 |
+
mp_drawing.draw_landmarks(
|
| 56 |
+
image=res,
|
| 57 |
+
landmark_list=face_landmarks,
|
| 58 |
+
connections=mp_face_mesh.FACEMESH_IRISES,
|
| 59 |
+
landmark_drawing_spec=None,
|
| 60 |
+
connection_drawing_spec=mp_drawing_styles.get_default_face_mesh_iris_connections_style(),
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
return res[:, :, ::-1]
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
image_paths = sorted(pathlib.Path("images").rglob("*.jpg"))
|
| 67 |
+
examples = [[path, 5, 0.5, True, True, True] for path in image_paths]
|
| 68 |
+
|
| 69 |
+
demo = gr.Interface(
|
| 70 |
+
fn=run,
|
| 71 |
+
inputs=[
|
| 72 |
+
gr.Image(label="Input", type="numpy"),
|
| 73 |
+
gr.Slider(label="Max Number of Faces", minimum=0, maximum=10, step=1, value=5),
|
| 74 |
+
gr.Slider(label="Minimum Detection Confidence", minimum=0, maximum=1, step=0.05, value=0.5),
|
| 75 |
+
gr.Checkbox(label="Show Tesselation", value=True),
|
| 76 |
+
gr.Checkbox(label="Show Contours", value=True),
|
| 77 |
+
gr.Checkbox(label="Show Irises", value=True),
|
| 78 |
+
],
|
| 79 |
+
outputs=gr.Image(label="Output"),
|
| 80 |
+
examples=examples,
|
| 81 |
+
title=TITLE,
|
| 82 |
+
description=DESCRIPTION,
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
if __name__ == "__main__":
|
| 86 |
+
demo.queue().launch()
|