2021-01-14 21:32:09 +08:00
|
|
|
'''
|
|
|
|
@ Date: 2021-01-12 17:08:25
|
|
|
|
@ Author: Qing Shuai
|
|
|
|
@ LastEditors: Qing Shuai
|
2021-01-25 19:37:23 +08:00
|
|
|
@ LastEditTime: 2021-01-25 19:32:44
|
2021-01-24 22:33:08 +08:00
|
|
|
@ FilePath: /EasyMocapRelease/code/demo_mv1pmf_smpl.py
|
2021-01-14 21:32:09 +08:00
|
|
|
'''
|
|
|
|
# show skeleton and reprojection
|
|
|
|
import pyrender # first import the pyrender
|
|
|
|
from pyfitting.optimize_simple import optimizeShape, optimizePose
|
|
|
|
from dataset.mv1pmf import MV1PMF
|
|
|
|
from dataset.config import CONFIG
|
2021-01-24 22:33:08 +08:00
|
|
|
from mytools.utils import Timer
|
|
|
|
from smplmodel import select_nf, init_params, Config, load_model, check_keypoints
|
|
|
|
from os.path import join
|
2021-01-14 21:32:09 +08:00
|
|
|
from tqdm import tqdm
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
def load_weight_shape():
|
2021-01-24 22:33:08 +08:00
|
|
|
weight = {'s3d': 1., 'reg_shapes': 5e-3}
|
2021-01-14 21:32:09 +08:00
|
|
|
return weight
|
|
|
|
|
2021-01-24 22:33:08 +08:00
|
|
|
def load_weight_pose(model):
|
|
|
|
if model == 'smpl':
|
|
|
|
weight = {
|
2021-01-25 19:37:23 +08:00
|
|
|
'k3d': 1., 'reg_poses_zero': 1e-2, 'smooth_body': 1e-2
|
2021-01-24 22:33:08 +08:00
|
|
|
}
|
|
|
|
elif model == 'smplh':
|
|
|
|
weight = {
|
|
|
|
'k3d': 1., 'reg_poses_zero': 1e-3,
|
|
|
|
'smooth_body': 1e-2, 'smooth_hand': 1e-2
|
|
|
|
}
|
|
|
|
elif model == 'smplx':
|
|
|
|
weight = {
|
|
|
|
'k3d': 1., 'reg_poses_zero': 1e-3,
|
|
|
|
'reg_expression': 1e-2,
|
|
|
|
'smooth_body': 1e-2, 'smooth_hand': 1e-2
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
2021-01-14 21:32:09 +08:00
|
|
|
return weight
|
|
|
|
|
2021-01-24 22:33:08 +08:00
|
|
|
def print_mean_skel(mode):
|
|
|
|
with Timer('Loading {}, {}'.format(args.model, args.gender)):
|
|
|
|
body_model = load_model(args.gender, model_type=args.model)
|
|
|
|
params_init = init_params(nFrames=1, model_type=args.model)
|
|
|
|
skel = body_model(return_verts=False, return_tensor=False, **params_init)[0]
|
|
|
|
# skel: nJoints, 3
|
|
|
|
config = CONFIG[mode]
|
|
|
|
skeleton = {}
|
|
|
|
for i, j_ in config['kintree']:
|
|
|
|
if j_ == 25:
|
|
|
|
j = 7
|
|
|
|
elif j_ == 46:
|
|
|
|
j = 4
|
|
|
|
else:
|
|
|
|
j = j_
|
|
|
|
key = tuple(sorted([i, j]))
|
|
|
|
limb_length = np.linalg.norm(skel[i] - skel[j])
|
|
|
|
skeleton[key] = {'mean': limb_length, 'std': limb_length*0.2}
|
|
|
|
print('{')
|
|
|
|
for key, val in skeleton.items():
|
|
|
|
res = ' ({:2d}, {:2d}): {{\'mean\': {:.3f}, \'std\': {:.3f}}}, '.format(*key, val['mean'], val['std'])
|
|
|
|
if 'joint_names' in config.keys():
|
|
|
|
res += '# {:9s}->{:9s}'.format(config['joint_names'][key[0]], config['joint_names'][key[1]])
|
|
|
|
print(res)
|
|
|
|
print('}')
|
|
|
|
|
2021-01-14 21:32:09 +08:00
|
|
|
def mv1pmf_smpl(path, sub, out, mode, args):
|
|
|
|
config = CONFIG[mode]
|
2021-01-24 22:33:08 +08:00
|
|
|
no_img = True
|
|
|
|
dataset = MV1PMF(path, cams=sub, config=CONFIG[mode], mode=args.body,
|
2021-01-14 21:32:09 +08:00
|
|
|
undis=args.undis, no_img=no_img, out=out)
|
2021-01-24 22:33:08 +08:00
|
|
|
if args.skel is None:
|
|
|
|
from demo_mv1pmf_skel import mv1pmf_skel
|
|
|
|
mv1pmf_skel(path, sub, out, mode, args)
|
|
|
|
args.skel = join(out, 'keypoints3d')
|
|
|
|
dataset.skel_path = args.skel
|
2021-01-14 21:32:09 +08:00
|
|
|
kp3ds = []
|
|
|
|
start, end = args.start, min(args.end, len(dataset))
|
|
|
|
dataset.no_img = True
|
|
|
|
annots_all = []
|
2021-01-24 22:33:08 +08:00
|
|
|
for nf in tqdm(range(start, end), desc='loading'):
|
2021-01-14 21:32:09 +08:00
|
|
|
images, annots = dataset[nf]
|
2021-01-24 22:33:08 +08:00
|
|
|
infos = dataset.read_skel(nf)
|
|
|
|
kp3ds.append(infos[0]['keypoints3d'])
|
2021-01-14 21:32:09 +08:00
|
|
|
annots_all.append(annots)
|
|
|
|
kp3ds = np.stack(kp3ds)
|
2021-01-24 22:33:08 +08:00
|
|
|
kp3ds = check_keypoints(kp3ds, 1)
|
2021-01-14 21:32:09 +08:00
|
|
|
# optimize the human shape
|
2021-01-24 22:33:08 +08:00
|
|
|
with Timer('Loading {}, {}'.format(args.model, args.gender)):
|
|
|
|
body_model = load_model(args.gender, model_type=args.model)
|
|
|
|
params_init = init_params(nFrames=1, model_type=args.model)
|
2021-01-14 21:32:09 +08:00
|
|
|
weight = load_weight_shape()
|
2021-01-24 22:33:08 +08:00
|
|
|
if args.model in ['smpl', 'smplh', 'smplx']:
|
|
|
|
# when use SMPL model, optimize the shape only with first 14 limbs
|
|
|
|
params_shape = optimizeShape(body_model, params_init, kp3ds, weight_loss=weight, kintree=CONFIG['body15']['kintree'])
|
|
|
|
else:
|
|
|
|
params_shape = optimizeShape(body_model, params_init, kp3ds, weight_loss=weight, kintree=config['kintree'])
|
2021-01-14 21:32:09 +08:00
|
|
|
# optimize 3D pose
|
|
|
|
cfg = Config()
|
2021-01-24 22:33:08 +08:00
|
|
|
cfg.VERBOSE = args.verbose
|
|
|
|
cfg.MODEL = args.model
|
|
|
|
params = init_params(nFrames=kp3ds.shape[0], model_type=args.model)
|
2021-01-14 21:32:09 +08:00
|
|
|
params['shapes'] = params_shape['shapes'].copy()
|
2021-01-24 22:33:08 +08:00
|
|
|
weight = load_weight_pose(args.model)
|
|
|
|
with Timer('Optimize global RT'):
|
|
|
|
cfg.OPT_R = True
|
|
|
|
cfg.OPT_T = True
|
|
|
|
params = optimizePose(body_model, params, kp3ds, weight_loss=weight, kintree=config['kintree'], cfg=cfg)
|
|
|
|
with Timer('Optimize Pose/{} frames'.format(end-start)):
|
|
|
|
cfg.OPT_POSE = True
|
|
|
|
params = optimizePose(body_model, params, kp3ds, weight_loss=weight, kintree=config['kintree'], cfg=cfg)
|
|
|
|
if args.model in ['smplh', 'smplx']:
|
|
|
|
cfg.OPT_HAND = True
|
|
|
|
params = optimizePose(body_model, params, kp3ds, weight_loss=weight, kintree=config['kintree'], cfg=cfg)
|
|
|
|
if args.model == 'smplx':
|
|
|
|
cfg.OPT_EXPR = True
|
|
|
|
params = optimizePose(body_model, params, kp3ds, weight_loss=weight, kintree=config['kintree'], cfg=cfg)
|
|
|
|
# TODO:optimize 2D pose
|
|
|
|
# write out the results
|
2021-01-14 21:32:09 +08:00
|
|
|
dataset.no_img = not args.vis_smpl
|
|
|
|
for nf in tqdm(range(start, end), desc='render'):
|
|
|
|
images, annots = dataset[nf]
|
|
|
|
dataset.write_smpl(select_nf(params, nf-start), nf)
|
|
|
|
if args.vis_smpl:
|
|
|
|
vertices = body_model(return_verts=True, return_tensor=False, **select_nf(params, nf-start))
|
2021-01-24 22:33:08 +08:00
|
|
|
dataset.vis_smpl(vertices=vertices, faces=body_model.faces, images=images, nf=nf, sub_vis=args.sub_vis, add_back=True)
|
2021-01-14 21:32:09 +08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-01-24 22:33:08 +08:00
|
|
|
from mytools.cmd_loader import load_parser
|
|
|
|
parser = load_parser()
|
|
|
|
parser.add_argument('--skel', type=str, default=None,
|
|
|
|
help='path to keypoints3d')
|
2021-01-14 21:32:09 +08:00
|
|
|
parser.add_argument('--vis_smpl', action='store_true')
|
|
|
|
args = parser.parse_args()
|
2021-01-24 22:33:08 +08:00
|
|
|
# print_mean_skel(args.body)
|
2021-01-14 21:32:09 +08:00
|
|
|
mv1pmf_smpl(args.path, args.sub, args.out, args.body, args)
|