Skip to content

Instantly share code, notes, and snippets.

@khalidmeister
Last active February 16, 2023 02:06
Show Gist options
  • Save khalidmeister/4667ba516aa96eea250032c26806e2af to your computer and use it in GitHub Desktop.
Save khalidmeister/4667ba516aa96eea250032c26806e2af to your computer and use it in GitHub Desktop.
# IMPORTING LIBRARIES
import cv2
import mediapipe as mp
# INITIALIZING OBJECTS
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_face_mesh = mp.solutions.face_mesh
drawing_spec = mp_drawing.DrawingSpec(thickness=1, circle_radius=1)
cap = cv2.VideoCapture(0)
# DETECT THE FACE LANDMARKS
with mp_face_mesh.FaceMesh(min_detection_confidence=0.5, min_tracking_confidence=0.5) as face_mesh:
while True:
success, image = cap.read()
# Flip the image horizontally and convert the color space from BGR to RGB
image = cv2.cvtColor(cv2.flip(image, 1), cv2.COLOR_BGR2RGB)
# To improve performance
image.flags.writeable = False
# Detect the face landmarks
results = face_mesh.process(image)
# To improve performance
image.flags.writeable = True
# Convert back to the BGR color space
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
# Draw the face mesh annotations on the image.
if results.multi_face_landmarks:
for face_landmarks in results.multi_face_landmarks:
mp_drawing.draw_landmarks(
image=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())
# Display the image
cv2.imshow('MediaPipe FaceMesh', image)
# Terminate the process
if cv2.waitKey(5) & 0xFF == 27:
break
cap.release()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment