Esempio n. 1
0
def bvh_reproj(args):
    anim, names, frametime = BVH.load(args.bvh_path)
    positions = Animation.positions_global(anim)
    print(positions.shape)

    camera_data = load_camera(args.json_path)

    for cam_name in camera_data.keys():
        if args.multi_process:
            Process(target=bvh_reproj_for_cam,
                    args=(args, cam_name, camera_data, positions,
                          anim)).start()
        else:
            bvh_reproj_for_cam(args, cam_name, camera_data, positions, anim)
Esempio n. 2
0
    def __init__(self, config, is_train=True):

        poses_3d_root, rotations, bones, alphas, contacts, projections = [], [], [], [], [], []
        self.frames = []
        self.config = config
        self.rotation_number = ROTATION_NUMBERS.get(config.arch.rotation_type)

        datasets = ['bvh']  #, 'bvh']
        if 'h36m' in datasets:
            dim_to_use_3d = h36m_utils.dimension_reducer(
                3, config.arch.predict_joints)
            subjects = h36m_utils.TRAIN_SUBJECTS if is_train else h36m_utils.TEST_SUBJECTS
            actions = h36m_utils.define_actions('All')
            self.cameras = h36m_utils.load_cameras(config.trainer.data_path)
            for subject in subjects:
                for action in actions:
                    for subaction in range(1, 3):
                        data_file = h5py.File(
                            '%s/S%s/%s-%s/annot.h5' %
                            (config.trainer.data_path, subject, action,
                             subaction), 'r')
                        data_size = data_file['frame'].size / 4
                        data_set = np.array(data_file['pose/3d']).reshape(
                            (-1, 96))[:, dim_to_use_3d]
                        for i in range(4):
                            camera_name = data_file['camera'][int(data_size *
                                                                  i)]
                            R, T, f, c, k, p, res_w, res_h = self.cameras[(
                                subject, str(camera_name))]
                            set_3d = data_set[int(data_size *
                                                  i):int(data_size *
                                                         (i + 1))].copy()
                            set_3d_world = h36m_utils.camera_to_world_frame(
                                set_3d.reshape((-1, 3)), R, T)
                            # set_3d_world[:, [1, 2]] = set_3d_world[:, [2, 1]]
                            # set_3d_world[:, [2]] *= -1
                            # set_3d_world = set_3d_world.reshape((-1, config.arch.predict_joints * 3))
                            set_3d_root = set_3d_world - np.tile(
                                set_3d_world[:, :3],
                                [1, int(set_3d_world.shape[-1] / 3)])

                            set_bones = self.get_bones(
                                set_3d_root, config.arch.predict_joints)
                            set_alphas = np.mean(set_bones, axis=1)

                            self.frames.append(set_3d_root.shape[0])
                            poses_3d_root.append(
                                set_3d_root /
                                np.expand_dims(set_alphas, axis=-1))
                            rotations.append(
                                np.zeros((set_3d_root.shape[0],
                                          int(set_3d_root.shape[1] / 3 *
                                              self.rotation_number))))
                            bones.append(set_bones /
                                         np.expand_dims(set_alphas, axis=-1))
                            alphas.append(set_alphas)
                            contacts.append(
                                self.get_contact(set_3d_world,
                                                 config.arch.predict_joints))
                            projections.append(
                                (set_3d_world.copy() /
                                 np.expand_dims(set_alphas, axis=-1)).reshape(
                                     (set_3d_world.shape[0], -1, 3))[:, 0, 2])

        if 'bvh' in datasets:
            to_keep = [
                0, 7, 8, 9, 2, 3, 4, 12, 15, 18, 19, 20, 25, 26, 27
            ] if config.arch.predict_joints == 15 else [
                0, 7, 8, 9, 2, 3, 4, 12, 13, 15, 16, 18, 19, 20, 25, 26, 27
            ]
            parents = [
                -1, 0, 1, 2, 0, 4, 5, 0, 7, 7, 9, 10, 7, 12, 13
            ] if config.arch.predict_joints == 15 else [
                -1, 0, 1, 2, 0, 4, 5, 0, 7, 8, 9, 8, 11, 12, 8, 14, 15
            ]

            bvh_files = util.make_dataset(['/mnt/dataset/test_bvh'],
                                          phase='bvh',
                                          data_split=1)
            bvh_files = bvh_files[:int(len(bvh_files) *
                                       0.8)] if is_train else bvh_files[
                                           int(len(bvh_files) * 0.8):]
            for bvh_file in bvh_files:
                original_anim, joint_names, frame_rate = BVH.load(bvh_file)
                set_skel_in = original_anim.positions[:, to_keep, :]
                set_rotations = original_anim.rotations.qs[:, to_keep, :]
                anim = Animation.Animation(
                    Quaternions(set_rotations), set_skel_in,
                    original_anim.orients.qs[to_keep, :], set_skel_in,
                    np.array(parents))
                set_3d_world = Animation.positions_global(anim).reshape(
                    set_rotations.shape[0], -1)
                set_3d_world[:, 0:3] = (set_3d_world[:, 3:6] +
                                        set_3d_world[:, 12:15]) / 2
                set_3d_root = set_3d_world - np.tile(
                    set_3d_world[:, :3],
                    [1, int(set_3d_world.shape[-1] / 3)])

                set_bones = self.get_bones(set_3d_root,
                                           config.arch.predict_joints)
                set_alphas = np.mean(set_bones, axis=1)

                self.frames.append(set_3d_root.shape[0])
                poses_3d_root.append(set_3d_root /
                                     np.expand_dims(set_alphas, axis=-1))
                rotations.append(
                    np.zeros((set_3d_root.shape[0],
                              int(set_3d_root.shape[1] / 3 *
                                  self.rotation_number))))
                bones.append(set_bones / np.expand_dims(set_alphas, axis=-1))
                alphas.append(set_alphas)
                contacts.append(
                    self.get_contact(set_3d_world, config.arch.predict_joints))
                projections.append(
                    (set_3d_world.copy() /
                     np.expand_dims(set_alphas, axis=-1)).reshape(
                         (set_3d_world.shape[0], -1, 3))[:, 0, 2])

        self.poses_3d = np.concatenate(poses_3d_root, axis=0)
        self.rotations = np.concatenate(rotations, axis=0)
        self.bones = np.concatenate(bones, axis=0)
        self.alphas = np.concatenate(alphas, axis=0)
        self.contacts = np.concatenate(contacts, axis=0)
        self.projections = np.concatenate(projections, axis=0)

        posed_3d_flip = self.get_flipping(self.poses_3d, 3,
                                          config.arch.predict_joints)
        if config.trainer.data_aug_flip and is_train:
            self.poses_3d = np.concatenate([self.poses_3d, posed_3d_flip],
                                           axis=0)

        self.poses_2d = self.get_projection(self.poses_3d)
        self.poses_2d_root = (self.poses_2d -
                              self.poses_2d[:, 0, None]).reshape(
                                  (self.poses_3d.shape[0], -1))

        import matplotlib.pyplot as plt
        import matplotlib.gridspec as gridspec
        from utils import visualization
        fig = plt.figure()
        gs = gridspec.GridSpec(1, 2)
        for i in range(1):
            ax1 = plt.subplot(gs[0], projection='3d')
            visualization.show3Dpose(self.poses_3d[i], ax1, radius=5)

            ax2 = plt.subplot(gs[1])
            visualization.show2Dpose(self.poses_2d_root[i] * 1000 + 500,
                                     ax2,
                                     radius=1000)

            fig.savefig('./images/2d_3d/_%d.png' % i)
            fig.clear()

        self.update_sequence_index()
Esempio n. 3
0
import sys
sys.path.append('./')

import numpy as np
import utils.BVH as BVH

from utils.Quaternions import Quaternions
from utils import util

rotations_bvh = []
bvh_files = util.make_dataset(['/mnt/dataset/cmubvh'], phase='bvh', data_split=1, sort_index=0)
for file in bvh_files:
    original_anim, _, frametime = BVH.load(file, rotate=True)
    sampling = 3
    to_keep = [0, 7, 8, 2, 3, 12, 13, 15, 18, 19, 25, 26]
    real_rotations = original_anim.rotations.qs[1:, to_keep, :]
    rotations_bvh.append(real_rotations[np.arange(0, real_rotations.shape[0] // sampling) * sampling].astype('float32'))
np.savez_compressed('./data/data_cmu.npz', rotations=rotations_bvh)