Ahsen Khaliq
commited on
Commit
·
0826a4c
1
Parent(s):
ff2e8c8
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import mediapipe as mp
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import cv2
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
# Images
|
| 8 |
+
torch.hub.download_url_to_file('https://artbreeder.b-cdn.net/imgs/c789e54661bfb432c5522a36553f.jpeg', 'face1.jpg')
|
| 9 |
+
torch.hub.download_url_to_file('https://artbreeder.b-cdn.net/imgs/c86622e8cb58d490e35b01cb9996.jpeg', 'face2.jpg')
|
| 10 |
+
|
| 11 |
+
mp_face_mesh = mp.solutions.face_mesh
|
| 12 |
+
|
| 13 |
+
# Prepare DrawingSpec for drawing the face landmarks later.
|
| 14 |
+
mp_drawing = mp.solutions.drawing_utils
|
| 15 |
+
drawing_spec = mp_drawing.DrawingSpec(thickness=1, circle_radius=1)
|
| 16 |
+
|
| 17 |
+
# Run MediaPipe Face Mesh.
|
| 18 |
+
|
| 19 |
+
def inference(image):
|
| 20 |
+
with mp_face_mesh.FaceMesh(
|
| 21 |
+
static_image_mode=True,
|
| 22 |
+
max_num_faces=2,
|
| 23 |
+
min_detection_confidence=0.5) as face_mesh:
|
| 24 |
+
# Convert the BGR image to RGB and process it with MediaPipe Face Mesh.
|
| 25 |
+
results = face_mesh.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
| 26 |
+
|
| 27 |
+
annotated_image = image.copy()
|
| 28 |
+
for face_landmarks in results.multi_face_landmarks:
|
| 29 |
+
mp_drawing.draw_landmarks(
|
| 30 |
+
image=annotated_image,
|
| 31 |
+
landmark_list=face_landmarks,
|
| 32 |
+
connections=mp_face_mesh.FACE_CONNECTIONS,
|
| 33 |
+
landmark_drawing_spec=drawing_spec,
|
| 34 |
+
connection_drawing_spec=drawing_spec)
|
| 35 |
+
return annotated_image
|
| 36 |
+
|
| 37 |
+
title = "Face Mesh"
|
| 38 |
+
description = "demo for Face Mesh. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below."
|
| 39 |
+
article = "<p style='text-align: center'><a href='https://arxiv.org/abs/1907.06724'>Real-time Facial Surface Geometry from Monocular Video on Mobile GPUs</a> | <a href='https://github.com/google/mediapipe'>Github Repo</a></p>"
|
| 40 |
+
|
| 41 |
+
gr.Interface(
|
| 42 |
+
inference,
|
| 43 |
+
[gr.inputs.Image(label="Input")],
|
| 44 |
+
gr.outputs.Image(type="pil", label="Output"),
|
| 45 |
+
title=title,
|
| 46 |
+
description=description,
|
| 47 |
+
article=article,
|
| 48 |
+
examples=[
|
| 49 |
+
["face1.jpg"],
|
| 50 |
+
["face2.jpg"]
|
| 51 |
+
]).launch(debug=True)
|