-
Notifications
You must be signed in to change notification settings - Fork 27
/
pkl2img.py
172 lines (150 loc) · 5.49 KB
/
pkl2img.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
168
169
170
171
172
import os
import sys
import os.path as osp
from pathlib import Path
import matplotlib
import platform
import warnings
if platform.system() == 'Windows':
matplotlib.use('TkAgg')
import platform
if platform.system() == "Linux":
os.environ['PYOPENGL_PLATFORM'] = 'egl'
else:
if 'PYOPENGL_PLATFORM' in os.environ:
os.environ.__delitem__('PYOPENGL_PLATFORM')
sys.path.extend([
osp.join(osp.dirname(__file__)),
osp.join(osp.dirname(__file__), 'configs'),
])
warnings.filterwarnings("ignore")
from SHOW.utils.metric import MeterBuffer
from SHOW.load_models import load_save_pkl
from SHOW.save_results import save_one_results
from SHOW.load_models import load_smplx_model, load_vposer_model
from pytorch3d.transforms import axis_angle_to_matrix
from pytorch3d.utils import opencv_from_cameras_projection
import torch
from pathlib import Path
import numpy as np
import torch.nn as nn
from tqdm import tqdm
from loguru import logger
from easydict import EasyDict
import SHOW.utils as utils
import mmcv
import SHOW
from SHOW.load_models import load_save_pkl
from SHOW.utils import default_timers
from SHOW.datasets import op_base
import os
import mmcv
import argparse
from loguru import logger
from SHOW.utils.video import images_to_video
def render_pkl_api(
img_folder,
ours_pkl_file_path,
ours_images_path,
output_video_path=None,
output_obj_path=None,
mica_all_dir=None,
replace_from_mica=False,
# use_npy_betas_ver='betas_2019_male',
model_path='../models/smplx/SMPLX_NEUTRAL_2020_org.npz',
body_model=None,
use_input_renderer=True,
**kwargs):
utils.platform_init()
op = op_base()
dtype = torch.float32
model_path = os.path.abspath(
os.path.join(os.path.dirname(__file__), model_path))
all_var = mmcv.load(ours_pkl_file_path)
# locals().update(all_var[0])
if isinstance(all_var, list):
all_var = all_var[0]
all_var = EasyDict(all_var)
if body_model is None:
body_model = load_smplx_model(dtype=dtype,
model_path=model_path,
use_face_contour=True,
use_pca=True,
flat_hand_mean=False,
use_hands=True,
use_face=True,
num_pca_comps=12,
num_betas=300,
num_expression_coeffs=100,
batch_size=all_var.batch_size)
camera_pose = op.get_smplx_to_pyrender_K(
torch.from_numpy(all_var.camera_transl))
smplx_params = dict(
body_pose=all_var.body_pose_axis,
betas=all_var.betas,
global_orient=all_var.global_orient,
transl=all_var.transl,
left_hand_pose=all_var.left_hand_pose,
right_hand_pose=all_var.right_hand_pose,
jaw_pose=all_var.jaw_pose,
leye_pose=all_var.leye_pose,
reye_pose=all_var.reye_pose,
expression=all_var.expression,
)
for key, val in smplx_params.items():
if isinstance(val, torch.Tensor):
smplx_params[key] = smplx_params[key].detach().to('cuda')
if isinstance(val, np.ndarray):
smplx_params[key] = torch.from_numpy(smplx_params[key]).to('cuda')
model_output = body_model(return_verts=True, **smplx_params)
if use_input_renderer:
import pyrender
input_renderer = pyrender.OffscreenRenderer(
viewport_width=all_var.width,
viewport_height=all_var.height,
point_size=1.0)
else:
input_renderer = None
vertices_ = model_output.vertices.detach().cpu().numpy()
Path(ours_images_path).mkdir(parents=True, exist_ok=True)
logger.info(f'saving final images to: {ours_images_path}')
if output_obj_path is not None:
Path(output_obj_path).mkdir(parents=True, exist_ok=True)
for idx in tqdm(range(vertices_.shape[0])): # idx=0
vertices = vertices_[idx]
input_img = None
for ext in ['.png', '.jpg']:
input_img = os.path.join(img_folder, f'{idx+1:06d}{ext}')
if Path(input_img).exists():
break
if not Path(input_img).exists():
logger.error(f'input_img not exists: {input_img}')
return False
meta_data = dict(
input_img=input_img,
output_name=os.path.join(ours_images_path, f'{idx+1:06d}.png'),
obj_path=os.path.join(output_obj_path, f'{idx+1:06d}.obj')
if output_obj_path is not None else None,
)
save_one_results(
vertices,
body_model.faces,
img_size=[all_var.height, all_var.width],
center=all_var.center,
focal_length=[all_var.focal_length, all_var.focal_length],
camera_pose=camera_pose,
meta_data=meta_data,
input_renderer=input_renderer,
)
if use_input_renderer:
input_renderer.delete()
if output_video_path is not None:
logger.info(f'saving final video to: {output_video_path}')
Path(output_video_path).parent.mkdir(parents=True, exist_ok=True)
if not SHOW.is_empty_dir(ours_images_path):
images_to_video(
input_folder=ours_images_path,
output_path=output_video_path,
img_format=None,
fps=30,
)