You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

寻求生成特定格式2D人体姿态关键点JSON输出的Python实现方案

寻求生成特定格式2D人体姿态关键点JSON输出的Python实现方案

Hey there! I get your frustration—getting the exact JSON format you need can be tricky, especially if you struggled with OpenPose setup. Let’s break down two practical approaches to get that specific output structure in Python:

1. 使用OpenPose Python绑定(原生支持该格式)

OpenPose actually natively outputs JSON in the exact structure you shared, so this is the most straightforward way if you can get it set up correctly. Here’s how to do it:

安装步骤

First, install the necessary dependencies:

pip install pyopenpose opencv-python

(Note: If pyopenpose doesn’t install smoothly on your system, you might need to build OpenPose from source with Python bindings enabled—focus on the official docs for your OS, but the pip package works for most modern systems.)

Python代码示例

This code will load an image, run pose estimation, and output the JSON structure you need:

import pyopenpose as op
import cv2
import json

# 配置OpenPose参数
params = {
    "model_folder": "./models/",  # 确保你有OpenPose的模型文件,可从官网下载
    "face": False,  # 如果不需要人脸关键点,设为False
    "hand": False   # 如果不需要手部关键点,设为False
}

# 初始化OpenPose
opWrapper = op.WrapperPython()
opWrapper.configure(params)
opWrapper.start()

# 加载图片
image_path = "your_image.jpg"
image = cv2.imread(image_path)
datum = op.Datum()
datum.cvInputData = image
opWrapper.emplaceAndPop(op.VectorDatum([datum]))

# 构建目标JSON结构
result = {
    "version": 1.0,
    "people": []
}

# 处理每个人的关键点(这里是单个人)
for pose_keypoints in datum.poseKeypoints:
    person_data = {
        "face_keypoints": [],
        "pose_keypoints": pose_keypoints.flatten().tolist(),
        "hand_right_keypoints": [],
        "hand_left_keypoints": []
    }
    result["people"].append(person_data)

# 输出或保存JSON
print(json.dumps(result, indent=2))
# 保存到文件
with open("pose_output.json", "w") as f:
    json.dump(result, f, indent=2)

(Don’t forget to download the OpenPose model files from their official site and point model_folder to the correct path.)

2. 使用MediaPipe Pose + 格式转换(更易安装)

If OpenPose setup is still giving you trouble, MediaPipe is a lightweight alternative that’s super easy to install. You’ll just need to convert its output to match your desired JSON format.

安装步骤

pip install mediapipe opencv-python

Python代码示例

This code will run pose estimation with MediaPipe and map the landmarks to OpenPose’s keypoint order:

import mediapipe as mp
import cv2
import json

# 初始化MediaPipe Pose
mp_pose = mp.solutions.pose
pose = mp_pose.Pose(static_image_mode=True, min_detection_confidence=0.5)

# MediaPipe到OpenPose的关键点映射(对应OpenPose的25个关键点)
# OpenPose顺序: 0-鼻,1-左眼,2-右眼,3-左耳,4-右耳,5-左肩,6-右肩,7-左肘,8-右肘,9-左腕,10-右腕,11-左髋,12-右髋,13-左膝,14-右膝,15-左踝,16-右踝,17-左脚跟,18-左脚尖,19-右脚跟,20-右脚尖,21-胸部,22-背部,23-头顶,24-背景(未使用)
mapping = [
    0,  # 鼻
    1,  # 左眼
    2,  # 右眼
    3,  # 左耳
    4,  # 右耳
    11, # 左肩
    12, # 右肩
    13, # 左肘
    14, # 右肘
    15, # 左腕
    16, # 右腕
    23, # 左髋
    24, # 右髋
    25, # 左膝
    26, # 右膝
    27, # 左踝
    28, # 右踝
    29, # 左脚跟
    30, # 左脚尖
    31, # 右脚跟
    32, # 右脚尖
    11, # 胸部(用左肩替代,MediaPipe无单独胸部点)
    12, # 背部(用右肩替代)
    0,  # 头顶(用鼻子替代)
    -1  # 背景点,设为0
]

# 加载图片
image_path = "your_image.jpg"
image = cv2.imread(image_path)
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# 运行姿态估计
results = pose.process(image_rgb)

# 构建目标JSON结构
result = {
    "version": 1.0,
    "people": []
}

if results.pose_landmarks:
    pose_keypoints = []
    for idx in mapping:
        if idx == -1:
            pose_keypoints.extend([0, 0, 0])
        else:
            landmark = results.pose_landmarks.landmark[idx]
            # 转换坐标为图像像素(MediaPipe输出是0-1的相对坐标)
            x = landmark.x * image.shape[1]
            y = landmark.y * image.shape[0]
            confidence = landmark.visibility  # 用visibility作为置信度
            pose_keypoints.extend([x, y, confidence])
    
    person_data = {
        "face_keypoints": [],
        "pose_keypoints": pose_keypoints,
        "hand_right_keypoints": [],
        "hand_left_keypoints": []
    }
    result["people"].append(person_data)

# 输出或保存JSON
print(json.dumps(result, indent=2))
with open("pose_output.json", "w") as f:
    json.dump(result, f, indent=2)

(Note: Some OpenPose keypoints don’t have direct matches in MediaPipe, so we use the closest alternative. Adjust the mapping if you need more accuracy.)

额外提示

  • If you need face or hand keypoints later, just enable those modules in OpenPose (set "face": True and "hand": True in params) or add MediaPipe Face Mesh and Hands modules and extend the conversion logic.
  • For OpenPose’s model files, you can grab them from the official OpenPose repository under the models directory.

备注:内容来源于stack exchange,提问作者Root

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.20 12:53:10