CoolFace
Apppublic

johngoad/Face-Mesh

sourceHugging Faceupdated 4y agoView on Hugging Face
9likes
app.py52 linesDownload Raw Back to root
1#Face Mesh imported from akhaliq/Face_Mesh HuggingFace Repo2import mediapipe as mp3import gradio as gr4import cv25import torch6 7 8# Images9torch.hub.download_url_to_file('https://artbreeder.b-cdn.net/imgs/c789e54661bfb432c5522a36553f.jpeg', 'face1.jpg')10torch.hub.download_url_to_file('https://artbreeder.b-cdn.net/imgs/c86622e8cb58d490e35b01cb9996.jpeg', 'face2.jpg')11 12mp_face_mesh = mp.solutions.face_mesh13 14# Prepare DrawingSpec for drawing the face landmarks later.15mp_drawing = mp.solutions.drawing_utils 16drawing_spec = mp_drawing.DrawingSpec(thickness=1, circle_radius=1)17 18# Run MediaPipe Face Mesh.19 20def inference(image):21    with mp_face_mesh.FaceMesh(22        static_image_mode=True,23        max_num_faces=2,24        min_detection_confidence=0.5) as face_mesh:25        # Convert the BGR image to RGB and process it with MediaPipe Face Mesh.26        results = face_mesh.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))27 28        annotated_image = image.copy()29        for face_landmarks in results.multi_face_landmarks:30            mp_drawing.draw_landmarks(31                image=annotated_image,32                landmark_list=face_landmarks,33                connections=mp_face_mesh.FACEMESH_CONTOURS,34                landmark_drawing_spec=drawing_spec,35                connection_drawing_spec=drawing_spec)36            return annotated_image37 38title = "Face Mesh"39description = "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."40article = "<p style='text-align: center'><a href='https://arxiv.org/abs/1907.06724' target='_blank'>Real-time Facial Surface Geometry from Monocular Video on Mobile GPUs</a> | <a href='https://github.com/google/mediapipe' target='_blank'>Github Repo</a></p>"41 42gr.Interface(43    inference, 44    [gr.inputs.Image(label="Input")], 45    gr.outputs.Image(type="pil", label="Output"),46    title=title,47    description=description,48    article=article, 49    examples=[50              ["face1.jpg"],51              ["face2.jpg"]52    ]).launch(debug=True)