NTU RGB + D 120 动作识别数据集 描绘骨架(python)

“ NTU RGB + D”和“ NTU RGB + D 120”。
“ NTU RGB + D”包含60个动作类别和56,880个视频样本。
“ NTU RGB + D 120”通过添加另外60类和另外57,600个视频样本来扩展“ NTU RGB + D”,即“ NTU RGB + D 120”总共有120类和114,480个样本。
这两个数据集都包含每个样本的RGB视频,深度图序列,3D骨骼数据和红外(IR)视频。每个数据集由三台Kinect V2摄像机同时捕获。
RGB视频的分辨率为1920x1080,深度图和IR视频的分辨率均为512x424,并且3D骨骼数据包含每帧25个人体关节的3D坐标。

有那些动作?

在这里插入图片描述
在这里插入图片描述

数据集的意思

参考这篇文章:https://blog.csdn.net/hsclyy/article/details/102809083

代码:

# -*- coding:utf-8 -*-
import os
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt


trunk_joints = [0, 1, 20, 2, 3]
arm_joints = [23, 24, 11, 10, 9, 8, 20, 4, 5, 6, 7, 22, 21]
leg_joints = [19, 18, 17, 16, 0, 12, 13, 14, 15]
body = [trunk_joints, arm_joints, leg_joints]


# Show 3D Skeleton with Axes3D for NTU RGB+D
class Draw3DSkeleton(object):

    def __init__(self, file, save_path=None, init_horizon=-45,
                 init_vertical=20, x_rotation=None,
                 y_rotation=None, pause_step=0.2):

        self.file = file
        self.save_path = save_path

      #  if not os.path.exists(self.save_path):
      #      os.mkdir(self.save_path)

        self.xyz = self.read_xyz(self.file)
        self.init_horizon = init_horizon
        self.init_vertical = init_vertical

        self.x_rotation = x_rotation
        self.y_rotation = y_rotation

        self._pause_step = pause_step

    def _read_skeleton(self, file):
        with open(file, 'r') as f:
            skeleton_sequence = {}
            skeleton_sequence['numFrame'] = int(f.readline())
            skeleton_sequence['frameInfo'] = []
            for t in range(skeleton_sequence['numFrame']):
                frame_info = {}
                frame_info['numBody'] = int(f.readline())
                frame_info['bodyInfo'] = []
                for m in range(frame_info['numBody']):
                    body_info = {}
                    body_info_key = [
                        'bodyID', 'clipedEdges', 'handLeftConfidence',
                        'handLeftState', 'handRightConfidence', 'handRightState',
                        'isResticted', 'leanX', 'leanY', 'trackingState'
                    ]
                    body_info = {
                        k: float(v)
                        for k, v in zip(body_info_key, f.readline().split())
                    }
                    body_info['numJoint'] = int(f.readline())
                    body_info['jointInfo'] = []
                    for v in range(body_info['numJoint']):
                        joint_info_key = [
                            'x', 'y', 'z', 'depthX', 'depthY', 'colorX', 'colorY',
                            'orientationW', 'orientationX', 'orientationY',
                            'orientationZ', 'trackingState'
                        ]
                        joint_info = {
                            k: float(v)
                            for k, v in zip(joint_info_key, f.readline().split())
                        }
                        body_info['jointInfo'].append(joint_info)
                    frame_info['bodyInfo'].append(body_info)
                skeleton_sequence['frameInfo'].append(frame_info)
        return skeleton_sequence

    def read_xyz(self, file, max_body=2, num_joint=25):
        seq_info = self._read_skeleton(file)
        data = np.zeros((3, seq_info['numFrame'], num_joint, max_body))  # (3,frame_nums,25 2)
        for n, f in enumerate(seq_info['frameInfo']):
            for m, b in enumerate(f['bodyInfo']):
                for j, v in enumerate(b['jointInfo']):
                    if m < max_body and j < num_joint:
                        data[:, n, j, m] = [v['x'], v['y'], v['z']]
                    else:
                        pass
        return data

    def _normal_skeleton(self, data):
        #  use as center joint
        center_joint = data[0, :, 0, :]

        center_jointx = np.mean(center_joint[:, 0])
        center_jointy = np.mean(center_joint[:, 1])
        center_jointz = np.mean(center_joint[:, 2])

        center = np.array([center_jointx, center_jointy, center_jointz])
        data = data - center

        return data

    def _rotation(self, data, alpha=0, beta=0):
        # rotate the skeleton around x-y axis
        r_alpha = alpha * np.pi / 180
        r_beta = beta * np.pi / 180

        rx = np.array([[1, 0, 0],
                       [0, np.cos(r_alpha), -1 * np.sin(r_alpha)],
                       [0, np.sin(r_alpha), np.cos(r_alpha)]]
                      )

        ry = np.array([
            [np.cos(r_beta), 0, np.sin(r_beta)],
            [0, 1, 0],
            [-1 * np.sin(r_beta), 0, np.cos(r_beta)],
        ])

        r = ry.dot(rx)
        data = data.dot(r)

        return data

    def visual_skeleton(self):
        fig = plt.figure()
        ax = Axes3D(fig)

        ax.view_init(self.init_vertical, self.init_horizon)
        plt.ion()

        data = np.transpose(self.xyz, (3, 1, 2, 0))   # 调换索引值,将x下标的索引值换到最后,将max_body索引值换到前面来

        # data rotation
        if (self.x_rotation is not None) or (self.y_rotation is not None):

            if self.x_rotation > 180 or self.y_rotation > 180:
                raise Exception("rotation angle should be less than 180.")

            else:
                data = self._rotation(data, self.x_rotation, self.y_rotation)

        # data normalization
        data = self._normal_skeleton(data)

        # show every frame 3d skeleton
        for frame_idx in range(data.shape[1]):

            plt.cla()
            plt.title("Frame: {}".format(frame_idx))

            ax.set_xlim3d([-1, 1])
            ax.set_ylim3d([-1, 1])
            ax.set_zlim3d([-0.8, 0.8])

            x = data[0, frame_idx, :, 0]
            y = data[0, frame_idx, :, 1]
            z = data[0, frame_idx, :, 2]

            for part in body:
                x_plot = x[part]
                y_plot = y[part]
                z_plot = z[part]
                ax.plot(x_plot, z_plot, y_plot, color='b', marker='o', markerfacecolor='r')

            ax.set_xlabel('X')
            ax.set_ylabel('Z')
            ax.set_zlabel('Y')

            if self.save_path is not None:
                save_pth = os.path.join(self.save_path, '{}.png'.format(frame_idx))
                plt.savefig(save_pth)
            print("The {} frame 3d skeleton......".format(frame_idx))

            ax.set_facecolor('none')
            plt.pause(self._pause_step)

        plt.ioff()
        ax.axis('off')
        plt.show()


if __name__ == '__main__':
    # test sample
    sk = Draw3DSkeleton("D:\BaiduNetdiskDownload\Raw_Skeleton_S01-S17\skeleton+D0-30000\S001C001P001R001A050.skeleton")
    sk.visual_skeleton()

  • 13
    点赞
  • 57
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值