Spaces:
Sleeping
Sleeping
File size: 7,136 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 |
import numpy as np
import torch
from smplx import FLAME as _FLAME
from smplx import FLAMELayer as _FLAMELayer
from detrsmpl.core.conventions.keypoints_mapping import (
convert_kps,
get_keypoint_num,
)
class FLAME(_FLAME):
"""Extension of the official FLAME implementation."""
head_pose_keys = {'global_orient', 'jaw_pose'}
full_pose_keys = {
'global_orient', 'neck_pose', 'jaw_pose', 'leye_pose', 'reye_pose'
}
NUM_VERTS = 5023
NUM_FACES = 9976
def __init__(self,
*args,
keypoint_src: str = 'flame',
keypoint_dst: str = 'human_data',
keypoint_approximate: bool = False,
**kwargs):
"""
Args:
*args: extra arguments for FLAME 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 FLAME initialization.
Returns:
None
"""
super(FLAME, 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 FLAME
return_verts: whether to return vertices
return_full_pose: whether to return full pose parameters
**kwargs: extra arguments for FLAME
Returns:
output: contains output parameters and attributes
"""
flame_output = super(FLAME, self).forward(*args, **kwargs)
joints = flame_output.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=flame_output.global_orient,
neck_pose=flame_output.neck_pose,
jaw_pose=flame_output.jaw_pose,
joints=joints,
joint_mask=joint_mask,
keypoints=torch.cat([joints, joint_mask[:, :, None]],
dim=-1),
betas=flame_output.betas,
expression=flame_output.expression)
if return_verts:
output['vertices'] = flame_output.vertices
if return_full_pose:
output['full_pose'] = flame_output.full_pose
return output
class FLAMELayer(_FLAMELayer):
"""Extension of the official FLAME implementation."""
head_pose_keys = {'global_orient', 'jaw_pose'}
full_pose_keys = {
'global_orient', 'neck_pose', 'jaw_pose', 'leye_pose', 'reye_pose'
}
NUM_VERTS = 5023
NUM_FACES = 9976
def __init__(self,
*args,
keypoint_src: str = 'flame',
keypoint_dst: str = 'human_data',
keypoint_approximate: bool = False,
**kwargs):
"""
Args:
*args: extra arguments for FLAME 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 FLAME initialization.
Returns:
None
"""
super(FLAMELayer, 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 FLAME
return_verts: whether to return vertices
return_full_pose: whether to return full pose parameters
**kwargs: extra arguments for FLAME
Returns:
output: contains output parameters and attributes
"""
flame_output = super(FLAMELayer, self).forward(*args, **kwargs)
joints = flame_output.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=flame_output.global_orient,
neck_pose=flame_output.neck_pose,
jaw_pose=flame_output.jaw_pose,
joints=joints,
joint_mask=joint_mask,
keypoints=torch.cat([joints, joint_mask[:, :, None]],
dim=-1),
betas=flame_output.betas,
expression=flame_output.expression)
if return_verts:
output['vertices'] = flame_output.vertices
if return_full_pose:
output['full_pose'] = flame_output.full_pose
return output
|