forked from EricGuo5513/HumanML3D
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraw_pose_processing.py
167 lines (139 loc) · 5.49 KB
/
raw_pose_processing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# Taken from https://github.com/EricGuo5513/HumanML3D/blob/main/raw_pose_processing.ipynb
"""Extract Poses from Amass Dataset
"""
import sys, os
import torch
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from tqdm import tqdm
import time
import codecs as cs
import pandas as pd
from os.path import join as pjoin
from human_body_prior.tools.omni_tools import copy2cpu as c2c
from human_body_prior.body_model.body_model import BodyModel
# Place all files under the directory **./amass_data/**
# Choose the device to run the body model on.
comp_device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
male_bm_path = './body_models/smplh/male/model.npz'
male_dmpl_path = './body_models/dmpls/male/model.npz'
female_bm_path = './body_models/smplh/female/model.npz'
female_dmpl_path = './body_models/dmpls/female/model.npz'
num_betas = 10 # number of body parameters
num_dmpls = 8 # number of DMPL parameters
male_bm = BodyModel(bm_fname=male_bm_path, num_betas=num_betas, num_dmpls=num_dmpls, dmpl_fname=male_dmpl_path).to(comp_device)
faces = c2c(male_bm.f)
female_bm = BodyModel(bm_fname=female_bm_path, num_betas=num_betas, num_dmpls=num_dmpls, dmpl_fname=female_dmpl_path).to(comp_device)
paths = []
folders = []
dataset_names = []
for root, dirs, files in os.walk('./amass_data'):
folders.append(root)
for name in files:
if name.endswith('.txt'): continue
dataset_name = root.split('/')[2]
if dataset_name not in dataset_names:
dataset_names.append(dataset_name)
paths.append(os.path.join(root, name))
save_root = './pose_data'
save_folders = [folder.replace('./amass_data', './pose_data') for folder in folders]
for folder in save_folders:
os.makedirs(folder, exist_ok=True)
group_path = [[path for path in paths if name in path] for name in dataset_names]
trans_matrix = np.array([[1.0, 0.0, 0.0],
[0.0, 0.0, 1.0],
[0.0, 1.0, 0.0]])
ex_fps = 20
def amass_to_pose(src_path, save_path):
bdata = np.load(src_path, allow_pickle=True)
fps = 0
try:
fps = bdata['mocap_framerate']
frame_number = bdata['trans'].shape[0]
except:
return fps
if bdata['gender'] == 'male':
bm = male_bm
else:
bm = female_bm
down_sample = int(fps / ex_fps)
bdata_poses = bdata['poses'][::down_sample,...]
bdata_trans = bdata['trans'][::down_sample,...]
body_parms = {
'root_orient': torch.Tensor(bdata_poses[:, :3]).to(comp_device),
'pose_body': torch.Tensor(bdata_poses[:, 3:66]).to(comp_device),
'pose_hand': torch.Tensor(bdata_poses[:, 66:]).to(comp_device),
'trans': torch.Tensor(bdata_trans).to(comp_device),
'betas': torch.Tensor(np.repeat(bdata['betas'][:num_betas][np.newaxis], repeats=len(bdata_trans), axis=0)).to(comp_device),
}
with torch.no_grad():
body = bm(**body_parms)
pose_seq_np = body.Jtr.detach().cpu().numpy()
pose_seq_np_n = np.dot(pose_seq_np, trans_matrix)
np.save(save_path, pose_seq_np_n)
return fps
group_path = group_path
all_count = sum([len(paths) for paths in group_path])
cur_count = 0
print("Extract poses from Amass dataset...")
for paths in group_path:
dataset_name = paths[0].split('/')[2]
pbar = tqdm(paths)
pbar.set_description('Processing: %s'%dataset_name)
fps = 0
for path in pbar:
save_path = path.replace('./amass_data', './pose_data')
save_path = save_path[:-3] + 'npy'
fps = amass_to_pose(path, save_path)
cur_count += len(paths)
print('Processed / All (fps %d): %d/%d'% (fps, cur_count, all_count) )
time.sleep(0.5)
"""Segment, Mirror and Relocate Motions
"""
def swap_left_right(data):
assert len(data.shape) == 3 and data.shape[-1] == 3
data = data.copy()
data[..., 0] *= -1
right_chain = [2, 5, 8, 11, 14, 17, 19, 21]
left_chain = [1, 4, 7, 10, 13, 16, 18, 20]
left_hand_chain = [22, 23, 24, 34, 35, 36, 25, 26, 27, 31, 32, 33, 28, 29, 30]
right_hand_chain = [43, 44, 45, 46, 47, 48, 40, 41, 42, 37, 38, 39, 49, 50, 51]
tmp = data[:, right_chain]
data[:, right_chain] = data[:, left_chain]
data[:, left_chain] = tmp
if data.shape[1] > 24:
tmp = data[:, right_hand_chain]
data[:, right_hand_chain] = data[:, left_hand_chain]
data[:, left_hand_chain] = tmp
return data
index_path = './index.csv'
save_dir = './joints'
os.makedirs(save_dir, exist_ok=True)
index_file = pd.read_csv(index_path)
total_amount = index_file.shape[0]
fps = 20
print("Segment, Mirror and Relocate Motions...")
for i in tqdm(range(total_amount)):
source_path = index_file.loc[i]['source_path']
new_name = index_file.loc[i]['new_name']
data = np.load(source_path)
start_frame = index_file.loc[i]['start_frame']
end_frame = index_file.loc[i]['end_frame']
if 'humanact12' not in source_path:
if 'Eyes_Japan_Dataset' in source_path:
data = data[3*fps:]
if 'MPI_HDM05' in source_path:
data = data[3*fps:]
if 'TotalCapture' in source_path:
data = data[1*fps:]
if 'MPI_Limits' in source_path:
data = data[1*fps:]
if 'Transitions_mocap' in source_path:
data = data[int(0.5*fps):]
data = data[start_frame:end_frame]
data[..., 0] *= -1
data_m = swap_left_right(data)
save_path = pjoin(save_dir, new_name)
np.save(pjoin(save_dir, new_name), data)
np.save(pjoin(save_dir, 'M'+new_name), data_m)