File size: 9,158 Bytes
d7e58f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import numpy as np
import torch
from smplx import MANO as _MANO
from smplx import MANOLayer as _MANOLayer

from detrsmpl.core.conventions.keypoints_mapping import (
    convert_kps,
    get_keypoint_num,
)


class MANO(_MANO):
    """Extension of the official MANO implementation."""
    full_pose_keys = {'global_orient', 'hand_pose'}

    NUM_VERTS = 776
    NUM_FACES = 9976

    KpId2manokps = {
        0: 0,  # Wrist
        1: 5,
        2: 6,
        3: 7,  # Index
        4: 9,
        5: 10,
        6: 11,  # Middle
        7: 17,
        8: 18,
        9: 19,  # Pinky
        10: 13,
        11: 14,
        12: 15,  # Ring
        13: 1,
        14: 2,
        15: 3
    }  # Thumb
    kpId2vertices = {
        4: 744,  # Thumb
        8: 320,  # Index
        12: 443,  # Middle
        16: 555,  # Ring
        20: 672  # Pink
    }

    def __init__(self,
                 *args,
                 keypoint_src: str = 'mano',
                 keypoint_dst: str = 'human_data',
                 keypoint_approximate: bool = False,
                 **kwargs):
        """
        Args:
            *args: extra arguments for MANO initialization.
            keypoint_src: source convention of keypoints. This convention
                is used for keypoints obtained from joint regressors.
                Keypoints then undergo conversion into keypoint_dst
                convention.
            keypoint_dst: destination convention of keypoints. This convention
                is used for keypoints in the output.
            keypoint_approximate: whether to use approximate matching in
                convention conversion for keypoints.
            **kwargs: extra keyword arguments for MANO initialization.

        Returns:
            None
        """
        super(MANO, self).__init__(*args, **kwargs)
        self.keypoint_src = keypoint_src
        self.keypoint_dst = keypoint_dst
        self.keypoint_approximate = keypoint_approximate

        self.num_verts = self.get_num_verts()
        self.num_faces = self.get_num_faces()
        self.num_joints = get_keypoint_num(convention=self.keypoint_dst)

    def forward(self,
                *args,
                return_verts: bool = True,
                return_full_pose: bool = False,
                **kwargs) -> dict:
        """Forward function.

        Args:
            *args: extra arguments for MANO
            return_verts: whether to return vertices
            return_full_pose: whether to return full pose parameters
            **kwargs: extra arguments for MANO

        Returns:
            output: contains output parameters and attributes
        """
        if 'right_hand_pose' in kwargs:
            kwargs['hand_pose'] = kwargs['right_hand_pose']
        mano_output = super(MANO, self).forward(*args, **kwargs)
        joints = mano_output.joints

        joints = self.get_keypoints_from_mesh(mano_output.vertices, joints)

        joints, joint_mask = convert_kps(joints,
                                         src=self.keypoint_src,
                                         dst=self.keypoint_dst,
                                         approximate=self.keypoint_approximate)
        if isinstance(joint_mask, np.ndarray):
            joint_mask = torch.tensor(joint_mask,
                                      dtype=torch.uint8,
                                      device=joints.device)

        batch_size = joints.shape[0]
        joint_mask = joint_mask.reshape(1, -1).expand(batch_size, -1)

        output = dict(
            global_orient=mano_output.global_orient,
            hand_pose=mano_output.hand_pose,
            joints=joints,
            joint_mask=joint_mask,
            keypoints=torch.cat([joints, joint_mask[:, :, None]], dim=-1),
            betas=mano_output.betas,
        )

        if return_verts:
            output['vertices'] = mano_output.vertices
        if return_full_pose:
            output['full_pose'] = mano_output.full_pose

        return output

    def get_keypoints_from_mesh(self, mesh_vertices, keypoints_regressed):
        """Assembles the full 21 keypoint set from the 16 Mano Keypoints and 5
        mesh vertices for the fingers."""
        batch_size = keypoints_regressed.shape[0]
        keypoints = torch.zeros((batch_size, 21, 3)).cuda()

        # fill keypoints which are regressed
        for manoId, myId in self.KpId2manokps.items():
            keypoints[:, myId, :] = keypoints_regressed[:, manoId, :]
        # get other keypoints from mesh
        for myId, meshId in self.kpId2vertices.items():
            keypoints[:, myId, :] = mesh_vertices[:, meshId, :]

        return keypoints


class MANOLayer(_MANOLayer):
    """Extension of the official MANO implementation."""
    full_pose_keys = {'global_orient', 'hand_pose'}

    NUM_VERTS = 776
    NUM_FACES = 9976

    KpId2manokps = {
        0: 0,  # Wrist
        1: 5,
        2: 6,
        3: 7,  # Index
        4: 9,
        5: 10,
        6: 11,  # Middle
        7: 17,
        8: 18,
        9: 19,  # Pinky
        10: 13,
        11: 14,
        12: 15,  # Ring
        13: 1,
        14: 2,
        15: 3
    }  # Thumb
    kpId2vertices = {
        4: 744,  # Thumb
        8: 320,  # Index
        12: 443,  # Middle
        16: 555,  # Ring
        20: 672  # Pink
    }

    def __init__(self,
                 *args,
                 keypoint_src: str = 'mano',
                 keypoint_dst: str = 'human_data',
                 keypoint_approximate: bool = False,
                 **kwargs):
        """
        Args:
            *args: extra arguments for MANO initialization.
            keypoint_src: source convention of keypoints. This convention
                is used for keypoints obtained from joint regressors.
                Keypoints then undergo conversion into keypoint_dst
                convention.
            keypoint_dst: destination convention of keypoints. This convention
                is used for keypoints in the output.
            keypoint_approximate: whether to use approximate matching in
                convention conversion for keypoints.
            **kwargs: extra keyword arguments for MANO initialization.

        Returns:
            None
        """
        super(MANOLayer, self).__init__(*args, **kwargs)
        self.keypoint_src = keypoint_src
        self.keypoint_dst = keypoint_dst
        self.keypoint_approximate = keypoint_approximate

        self.num_verts = self.get_num_verts()
        self.num_faces = self.get_num_faces()
        self.num_joints = get_keypoint_num(convention=self.keypoint_dst)

    def forward(self,
                *args,
                return_verts: bool = True,
                return_full_pose: bool = False,
                **kwargs) -> dict:
        """Forward function.

        Args:
            *args: extra arguments for MANO
            return_verts: whether to return vertices
            return_full_pose: whether to return full pose parameters
            **kwargs: extra arguments for MANO

        Returns:
            output: contains output parameters and attributes
        """
        if 'right_hand_pose' in kwargs:
            kwargs['hand_pose'] = kwargs['right_hand_pose']
        mano_output = super(MANOLayer, self).forward(*args, **kwargs)
        joints = mano_output.joints

        joints = self.get_keypoints_from_mesh(mano_output.vertices, joints)

        joints, joint_mask = convert_kps(joints,
                                         src=self.keypoint_src,
                                         dst=self.keypoint_dst,
                                         approximate=self.keypoint_approximate)
        if isinstance(joint_mask, np.ndarray):
            joint_mask = torch.tensor(joint_mask,
                                      dtype=torch.uint8,
                                      device=joints.device)

        batch_size = joints.shape[0]
        joint_mask = joint_mask.reshape(1, -1).expand(batch_size, -1)

        output = dict(
            global_orient=mano_output.global_orient,
            hand_pose=mano_output.hand_pose,
            joints=joints,
            joint_mask=joint_mask,
            keypoints=torch.cat([joints, joint_mask[:, :, None]], dim=-1),
            betas=mano_output.betas,
        )

        if return_verts:
            output['vertices'] = mano_output.vertices
        if return_full_pose:
            output['full_pose'] = mano_output.full_pose

        return output

    def get_keypoints_from_mesh(self, mesh_vertices, keypoints_regressed):
        """Assembles the full 21 keypoint set from the 16 Mano Keypoints and 5
        mesh vertices for the fingers."""
        batch_size = keypoints_regressed.shape[0]
        keypoints = torch.zeros((batch_size, 21, 3)).cuda()

        # fill keypoints which are regressed
        for manoId, myId in self.KpId2manokps.items():
            keypoints[:, myId, :] = keypoints_regressed[:, manoId, :]
        # get other keypoints from mesh
        for myId, meshId in self.kpId2vertices.items():
            keypoints[:, myId, :] = mesh_vertices[:, meshId, :]

        return keypoints