Skip to content

Instantly share code, notes, and snippets.

@njemanzedavid
Forked from secemp9/mediapipe_landmark.py
Created September 30, 2024 05:41
Show Gist options
  • Save njemanzedavid/3e1c85cab83c85d80b534d29a40d4b43 to your computer and use it in GitHub Desktop.
Save njemanzedavid/3e1c85cab83c85d80b534d29a40d4b43 to your computer and use it in GitHub Desktop.

Revisions

  1. @secemp9 secemp9 created this gist Sep 30, 2024.
    80 changes: 80 additions & 0 deletions mediapipe_landmark.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,80 @@
    import cv2
    import numpy as np
    import mediapipe as mp

    # Initialize mediapipe Face Mesh and Hands solutions
    mp_face_mesh = mp.solutions.face_mesh
    mp_hands = mp.solutions.hands
    mp_drawing = mp.solutions.drawing_utils
    mp_drawing_styles = mp.solutions.drawing_styles

    # Set up the webcam
    cap = cv2.VideoCapture(0)

    # Initialize Face Mesh and Hands
    with mp_face_mesh.FaceMesh(max_num_faces=1, refine_landmarks=True, min_detection_confidence=0.5, min_tracking_confidence=0.5) as face_mesh, \
    mp_hands.Hands(max_num_hands=2, min_detection_confidence=0.5, min_tracking_confidence=0.5) as hands:

    while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
    break

    # Convert the BGR image to RGB for processing
    rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

    # Process the face and hands in the frame
    face_results = face_mesh.process(rgb_frame)
    hand_results = hands.process(rgb_frame)

    # Create a black image to draw the landmarks on
    black_image = np.zeros(frame.shape, dtype=np.uint8)

    # Draw face landmarks if detected
    if face_results.multi_face_landmarks:
    for face_landmarks in face_results.multi_face_landmarks:
    mp_drawing.draw_landmarks(
    image=black_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())
    mp_drawing.draw_landmarks(
    image=black_image,
    landmark_list=face_landmarks,
    connections=mp_face_mesh.FACEMESH_CONTOURS,
    landmark_drawing_spec=None,
    connection_drawing_spec=mp_drawing_styles.get_default_face_mesh_contours_style())
    mp_drawing.draw_landmarks(
    image=black_image,
    landmark_list=face_landmarks,
    connections=mp_face_mesh.FACEMESH_IRISES,
    landmark_drawing_spec=None,
    connection_drawing_spec=mp_drawing_styles.get_default_face_mesh_iris_connections_style())

    # Draw hand landmarks if detected
    if hand_results.multi_hand_landmarks:
    for hand_landmarks in hand_results.multi_hand_landmarks:
    mp_drawing.draw_landmarks(
    image=black_image,
    landmark_list=hand_landmarks,
    connections=mp_hands.HAND_CONNECTIONS,
    landmark_drawing_spec=mp_drawing_styles.get_default_hand_landmarks_style(),
    connection_drawing_spec=mp_drawing_styles.get_default_hand_connections_style())

    # Resize the image to make it bigger
    scale_factor = 1.5 # Adjust this value to change the window size
    width = int(black_image.shape[1] * scale_factor)
    height = int(black_image.shape[0] * scale_factor)
    resized_image = cv2.resize(black_image, (width, height), interpolation=cv2.INTER_LINEAR)

    # Display the annotated image
    cv2.imshow('Face Mesh and Hands', resized_image)

    # Break the loop if 'q' is pressed
    if cv2.waitKey(1) & 0xFF == ord('q'):
    break

    # Release resources
    cap.release()
    cv2.destroyAllWindows()