-
Notifications
You must be signed in to change notification settings - Fork 1
/
smooth copy.py
154 lines (118 loc) · 6.26 KB
/
smooth copy.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
# from scipy.signal import savgol_filter
import pickle
import numpy as np
import os
from scipy.spatial.transform import Rotation as R
import argparse
# Create the parser
parser = argparse.ArgumentParser()
# Add an argument
parser.add_argument('--tot_characters', type=int, required=True)
parser.add_argument('--pkl_file_input', type=str, required=True) # normalmente vai ser 'demo_video_converted.pkl' ou 'demo_video_converted_smooth.pkl'
parser.add_argument('--smooth_what', type=str, required=True) # trans,pose,both
# Parse the argument
args = parser.parse_args()
path_addon = os.path.dirname(os.path.abspath(__file__))
pkl_folder = os.path.join(path_addon,'4D-Humans-main','outputs','results')
# file = r"D:\0_Programs\CEB_4D_Human_prj\CEB_4d_Human\4D-Humans-main\outputs\results\demo_video_converted.pkl"
# file_smooth = r"D:\0_Programs\CEB_4D_Human_prj\CEB_4d_Human\4D-Humans-main\outputs\results\demo_video_converted_smooth.pkl"
# file_smooth2 = r"D:\0_Programs\CEB_4D_Human_prj\CEB_4d_Human\4D-Humans-main\outputs\results\demo_video_converted_smooth.pkl"
# file = os.path.join(pkl_folder,'demo_video_converted.pkl')
file = os.path.join(pkl_folder,args.pkl_file_input)
file_smooth = os.path.join(pkl_folder,'demo_video_converted_smooth.pkl')
with open(file, 'rb') as handle:
b = pickle.load(handle)
print('tot characters: '+str(args.tot_characters))
characters = args.tot_characters
for num_character in range(0,characters):
print('charcter: '+str(num_character))
for fframe, data in enumerate(b.items()):
if num_character <= len(data[1]['smpl'])-1:
if args.smooth_what == 'trans' or args.smooth_what == 'both':
trans = data[1]['camera'][num_character]
trans_temp = [trans[0],trans[1],0]
if fframe==0:
smpl_trans =trans_temp
else:
smpl_trans = np.vstack([smpl_trans,trans_temp])
#shape = data[1]['smpl'][character]['betas']
if args.smooth_what == 'pose' or args.smooth_what == 'both':
global_orient = data[1]['smpl'][num_character]['global_orient']
body_pose = data[1]['smpl'][num_character]['body_pose']
final_body_pose = np.vstack([global_orient, body_pose])
r = R.from_matrix(final_body_pose)
body_pose_vec = r.as_rotvec()
if fframe==0:
smpl_pose = body_pose_vec.reshape(1,72)
else:
smpl_pose = np.vstack([smpl_pose,body_pose_vec.reshape(1,72)])
else:
print('skipping to the next')
# print('trans: ',smpl_trans.shape)
# print('shape: ',smpl_pose.shape)
import copy
import numpy as np
from mmhuman3d.utils.demo_utils import smooth_process
smooth_type_s = 'smoothnet_windowsize8'
# smooth_type_s = 'smoothnet_windowsize16'
# smooth_type_s = 'savgol'
# smooth_type_s = 'oneeuro'
# smooth_type_s = 'guas1d'
# smooth_type_s = 'smoothnet_windowsize32'
# smooth_type_s = 'smoothnet_windowsize64'
if args.smooth_what == 'trans' or args.smooth_what == 'both':
trans = smpl_trans # (N,3), "global_t" in npz file
t0 = trans[::2]
frame_num = t0.shape[0]
new_trans_0 = smooth_process(t0[:, np.newaxis],
# smooth_type='smoothnet_windowsize8',
smooth_type=smooth_type_s,
cfg_base_dir='configs_cliff/_base_/post_processing/').reshape(frame_num,3)
if args.smooth_what == 'pose' or args.smooth_what == 'both':
pose = smpl_pose # (N,72), "pose" in npz file
p0 = pose[::2]
frame_num = p0.shape[0]
new_pose_0 = smooth_process(p0.reshape(frame_num,24,3),
# smooth_type='smoothnet_windowsize8',
smooth_type=smooth_type_s,
cfg_base_dir='configs_cliff/_base_/post_processing/').reshape(frame_num,72)
# start from 1, the interval is 2
if args.smooth_what == 'trans' or args.smooth_what == 'both':
t1 = trans[1::2]
frame_num = t1.shape[0]
new_trans_1 = smooth_process(t1[:, np.newaxis],
# smooth_type='smoothnet_windowsize8',
smooth_type=smooth_type_s,
cfg_base_dir='configs_cliff/_base_/post_processing/').reshape(frame_num,3)
if args.smooth_what == 'pose' or args.smooth_what == 'both':
p1 = pose[1::2]
frame_num = p1.shape[0]
new_pose_1 = smooth_process(p1.reshape(frame_num,24,3),
# smooth_type='smoothnet_windowsize8',
smooth_type=smooth_type_s,
cfg_base_dir='configs_cliff/_base_/post_processing/').reshape(frame_num,72)
if args.smooth_what == 'pose' or args.smooth_what == 'both':
new_pose = copy.copy(pose)
new_pose[::2] = new_pose_0
new_pose[1::2] = new_pose_1
if args.smooth_what == 'trans' or args.smooth_what == 'both':
new_trans = copy.copy(trans)
new_trans[::2] = new_trans_0
new_trans[1::2] = new_trans_1
# realimentando pparar criar o pickle
for ffnew, data_new in enumerate(b.items()):
if num_character <= len(data_new[1]['smpl'])-1:
if args.smooth_what == 'trans' or args.smooth_what == 'both':
b[data_new[0]]['camera'][num_character] = new_trans[ffnew]
if args.smooth_what == 'pose' or args.smooth_what == 'both':
## De volta de rot vec para rot matrix
r_new = R.from_rotvec(new_pose[ffnew].reshape(24,3))
body_pose_matrix = r_new.as_matrix()
global_orient_new = body_pose_matrix[0].reshape(1,3,3)
body_pose_new = body_pose_matrix[1:]
b[data_new[0]]['smpl'][num_character]['global_orient'] = global_orient_new
b[data_new[0]]['smpl'][num_character]['body_pose'] = body_pose_new
else:
print('skipping to the next')
with open(file_smooth, 'wb') as handle:
pickle.dump(b, handle, protocol=pickle.HIGHEST_PROTOCOL)