File size: 4,909 Bytes
dbf8921 | 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 | # Copyright (c) Meta Platforms, Inc. and affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
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 ( # @manual
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__()
#mano_hand_pose_trajectory.jsonl:每一帧中每只手的“手腕位置 + 手腕朝向 + 手指姿态 + 手型”
super()._init_hand_poses(hand_pose_trajectory_filepath)
# Hand profile
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
|