Given the following example:

import cv2 #OpenCV is the library that we will be using for image processing import mediapipe as mp #Mediapipe is the framework that will allow us to get our pose estimation import time


mpDraw = mp.solutions.drawing_utils mpPose = mp.solutions.pose

pose = mpPose.Pose()
#pose = mpPose.Pose(static_image_mode = False, upper_body_only = True) #ONLY UPPER_BODY_TRACKING

#cap = cv2.VideoCapture(0) cap = cv2.VideoCapture('PoseVideos/1_girl_choreography.mp4')

pTime = 0 #previous time

while True:
    success, img = cap.read() #that will give it our image and then we can write the cv2.imshow()
    imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) #convert our image to RGB because Mediapipe use that format
    results = pose.process(imgRGB) #we are simply going to send this image to our model

    #print(enumerate(results.pose_landmarks.landmark)) #<enumerate object at 0x0000012312DD1A00>

    #so then we will check if it is detected or not
    if results.pose_landmarks:

        mpDraw.draw_landmarks(img, results.pose_landmarks, mpPose.POSE_CONNECTIONS) 

        for id, lm in enumerate(results.pose_landmarks.landmark):

            h, w, c = img.shape #get dimensions(h height, w width) and the c channel of image
            
            print(id)
            print(lm)

            cx, cy = int(lm.x * w), int(lm.y * h)

            cv2.circle(img, (cx, cy), 5, (255, 0, 0), cv2.FILLED)


    cTime = time.time()
    fps = 1 / (cTime - pTime)
    pTime = cTime

    cv2.putText(img, str(int(fps)), (70, 50), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 0), 3)

    cv2.imshow("Image", img)
    cv2.waitKey(1)

I do not want to draw all the keypoints in results.pose_landmarks, but I would like to remove the first 10 points.

Basically I would like to do the following

mpDraw.draw_landmarks(img, results.pose_landmarks[10:], mpPose.POSE_CONNECTIONS) 

Doing that, I get the following error:

landmark_list = keypoints.pose_landmarks[10:], 
TypeError: 'NormalizedLandmarkList' object is not subscriptable

Any idea, how to remove the first 10 element from pose_landmarks?

1

There are 1 best solutions below

0
On

I assume you are trying to draw all the landmarks except for the face (thus [10:]). I suggest you to use a solution I have described here: Mediapipe Display Body Landmarks Only. You can use the standard draw_landmarks() function and simply adjust the drawing parameters (landmark_drawing_spec and connections) so that unnecessary landmarks and connections are not displayed.

Regarding the slice: you can get a valid slice of the landmarks:

from mediapipe.framework.formats import landmark_pb2
landmark_subset = landmark_pb2.NormalizedLandmarkList(
    landmark = results.pose_landmarks.landmark[10:])

And draw them using:

mpDraw.draw_landmarks(
            annotated_image,
            landmark_subset)

This, however, does not solve the problem with connections: providing mp_pose.POSE_CONNECTIONS to draw_landmarks() would cause an exception Landmark index is out of range. Unfortunately, it is not enough to slice the connections (list(mpPose.POSE_CONNECTIONS)[10:]), since they are in a different order than the landmarks.