akhaliq/FaceMesh
45
1import mediapipe as mp2import gradio as gr3import cv24import torch5 6 7# Images8torch.hub.download_url_to_file('https://artbreeder.b-cdn.net/imgs/c789e54661bfb432c5522a36553f.jpeg', 'face1.jpg')9torch.hub.download_url_to_file('https://artbreeder.b-cdn.net/imgs/c86622e8cb58d490e35b01cb9996.jpeg', 'face2.jpg')10 11mp_face_mesh = mp.solutions.face_mesh12 13# Prepare DrawingSpec for drawing the face landmarks later.14mp_drawing = mp.solutions.drawing_utils 15drawing_spec = mp_drawing.DrawingSpec(thickness=1, circle_radius=1)16 17# Run MediaPipe Face Mesh.18 19def 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.FACEMESH_TESSELATION,33 landmark_drawing_spec=drawing_spec,34 connection_drawing_spec=drawing_spec)35 return annotated_image36 37title = "Face Mesh"38description = "Gradio 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."39article = "<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 41gr.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()