Change 25 human joints to 18 human joints json-file for Openpose.

Openpose可以对图像或视频进行人体姿态估计,并将估计的人体关节点坐标及置信度保存为json格式文件。Openpose的早期版本检测人体的$18$个姿态关节点,较新版本检测人体的$25$个姿态关节点,这就涉及到两种关节点的对应和转换问题。其定义的$18$姿态个关节点和$25$个姿态关节点如下图所示:

$18$个姿态关节点(上图左)的对应位置表示为:

{0, “Nose”},{1, “Neck”},{2, “RShoulder”},{3, “RElbow”},{4, “RWrist”},{5, “LShoulder”},{6, “LElbow”},{7, “LWrist”},{8, “RHip”},{9, “RKnee”},{10, “RAnkle”},{11, “LHip”},{12, “LKnee”},{13, “LAnkle”},{14, “REye”},{15, “LEye”},{16, “REar”},{17, “LEar”}

$25$个姿态关节点(上图右)的对应位置表示为:

{0, “Nose”},{1, “Neck”},{2, “RShoulder”},{3, “RElbow”},{4, “RWrist”},{5, “LShoulder”},{6, “LElbow”},{7, “LWrist”},{8, “MidHip”},{9, “RHip”},{10, “RKnee”},{11, “RAnkle”},{12, “LHip”},{13, “LKnee”},{14, “LAnkle”},{15, “REye”},{16, “LEye”},{17, “REar”},{18, “LEar”},{19, “LBigToe”},{20, “LSmallToe”},{21, “LHeel”},{22, “RBigToe”},{23, “RSmallToe”},{24, “RHeel”},

下面是一个具有$25$个姿态关节点的json输出文件,其中只预测了人体的姿态关节点,没有预测具有更多细节的手部和头部关节点。

{"version":1.3,"people":[{"person_id":[-1],"pose_keypoints_2d":[334.615,110.172,0.942269,334.613,151.898,0.926751,305.862,150.62,0.897234,295.462,202.779,0.887931,296.724,243.257,0.891767,363.348,153.176,0.892826,371.122,202.823,0.870904,369.85,244.563,0.909717,331.982,243.281,0.775034,313.715,243.281,0.751697,318.945,316.335,0.846198,320.306,384.261,0.813755,350.273,244.564,0.78912,346.368,316.316,0.872792,343.801,382.934,0.801428,329.362,106.217,0.946144,339.901,106.249,0.943413,320.284,112.724,0.855781,350.296,114.084,0.914338,343.751,403.811,0.768328,352.928,401.189,0.791169,341.175,389.425,0.666168,318.952,407.683,0.77182,311.122,403.815,0.792562,325.496,390.768,0.684426],"face_keypoints_2d":[],"hand_left_keypoints_2d":[],"hand_right_keypoints_2d":[],"pose_keypoints_3d":[],"face_keypoints_3d":[],"hand_left_keypoints_3d":[],"hand_right_keypoints_3d":[]}]}

将具有$25$个姿态关节点的json文件改写成具有$18$个姿态关节点的json文件,可采用如下方式:

import json

json18_path = './k18/pose1_keypoints.json'
json25_path = './k25/pose1_keypoints.json'
dict={}

def joint_map(k25):
    k18 = []
    joint_index = [0, 1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
    for k in joint_index:
        k18.append(k25[3*k])
        k18.append(k25[3*k+1])
        k18.append(k25[3*k+2])
    assert len(k18) == 18*3
    return k18
    
def get_json_data(json_path):
    with open(json_path,'rb') as f:
        params = json.load(f)
        for i in range(len(params['people'])):
            params['people'][i]['pose_keypoints_2d'] = joint_map(params['people'][i]['pose_keypoints_2d'])
        dict = params
    f.close()
    return dict

def write_json_data(dict):
    with open(json18_path,'w') as r:
        json.dump(dict,r)
    r.close()

the_revised_dict = get_json_data(json25_path)
write_json_data(the_revised_dict)

修改后具有$18$个姿态关节点的json输出文件如下:

{"version": 1.3, "people": [{"person_id": [-1], "pose_keypoints_2d": [334.615, 110.172, 0.942269, 334.613, 151.898, 0.926751, 305.862, 150.62, 0.897234, 295.462, 202.779, 0.887931, 296.724, 243.257, 0.891767, 363.348, 153.176, 0.892826, 371.122, 202.823, 0.870904, 369.85, 244.563, 0.909717, 313.715, 243.281, 0.751697, 318.945, 316.335, 0.846198, 320.306, 384.261, 0.813755, 350.273, 244.564, 0.78912, 346.368, 316.316, 0.872792, 343.801, 382.934, 0.801428, 329.362, 106.217, 0.946144, 339.901, 106.249, 0.943413, 320.284, 112.724, 0.855781, 350.296, 114.084, 0.914338], "face_keypoints_2d": [], "hand_left_keypoints_2d": [], "hand_right_keypoints_2d": [], "pose_keypoints_3d": [], "face_keypoints_3d": [], "hand_left_keypoints_3d": [], "hand_right_keypoints_3d": []}]}