EasyMocap/code/demo_mv1pmf_skel.py

97 lines
3.7 KiB
Python
Raw Normal View History

2021-01-14 21:32:09 +08:00
'''
@ Date: 2021-01-12 17:08:25
@ Author: Qing Shuai
@ LastEditors: Qing Shuai
2021-01-24 22:33:08 +08:00
@ LastEditTime: 2021-01-24 20:57:35
@ FilePath: /EasyMocapRelease/code/demo_mv1pmf_skel.py
2021-01-14 21:32:09 +08:00
'''
# show skeleton and reprojection
from dataset.mv1pmf import MV1PMF
from dataset.config import CONFIG
from mytools.reconstruction import simple_recon_person, projectN3
2021-01-24 22:33:08 +08:00
# from mytools.robust_triangulate import robust_triangulate
2021-01-14 21:32:09 +08:00
from tqdm import tqdm
import numpy as np
2021-01-24 22:33:08 +08:00
from smplmodel import check_keypoints
2021-01-14 21:32:09 +08:00
def smooth_skeleton(skeleton):
# nFrames, nJoints, 4: [[(x, y, z, c)]]
nFrames = skeleton.shape[0]
span = 2
# results = np.zeros((nFrames-2*span, skeleton.shape[1], skeleton.shape[2]))
origin = skeleton[span:nFrames-span, :, :].copy()
conf = origin[:, :, 3:4].copy()
skel = origin[:, :, :3] * conf
base_start = span
for i in range(-span, span+1):
sample = skeleton[base_start+i:base_start+i+skel.shape[0], :, :]
skel += sample[:, :, :3] * sample[:, :, 3:]
conf += sample[:, :, 3:]
not_f, not_j, _ = np.where(conf<0.1)
skel[not_f, not_j, :] = 0.
conf[not_f, not_j, :] = 1.
skel = skel/conf
skeleton[span:nFrames-span, :, :3] = skel
return skeleton
2021-01-24 22:33:08 +08:00
def get_limb_length(config, keypoints):
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]))
length, confs = 0, 0
for nf in range(keypoints.shape[0]):
limb_length = np.linalg.norm(keypoints[nf, i, :3] - keypoints[nf, j, :3])
conf = keypoints[nf, [i, j], -1].min()
length += limb_length * conf
confs += conf
limb_length = length/confs
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_skel(path, sub, out, mode, args):
2021-01-24 22:33:08 +08:00
MIN_CONF_THRES = 0.3
2021-01-14 21:32:09 +08:00
no_img = not (args.vis_det or args.vis_repro)
2021-01-24 22:33:08 +08:00
config = CONFIG[mode]
dataset = MV1PMF(path, cams=sub, config=config, mode=mode,
2021-01-14 21:32:09 +08:00
undis=args.undis, no_img=no_img, out=out)
kp3ds = []
start, end = args.start, min(args.end, len(dataset))
for nf in tqdm(range(start, end), desc='triangulation'):
images, annots = dataset[nf]
conf = annots['keypoints'][..., -1]
conf[conf < MIN_CONF_THRES] = 0
2021-01-24 22:33:08 +08:00
annots['keypoints'] = check_keypoints(annots['keypoints'], WEIGHT_DEBUFF=1)
keypoints3d, _, kpts_repro = simple_recon_person(annots['keypoints'], dataset.Pall, config=config, ret_repro=True)
# keypoints3d, _, kpts_repro = robust_triangulate(annots['keypoints'], dataset.Pall, config=config, ret_repro=True)
2021-01-14 21:32:09 +08:00
kp3ds.append(keypoints3d)
if args.vis_det:
dataset.vis_detections(images, annots, nf, sub_vis=args.sub_vis)
if args.vis_repro:
dataset.vis_repro(images, annots, kpts_repro, nf, sub_vis=args.sub_vis)
# smooth the skeleton
kp3ds = np.stack(kp3ds)
2021-01-24 22:33:08 +08:00
# 计算一下骨长
# get_limb_length(config, kp3ds)
# if args.smooth:
# kp3ds = smooth_skeleton(kp3ds)
2021-01-14 21:32:09 +08:00
for nf in tqdm(range(kp3ds.shape[0]), desc='dump'):
dataset.write_keypoints3d(kp3ds[nf], nf + start)
if __name__ == "__main__":
2021-01-24 22:33:08 +08:00
from mytools.cmd_loader import load_parser
parser = load_parser()
2021-01-14 21:32:09 +08:00
args = parser.parse_args()
mv1pmf_skel(args.path, args.sub, args.out, args.body, args)