def export(pose_folder): video_name = pose_folder.split('/')[-1] files = util.make_dataset([pose_folder], phase='json', data_split=1, sort=True, sort_index=0) IMAGE_WIDTH = 1080 # Should be changed refer to your test data pose_batch = [] confidence_batch = [] for pose_file_name in files: with open(pose_file_name, 'r') as f: h36m_locations, h36m_confidence = h36m_utils.convert_openpose( json.load(f)) pose_batch.append(h36m_locations) confidence_batch.append(h36m_confidence) poses_2d = np.concatenate(pose_batch, axis=0) / IMAGE_WIDTH confidences = np.concatenate(confidence_batch, axis=0) poses_2d_root = ( poses_2d - np.tile(poses_2d[:, :2], [1, int(poses_2d.shape[-1] / 2)])) if config.arch.confidence: poses_2d_root_c = np.zeros( (poses_2d_root.shape[0], int(poses_2d_root.shape[-1] / 2 * 3))) for joint_index in range(int(poses_2d_root.shape[-1] / 2)): poses_2d_root_c[:, 3 * joint_index] = poses_2d_root[:, 2 * joint_index].copy( ) poses_2d_root_c[:, 3 * joint_index + 1] = poses_2d_root[:, 2 * joint_index + 1].copy() poses_2d_root_c[:, 3 * joint_index + 2] = np.array( confidences)[:, joint_index].copy() poses_2d = poses_2d_root_c poses_2d = np.divide((poses_2d - parameters[0].cpu().numpy()), parameters[1].cpu().numpy()) poses_2d = torch.from_numpy( np.array(poses_2d)).unsqueeze(0).float().to(device) with torch.no_grad(): pre_bones, pre_rotations, pre_rotations_full, pre_pose_3d, pre_c, pre_proj = model.forward_fk( poses_2d, parameters) if config.arch.translation: pose_2d_film = (poses_2d[0, :, :2].cpu().numpy() - 0.5) translations = np.ones(shape=(pose_2d_film.shape[0], 3)) translations[:, :2] = pose_2d_film translation = (translations * np.repeat( pre_proj[0].cpu().numpy(), 3, axis=-1).reshape( (-1, 3))) * 3 translation[:] -= translation[[0]] else: translation = np.zeros((poses_2d.shape[1], 3)) rotations = pre_rotations_full[0].cpu().numpy() length = (pre_bones * parameters[3].unsqueeze(0) + parameters[2].repeat(pre_bones.shape[0], 1, 1))[0].cpu().numpy() BVH.save('%s/%s.bvh' % (output_folder, video_name), Animation.load_from_network(translation, rotations, length, third_dimension=1), names=name_list) print('The bvh file of %s has been saved!' % video_name)
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()
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)