7e4ef6f98b
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
44 lines
2.1 KiB
Python
44 lines
2.1 KiB
Python
import torch
|
|
import numpy as np
|
|
import pickle
|
|
from typing import Optional
|
|
from body_model import MANOLayer
|
|
from smplx.lbs import vertices2joints
|
|
from smplx.utils import MANOOutput, to_tensor
|
|
from smplx.vertex_ids import vertex_ids
|
|
|
|
|
|
class MANO(MANOLayer):
|
|
def __init__(self, *args, joint_regressor_extra: Optional[str] = None, **kwargs):
|
|
"""
|
|
Extension of the official MANO implementation to support more joints.
|
|
Args:
|
|
Same as MANOLayer.
|
|
joint_regressor_extra (str): Path to extra joint regressor.
|
|
"""
|
|
super(MANO, self).__init__(*args, **kwargs)
|
|
mano_to_openpose = [0, 13, 14, 15, 16, 1, 2, 3, 17, 4, 5, 6, 18, 10, 11, 12, 19, 7, 8, 9, 20]
|
|
|
|
#2, 3, 5, 4, 1
|
|
if joint_regressor_extra is not None:
|
|
self.register_buffer('joint_regressor_extra', torch.tensor(pickle.load(open(joint_regressor_extra, 'rb'), encoding='latin1'), dtype=torch.float32))
|
|
self.register_buffer('extra_joints_idxs', to_tensor(list(vertex_ids['mano'].values()), dtype=torch.long))
|
|
self.register_buffer('joint_map', torch.tensor(mano_to_openpose, dtype=torch.long))
|
|
self.pose2rot = kwargs.get('pose2rot')
|
|
# print([attr for attr in dir(self) if not callable(getattr(self, attr)) and not attr.startswith("__")])
|
|
# print(self.hand_mean, self.flat_hand_mean, self.num_pca_comps, self.use_pca)
|
|
# exit()
|
|
|
|
def forward(self, *args, **kwargs) -> MANOOutput:
|
|
"""
|
|
Run forward pass. Same as MANO and also append an extra set of joints if joint_regressor_extra is specified.
|
|
"""
|
|
mano_output = super(MANO, self).forward(*args, **kwargs)
|
|
extra_joints = torch.index_select(mano_output.vertices, 1, self.extra_joints_idxs)
|
|
joints = torch.cat([mano_output.joints, extra_joints], dim=1)
|
|
joints = joints[:, self.joint_map, :]
|
|
if hasattr(self, 'joint_regressor_extra'):
|
|
extra_joints = vertices2joints(self.joint_regressor_extra, mano_output.vertices)
|
|
joints = torch.cat([joints, extra_joints], dim=1)
|
|
mano_output.joints = joints
|
|
return mano_output |