File size: 1,388 Bytes
15bc41b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.utils.data
from torch.autograd import Variable
import numpy as np
import torch.nn.functional as F

from manopth.manolayer import ManoLayer



def create_mano_layers(mano_path, device, n_cmps):
    class Output:
        def __init__(self, vertices, joints):
            self.vertices = vertices
            self.joints = joints

    class SmplxAdapter:
        def __init__(self, side):
            self.m = ManoLayer(mano_root=f'{mano_path}/mano', use_pca=True, ncomps=n_cmps, side=side, flat_hand_mean=False, robust_rot=True).to(device)
            self.faces = self.m.th_faces.cpu().numpy()
            self.shapedirs = self.m.th_shapedirs

        def __call__(self, global_orient, hand_pose, betas, transl): 
            vertices, joints = self.m(torch.cat([global_orient, hand_pose], 1), betas, transl)
            
            vertices /= 1000
            joints /= 1000

            return Output(vertices, joints)
                
    mano_layer = {
            'left': SmplxAdapter(side='left'),
            'right': SmplxAdapter(side='right')
            }
    
    if torch.sum(torch.abs(mano_layer['left'].m.th_shapedirs[:,0,:] - mano_layer['right'].m.th_shapedirs[:,0,:])) < 1:
        print('Fix th_shapedirs bug of MANO')
        mano_layer['left'].m.th_shapedirs[:,0,:] *= -1

    return mano_layer