Skip to content

How to draw the 3d points? #18

Description

@AbdulMoqeet

I am trying to visualize 3D human pose data using Matplotlib, but I’m unsure how to draw the skeleton correctly with proper orientation.

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

pose_data = np.array([[[[0.74705994, 1.33978031, 2.79437466],
                        [0.74739136, 1.33720988, 2.71716629],
                        [0.90648993, 1.34201772, 2.8765513 ],
                        [0.609125  , 1.2828455 , 2.87883011],
                        [0.98467566, 1.33480066, 3.12755246],
                        [0.84387189, 1.28449173, 3.30608122],
                        [0.54584106, 1.25342437, 3.13973339],
                        [0.68115039, 1.25501279, 3.30778954],
                        [1.04338965, 1.31156887, 3.40527568],
                        [0.85987115, 1.27624637, 3.68631802],
                        [0.54696472, 1.20856236, 3.43945209],
                        [0.67976501, 1.28844406, 3.68246365],
                        [0.82094916, 1.34407559, 4.03423926],
                        [0.67464734, 1.35550519, 4.03655939]]]])

points = np.squeeze(pose_data) # Shape is now (14, 3)

# 2. Define the keypoint names and skeleton connections
# This mapping is based on the 14-keypoint format used in the repository (similar to CrowdPose)
KEYPOINT_NAMES = [
    'left_shoulder', 'right_shoulder', 'left_elbow', 'right_elbow', 
    'left_wrist', 'right_wrist', 'left_hip', 'right_hip', 
    'left_knee', 'right_knee', 'left_ankle', 'right_ankle', 
    'head', 'neck'
]

# Define which points to connect to form the skeleton
SKELETON = [
    [12, 13],  # head -> neck
    [13, 0],   # neck -> left_shoulder
    [0, 2],    # left_shoulder -> left_elbow
    [2, 4],    # left_elbow -> left_wrist
    [13, 1],   # neck -> right_shoulder
    [1, 3],    # right_shoulder -> right_elbow
    [3, 5],    # right_elbow -> right_wrist
    [0, 6],    # left_shoulder -> left_hip
    [1, 7],    # right_shoulder -> right_hip
    [6, 7],    # left_hip -> right_hip
    [6, 8],    # left_hip -> left_knee
    [8, 10],   # left_knee -> left_ankle
    [7, 9],    # right_hip -> right_knee
    [9, 11]    # right_knee -> right_ankle
]

# 3. Create the 3D plot
fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(111, projection='3d')

# Plot the 14 keypoints
ax.scatter(points[:, 0], points[:, 1], points[:, 2], c='red', marker='o', s=50)

# Plot the skeleton connections
for start_idx, end_idx in SKELETON:
    ax.plot(
        [points[start_idx, 0], points[end_idx, 0]],
        [points[start_idx, 1], points[end_idx, 1]],
        [points[start_idx, 2], points[end_idx, 2]],
        'b-' # Blue line
    )

# 4. Set plot properties for better visualization
ax.set_xlabel('X coordinate')
ax.set_ylabel('Y coordinate')
ax.set_zlabel('Z coordinate')
ax.set_title('3D Human Pose Visualization')

# To make the aspect ratio look correct, set the limits for each axis
# to be the same. This prevents the skeleton from looking stretched.
x = points[:, 0]
y = points[:, 1]
z = points[:, 2]
max_range = np.array([x.max()-x.min(), y.max()-y.min(), z.max()-z.min()]).max() / 2.0
mid_x = (x.max()+x.min()) * 0.5
mid_y = (y.max()+y.min()) * 0.5
mid_z = (z.max()+z.min()) * 0.5
ax.set_xlim(mid_x - max_range, mid_x + max_range)
ax.set_ylim(mid_y - max_range, mid_y + max_range)
ax.set_zlim(mid_z - max_range, mid_z + max_range)

# You can change the viewing angle to see the pose from different perspectives
# ax.view_init(elev=15., azim=-75)

plt.savefig('3d_human_pose.png', dpi=300)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions