Created
May 19, 2023 02:52
-
-
Save megarubber/90a9da7534c7f68a6d6bc774d67ec642 to your computer and use it in GitHub Desktop.
Detect a simple face landmarks using Google's Mediapipe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy | |
import math | |
import cv2 | |
import mediapipe as mp | |
DESIRED_HEIGHT = 480 | |
DESIRED_WIDTH = 480 | |
localImages = [ | |
r'/home/guilherme/Projects/mediapipe/teste.png' | |
] | |
mp_face_mesh = mp.solutions.face_mesh | |
mp_drawing = mp.solutions.drawing_utils | |
mp_drawing_styles = mp.solutions.drawing_styles | |
def resize_and_show(image): | |
h, w = image.shape[:2] | |
img = cv2.resize( | |
image, | |
(DESIRED_WIDTH, math.floor(h/(w/DESIRED_WIDTH))) if h < w | |
else (math.floor(w/(h/DESIRED_HEIGHT)), DESIRED_HEIGHT) | |
) | |
cv2.imshow('Teste', img) | |
cv2.waitKey(0) | |
cv2.destroyAllWindows() | |
images = {name: cv2.imread(name) for name in localImages} | |
with mp_face_mesh.FaceMesh( | |
static_image_mode=True, | |
refine_landmarks=True, | |
max_num_faces=2, | |
min_detection_confidence=0.5 | |
) as face_mesh: | |
for name, image in images.items(): | |
results = face_mesh.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) | |
if not results.multi_face_landmarks: | |
continue | |
annot_image = image.copy() | |
for face_landmarks in results.multi_face_landmarks: | |
mp_drawing.draw_landmarks( | |
image=annot_image, | |
landmark_list=face_landmarks, | |
connections=mp_face_mesh.FACEMESH_TESSELATION, | |
landmark_drawing_spec=None, | |
connection_drawing_spec=mp_drawing_styles.get_default_face_mesh_tesselation_style()) | |
resize_and_show(annot_image) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment