| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| from typing import List, Optional |
|
|
| import numpy as np |
| import torch |
| from data_loaders.HandDataProviderBase import HandDataProviderBase |
| from data_loaders.loader_hand_poses import Handedness, HandPose, load_mano_shape_params |
| from data_loaders.pytorch3d_rotation.rotation_conversions import ( |
| matrix_to_axis_angle, |
| ) |
|
|
| from .mano_layer import MANOHandModel |
|
|
|
|
| class MANOHandDataProvider(HandDataProviderBase): |
| def __init__( |
| self, |
| hand_pose_trajectory_filepath: str, |
| mano_layer: MANOHandModel, |
| ) -> None: |
| super().__init__() |
| |
| super()._init_hand_poses(hand_pose_trajectory_filepath) |
|
|
| |
| self._mano_shape_params = load_mano_shape_params(hand_pose_trajectory_filepath) |
| if self._mano_shape_params is not None: |
| self._mano_shape_params = torch.from_numpy( |
| np.array(self._mano_shape_params) |
| ) |
|
|
| self.mano_layer = mano_layer |
|
|
| def get_hand_mesh_vertices( |
| self, hand_wrist_data: HandPose |
| ) -> Optional[torch.Tensor]: |
| """ |
| Return the hand mesh corresponding to given HandPose |
| """ |
| if ( |
| hand_wrist_data.wrist_pose is not None |
| and self._mano_shape_params is not None |
| and self.mano_layer is not None |
| ): |
| hand_wrist_pose_matrix = hand_wrist_data.wrist_pose.to_matrix() |
| hand_wrist_pose_tensor = torch.from_numpy(hand_wrist_pose_matrix) |
|
|
| hand_wrist_rotation_axis_angle = matrix_to_axis_angle( |
| hand_wrist_pose_tensor[:3, :3] |
| ) |
| hand_wrist_pose_tensor = torch.cat( |
| [hand_wrist_rotation_axis_angle, hand_wrist_pose_tensor[:3, 3]] |
| ) |
|
|
| mesh_vertices, landmarks = self.mano_layer.forward_kinematics( |
| self._mano_shape_params, |
| torch.from_numpy(np.array(hand_wrist_data.joint_angles)), |
| hand_wrist_pose_tensor, |
| torch.tensor([hand_wrist_data.handedness == Handedness.Right]), |
| ) |
|
|
| return mesh_vertices |
| return None |
|
|
| def get_hand_mesh_faces_and_normals( |
| self, hand_wrist_data: HandPose |
| ) -> Optional[List[np.ndarray]]: |
| """ |
| Return the hand mesh faces and normals |
| """ |
| if self.mano_layer is not None: |
| if hand_wrist_data.handedness == Handedness.Right: |
| hand_triangles = self.mano_layer.mano_layer_right.faces |
| else: |
| hand_triangles = self.mano_layer.mano_layer_left.faces |
|
|
| vertices = self.get_hand_mesh_vertices(hand_wrist_data) |
| assert vertices is not None |
| normals = HandDataProviderBase.get_triangular_mesh_normals( |
| vertices.float().numpy(), hand_triangles |
| ) |
| return [hand_triangles, normals] |
| else: |
| return None |
|
|
| def get_hand_landmarks(self, hand_wrist_data: HandPose) -> Optional[torch.Tensor]: |
| """ |
| Return the hand joint landmarks corresponding to given HandPose |
| See how to map the vertices together to represent a Hand as linked lines using LANDMARK_CONNECTIVITY |
| """ |
| if ( |
| hand_wrist_data.wrist_pose is not None |
| and self._mano_shape_params is not None |
| and self.mano_layer is not None |
| ): |
| hand_wrist_pose_matrix = hand_wrist_data.wrist_pose.to_matrix() |
| hand_wrist_pose_tensor = torch.from_numpy(hand_wrist_pose_matrix) |
|
|
| hand_wrist_rotation_axis_angle = matrix_to_axis_angle( |
| hand_wrist_pose_tensor[:3, :3] |
| ) |
| hand_wrist_pose_tensor = torch.cat( |
| [hand_wrist_rotation_axis_angle, hand_wrist_pose_tensor[:3, 3]] |
| ) |
|
|
| mesh_vertices, hand_landmarks = self.mano_layer.forward_kinematics( |
| self._mano_shape_params, |
| torch.from_numpy(np.array(hand_wrist_data.joint_angles)), |
| hand_wrist_pose_tensor, |
| torch.tensor([hand_wrist_data.handedness == Handedness.Right]), |
| ) |
|
|
| return hand_landmarks |
|
|
| return None |
|
|