1

I want to recognize the pose in the video using Mediapipe.

There is nothing wrong with using the example code, but when using a method to recognize only certain landmarks, AttributeError: 'NoneType' object has no attribute 'landmark' errors continue (when it comes to webcam, it works well).

How should I fix it? I'm sorry for showing the wrong code :(

import cv2
import mediapipe
import numpy
from mediapipe.framework.formats import landmark_pb2 as mediapipe_landmarks     

mediapipe_drawing = mediapipe.solutions.drawing_utils               
mediapipe_drawing_styles = mediapipe.solutions.drawing_styles       
mediapipe_pose = mediapipe.solutions.pose                           
bodyconnections = [(0,1), (0,6), (0,2), (2,4), (1,7), (1,3), (3,5), 
(6,7), (6,8), (8,10), (7,9), (9,11)]

cap = cv2.VideoCapture('/Users/dee_to/Documents/Pro-pose/ditto1.mp4')

with mediapipe_pose.Pose(
    min_detection_confidence = 0.2,     
    min_tracking_confidence = 0.2,      
    model_complexity = 1,               
    ) as pose :

    while cap.isOpened() :
        success, image = cap.read()
        if not success :
            print("camera error")
            break

        image.flags.writeable = False
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        results = pose.process(image)

        landmark_subset = mediapipe_landmarks.NormalizedLandmarkList(
        landmark=[   
            results.pose_landmarks.landmark[11],
            results.pose_landmarks.landmark[12],
            results.pose_landmarks.landmark[13],
            results.pose_landmarks.landmark[14],
            results.pose_landmarks.landmark[15],
            results.pose_landmarks.landmark[16],
            results.pose_landmarks.landmark[23],
            results.pose_landmarks.landmark[24],
            results.pose_landmarks.landmark[25],
            results.pose_landmarks.landmark[26],
            results.pose_landmarks.landmark[27],
            results.pose_landmarks.landmark[28]
                ]
        )
        
        image.flags.writeable = True
        image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
        mediapipe_drawing.draw_landmarks(
            image,
            landmark_subset
            )
        
        poses = landmark_subset.landmark
        for i in range(0, len(bodyconnections)):
            start_idx = [
                poses[bodyconnections[i][0]].x,
                poses[bodyconnections[i][0]].y
            ]

            end_idx = [
                poses[bodyconnections[i][1]].x,
                poses[bodyconnections[i][1]].y
            ]
            IMG_HEIGHT, IMG_WIDTH = image.shape[:2]

            cv2.line(image,
                tuple(numpy.multiply(start_idx[:2], [
                    IMG_WIDTH, IMG_HEIGHT]).astype(int)),
                tuple(numpy.multiply(end_idx[:2], [
                    IMG_WIDTH, IMG_HEIGHT]).astype(int)),
                (255, 0, 0), 9)
        
        cv2.imshow('Pose_Check', cv2.flip(image, 1))
        if cv2.waitKey(5) & 0xFF == ord('q'):
            break
cap.release()

Here is the error:

INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
Traceback (most recent call last):
  File "/Users/dee_to/Documents/Pro-pose/pose-py", line 40, in <module>
    results.pose_landmarks.landmark[11],
AttributeError: `NoneType` object has no attribute 'landmark'
Sylvester Kruin
  • 3,294
  • 5
  • 16
  • 39
dee_to
  • 11
  • 2
  • 1
    Does this answer your question? [Why do I get AttributeError: 'NoneType' object has no attribute 'something'?](https://stackoverflow.com/questions/8949252/why-do-i-get-attributeerror-nonetype-object-has-no-attribute-something) At the very least, this should help you improve your question after gaining some understanding. – Ulrich Eckhardt Jan 19 '23 at 18:52

1 Answers1

0

Looking at the code, it seems like you just need to display a subset of landmarks. If this is the case, I suggest you 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.

I have described this approach here: https://stackoverflow.com/a/75427369/10571624

As for your code - if all landmarks have been successfully recognised by mediapipe, it works fine; however, an exception (no attribute 'landmark') occurs if no landmarks have been recognised in the image (for example, if the image does not contain a person). It can be solved by checking results.pose_landmarks for None, but draw_landmarks() takes care of these edge cases, so I still would recommend using it.

Stefan
  • 355
  • 2
  • 6