Skip to content
Advertisement

How to remove specific hand landmarks with mediapipe (Remove thumb)?

I want to remove the thumb from the Hand landmark tracking Mediapipe model to only track my 4 fingers without the thumb. How can I do that?

Here’s what I have done so far and I was able to remove the unwanted CONNECTIONS by defining a custom connections list. But still landmark dots/points visible. How can I remove them?. Thank You.

need to remove thumb landmark points

import cv2
import mediapipe as mp

mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
handConnection = [(5, 9), (10, 11), (5, 6), (15, 16), (13, 17), (18, 19),
            (6, 7), (0, 17), (9, 10), (0, 5), (14, 15),
            (11, 12), (19, 20), (9, 13), (17, 18), (13, 14), (7, 8)]

cap = cv2.VideoCapture(1)

def main():
    hands = mp_hands.Hands(
        min_detection_confidence=0.7, min_tracking_confidence=0.7)
    hand_landmark_drawing_spec = mp_drawing.DrawingSpec(thickness=5, circle_radius=5)
    hand_connection_drawing_spec = mp_drawing.DrawingSpec(thickness=10, circle_radius=10)

    while cap.isOpened():
        ret, image = cap.read()
        image = cv2.flip(image, 1)
        image = cv2.cvtColor(cv2.flip(image, 1), cv2.COLOR_BGR2RGB)
        results_hand = hands.process(image)
        image.flags.writeable = True
        image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
        if results_hand.multi_hand_landmarks:
            for hand_landmarks in results_hand.multi_hand_landmarks:
                mp_drawing.draw_landmarks(
                    image=image,
                    landmark_list=hand_landmarks,
                    connections=handConnection,
                    landmark_drawing_spec=hand_landmark_drawing_spec,
                    connection_drawing_spec=hand_connection_drawing_spec)

        keypress = cv2.waitKey(1)
        if keypress == ord('c'):
            break
        cv2.imshow("Img", image)

    hands.close()
    cap.release()

main()

Advertisement

Answer

You could just change the drawing function. To do that, open drawing_utils.py, go to draw_landmarks()and at the beginning of the for loop that iterates over the landmarks (for idx, landmark in enumerate(landmark_list.landmark):) and those two lines (where in the square bracket you would put the indices that you want to skip

if idx in [0, 1, 2, 3, 4]:
  continue

so instead of this:

  for idx, landmark in enumerate(landmark_list.landmark):
    if ((landmark.HasField('visibility') and
         landmark.visibility < _VISIBILITY_THRESHOLD) or
        (landmark.HasField('presence') and
         landmark.presence < _PRESENCE_THRESHOLD)):
      continue
    landmark_px = _normalized_to_pixel_coordinates(landmark.x, landmark.y,
                                                   image_cols, image_rows)
    if landmark_px:
      idx_to_coordinates[idx] = landmark_px

you should have this:

  for idx, landmark in enumerate(landmark_list.landmark):
    if idx in [10, 11, 7, 3, 4]:
      continue
    if ((landmark.HasField('visibility') and
         landmark.visibility < _VISIBILITY_THRESHOLD) or
        (landmark.HasField('presence') and
         landmark.presence < _PRESENCE_THRESHOLD)):
      continue
    landmark_px = _normalized_to_pixel_coordinates(landmark.x, landmark.y,
                                                   image_cols, image_rows)
    if landmark_px:
      idx_to_coordinates[idx] = landmark_px
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement