File size: 6,748 Bytes
f7ca549
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import unicode_literals, print_function
import transforms3d as t3d
import pickle
import chumpy as ch
import numpy as np
from opendr.lighting import LambertianPointLight
try:
    from utils.mano_core.mano_loader import load_model
    from utils.mano_core.lbs import global_rigid_transformation
    from utils.mano_utils import get_keypoints_from_mesh_ch
except ImportError as e:
    raise ImportError('%s \nDid you set up the repository for advanced use?' % e)


renderer = None
def pose_hand(mano, K, use_mean_pose=True):
    global renderer
    if renderer is None:
        renderer = HandModel(use_mean_pca=False, use_mean_pose=use_mean_pose)

    # split mano parameters
    poses, shapes, uv_root, scale = split_theta(mano)
    focal, pp = get_focal_pp(K)
    xyz_root = recover_root(uv_root, scale, focal, pp)

    # set up the hand model and feed hand parameters
    renderer.pose_by_root(xyz_root[0], poses[0], shapes[0])
    V, F = renderer._get_verts_faces()
    xyz = renderer._calc_coords()
    return np.array(xyz), np.array(V)


def split_theta(theta):
    poses = theta[:, :48]
    shapes = theta[:, 48:58]
    uv_root = theta[:, 58:60]
    scale = theta[:, 60:]
    return poses, shapes, uv_root, scale

def get_focal_pp(K):
    """ Extract the camera parameters that are relevant for an orthographic assumption. """
    focal = 0.5 * (K[0, 0] + K[1, 1])
    pp = K[:2, 2]
    return focal, pp


def backproject_ortho(uv, scale,  # kind of the predictions
                      focal, pp):  # kind of the camera calibration
    """ Calculate 3D coordinates from 2D coordinates and the camera parameters. """
    uv = uv.copy()
    uv -= pp
    xyz = np.concatenate([np.reshape(uv, [-1, 2]),
                          np.ones_like(uv[:, :1])*focal], 1)
    xyz /= scale
    return xyz


def recover_root(uv_root, scale,
                 focal, pp):
    uv_root = np.reshape(uv_root, [1, 2])
    xyz_root = backproject_ortho(uv_root, scale, focal, pp)
    return xyz_root


class HandModel(object):
    def __init__(self, use_mean_pca=False, use_mean_pose=False):
        if use_mean_pca:
            self.model = load_model('./data/MANO_RIGHT.pkl', ncomps=6, flat_hand_mean=not use_mean_pose,
                                    use_pca=True)
        else:
            self.model = load_model('./data/MANO_RIGHT.pkl', ncomps=45, flat_hand_mean=not use_mean_pose,
                                    use_pca=False)

        self.global_trans = ch.array([0.0, 0.0, 0.3])

    def _get_verts_faces(self):
        V = self.model + self.global_trans
        F = self.model.f
        return V, F

    def _calc_coords(self):
        # calculate joint location and rotation
        V, _ = self._get_verts_faces()
        J_regressor = self.model.dd['J_regressor']
        Jtr_x = ch.MatVecMult(J_regressor, V[:, 0])
        Jtr_y = ch.MatVecMult(J_regressor, V[:, 1])
        Jtr_z = ch.MatVecMult(J_regressor, V[:, 2])
        Jtr = ch.vstack([Jtr_x, Jtr_y, Jtr_z]).T
        coords_kp_xyz = get_keypoints_from_mesh_ch(V, Jtr)
        return coords_kp_xyz

    def pose_by_root(self, xyz_root, poses, shapes, root_id=9):
        """ Poses the MANO model according to the root keypoint given. """
        self.model.pose[:] = poses  # set estimated articulation
        self.model.betas[:] = shapes  # set estimated shape
        self.global_trans[:] = 0.0

        # how to chose translation
        xyz = np.array(self._calc_coords())
        global_t = xyz_root - xyz[root_id]  # new - old root keypoint

        # apply changes
        self.global_trans[:] = global_t

    def render(self, cam_intrinsics, dist=None, M=None, img_shape=None, render_mask=False):
        from opendr.camera import ProjectPoints
        from utils.renderer import ColoredRenderer

        if dist is None:
            dist = np.zeros(5)
        dist = dist.flatten()
        if M is None:
            M = np.eye(4)

        # get R, t from M (has to be world2cam)
        R = M[:3, :3]
        ax, angle = t3d.axangles.mat2axangle(R)
        rt = ax*angle
        rt = rt.flatten()
        t = M[:3, 3]

        w, h = (320, 320)
        if img_shape is not None:
            w, h = img_shape[1], img_shape[0]

        pp = np.array([cam_intrinsics[0, 2], cam_intrinsics[1, 2]])
        f = np.array([cam_intrinsics[0, 0], cam_intrinsics[1, 1]])

        # Create OpenDR renderer
        rn = ColoredRenderer()

        # Assign attributes to renderer
        rn.camera = ProjectPoints(rt=rt,
                                  t=t, # camera translation
                                  f=f,  # focal lengths
                                  c=pp,  # camera center (principal point)
                                  k=dist)  # OpenCv distortion params
        rn.frustum = {'near': 0.1, 'far': 5., 'width': w, 'height': h}

        V, F = self._get_verts_faces()
        rn.set(v=V,
               f=F,
               bgcolor=np.zeros(3))

        if render_mask:
            rn.vc = np.ones_like(V)  #for segmentation mask like rendering
        else:
            colors = np.ones_like(V)

            # Construct point light sources
            rn.vc = LambertianPointLight(f=F,
                                         v=V,
                                         num_verts=V.shape[0],
                                         light_pos=np.array([-1000, -1000, -2000]),
                                         vc=0.8 * colors,
                                         light_color=np.array([1., 1., 1.]))

            rn.vc += LambertianPointLight(f=F,
                                          v=V,
                                          num_verts=V.shape[0],
                                          light_pos=np.array([1000, 1000, -2000]),
                                          vc=0.25 * colors,
                                          light_color=np.array([1., 1., 1.]))

            rn.vc += LambertianPointLight(f=F,
                                          v=V,
                                          num_verts=V.shape[0],
                                          light_pos=np.array([2000, 2000, 2000]),
                                          vc=0.1 * colors,
                                          light_color=np.array([1., 1., 1.]))

            rn.vc += LambertianPointLight(f=F,
                                          v=V,
                                          num_verts=V.shape[0],
                                          light_pos=np.array([-2000, -2000, 2000]),
                                          vc=0.1 * colors,
                                          light_color=np.array([1., 1., 1.]))

        # render
        img = (np.array(rn.r) * 255).astype(np.uint8)
        return img