-1

I have this code for taking measurements of a human body using a web camera. But I'm keep having the following error.

Traceback (most recent call last):
  File "c:/Users/user/Desktop/Body detect/measurements4.py", line 23, in <module>
    results = pose.process(gray)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\mediapipe\python\solutions\pose.py", line 185, in process
    results = super().process(input_data={'image': image})
  File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\mediapipe\python\solution_base.py", line 353, in
process
    if data.shape[2] != RGB_CHANNELS:
IndexError: tuple index out of range

I don't now how to fix it.

This is my code:

import cv2
import mediapipe as mp
import math

# Set up Mediapipe Pose model
mp_pose = mp.solutions.pose
pose = mp_pose.Pose(static_image_mode=False, min_detection_confidence=0.5, min_tracking_confidence=0.5)

# Capture video from webcam
cap = cv2.VideoCapture(0)

while True:
    # Read a frame from the webcam
    ret, frame = cap.read()

    # Flip the frame horizontally
    frame = cv2.flip(frame, 1)

    # Convert the frame to grayscale
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Detect the pose landmarks in the frame
    results = pose.process(gray)

    # Check if any pose landmarks were detected
    if results.pose_landmarks:
        # Extract the coordinates of the shoulder, elbow, wrist, and chest landmarks
        landmarks = results.pose_landmarks.landmark
        shoulder_left = landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER].x, landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER].y
        shoulder_right = landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER].x, landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER].y
        elbow_left = landmarks[mp_pose.PoseLandmark.LEFT_ELBOW].x, landmarks[mp_pose.PoseLandmark.LEFT_ELBOW].y
        elbow_right = landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW].x, landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW].y
        wrist_left = landmarks[mp_pose.PoseLandmark.LEFT_WRIST].x, landmarks[mp_pose.PoseLandmark.LEFT_WRIST].y
        wrist_right = landmarks[mp_pose.PoseLandmark.RIGHT_WRIST].x, landmarks[mp_pose.PoseLandmark.RIGHT_WRIST].y
        chest = landmarks[mp_pose.PoseLandmark.MIDCHEST].x, landmarks[mp_pose.PoseLandmark.MIDCHEST].y

        # Calculate the distance between the shoulder joints to obtain the user's shoulder width in centimeters
        pixels_per_cm = 37  # adjust this value based on your camera and distance from camera to user
        shoulder_width = int(abs(shoulder_left[0] - shoulder_right[0]) * pixels_per_cm)

        # Calculate the distance between the elbow joints to obtain the user's arm length in centimeters
        arm_length_left = int(math.sqrt((elbow_left[0] - shoulder_left[0])**2 + (elbow_left[1] - shoulder_left[1])**2) * pixels_per_cm)
        arm_length_right = int(math.sqrt((elbow_right[0] - shoulder_right[0])**2 + (elbow_right[1] - shoulder_right[1])**2) * pixels_per_cm)

        # Measure the distance between the highest point of the shoulder joint to the bottom of the ribcage to obtain the user's upper body height in centimeters
        upper_body_height = int(abs(shoulder_left[1] - chest[1]) * pixels_per_cm)

        # Measure the distance between the chest points to obtain the user's chest size in centimeters
        chest_size = int(abs(chest[0] - chest[1]) * pixels_per_cm)

        # Display the measurements on the frame
        cv2.putText(frame, f"Shoulder width: {shoulder_width} cm", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.7 (0, 255, 0), 2)
        cv2.putText(frame, f"Arm length (left): {arm_length_left} cm", (50, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
        cv2.putText(frame, f"Arm length (right): {arm_length_right} cm", (50, 110), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
        cv2.putText(frame, f"Upper body height: {upper_body_height} cm", (50, 140), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
        cv2.putText(frame, f"Chest size: {chest_size} cm", (50, 170), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)

        # Show the frame
        cv2.imshow('Frame', frame)

    # Exit if the user presses the 'q' key
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release the webcam and close the window
cap.release()
cv2.destroyAllWindows()

I updated mediapipe library. but the problem still the same.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Anji_Ishu
  • 1
  • 1

1 Answers1

0

Try using:

image_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = pose.process(image_rgb)

From the examples on the media pipe pose webpage, it seems the function process expects an RGB image.

Also, what did you update in the mediapipe library? It is important to know before adding anything further.