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.
JavaScript
x
43
43
1
import cv2
2
import mediapipe as mp
3
4
mp_drawing = mp.solutions.drawing_utils
5
mp_hands = mp.solutions.hands
6
handConnection = [(5, 9), (10, 11), (5, 6), (15, 16), (13, 17), (18, 19),
7
(6, 7), (0, 17), (9, 10), (0, 5), (14, 15),
8
(11, 12), (19, 20), (9, 13), (17, 18), (13, 14), (7, 8)]
9
10
cap = cv2.VideoCapture(1)
11
12
def main():
13
hands = mp_hands.Hands(
14
min_detection_confidence=0.7, min_tracking_confidence=0.7)
15
hand_landmark_drawing_spec = mp_drawing.DrawingSpec(thickness=5, circle_radius=5)
16
hand_connection_drawing_spec = mp_drawing.DrawingSpec(thickness=10, circle_radius=10)
17
18
while cap.isOpened():
19
ret, image = cap.read()
20
image = cv2.flip(image, 1)
21
image = cv2.cvtColor(cv2.flip(image, 1), cv2.COLOR_BGR2RGB)
22
results_hand = hands.process(image)
23
image.flags.writeable = True
24
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
25
if results_hand.multi_hand_landmarks:
26
for hand_landmarks in results_hand.multi_hand_landmarks:
27
mp_drawing.draw_landmarks(
28
image=image,
29
landmark_list=hand_landmarks,
30
connections=handConnection,
31
landmark_drawing_spec=hand_landmark_drawing_spec,
32
connection_drawing_spec=hand_connection_drawing_spec)
33
34
keypress = cv2.waitKey(1)
35
if keypress == ord('c'):
36
break
37
cv2.imshow("Img", image)
38
39
hands.close()
40
cap.release()
41
42
main()
43
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
JavaScript
1
3
1
if idx in [0, 1, 2, 3, 4]:
2
continue
3
so instead of this:
JavaScript
1
11
11
1
for idx, landmark in enumerate(landmark_list.landmark):
2
if ((landmark.HasField('visibility') and
3
landmark.visibility < _VISIBILITY_THRESHOLD) or
4
(landmark.HasField('presence') and
5
landmark.presence < _PRESENCE_THRESHOLD)):
6
continue
7
landmark_px = _normalized_to_pixel_coordinates(landmark.x, landmark.y,
8
image_cols, image_rows)
9
if landmark_px:
10
idx_to_coordinates[idx] = landmark_px
11
you should have this:
JavaScript
1
13
13
1
for idx, landmark in enumerate(landmark_list.landmark):
2
if idx in [10, 11, 7, 3, 4]:
3
continue
4
if ((landmark.HasField('visibility') and
5
landmark.visibility < _VISIBILITY_THRESHOLD) or
6
(landmark.HasField('presence') and
7
landmark.presence < _PRESENCE_THRESHOLD)):
8
continue
9
landmark_px = _normalized_to_pixel_coordinates(landmark.x, landmark.y,
10
image_cols, image_rows)
11
if landmark_px:
12
idx_to_coordinates[idx] = landmark_px
13