opencv 如何解决索引错误:元组索引超出范围

mbjcgjjk  于 2023-03-13  发布在  其他
关注(0)|答案(1)|浏览(256)

我有这个代码使用网络摄像头测量人体。但我一直有以下错误。

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

我不知道怎么修理它。
这是我的代码:

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()

我更新了mediapipe库。但问题仍然相同。

iqjalb3h

iqjalb3h1#

尝试使用:

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

从媒体管道姿势网页上的示例来看,函数进程似乎需要RGB图像。
另外,您在mediapipe库中更新了什么?在进一步添加任何内容之前,了解这一点非常重要。

相关问题