def __init__(self, reslu=512, mano_root=None): threading.Thread.__init__(self) if mano_root is None: mano_root = 'manopth/mano/models' mano_pth = os.path.join(mano_root, 'MANO_RIGHT.pkl') smpl_data = ready_arguments(mano_pth) faces = smpl_data['f'].astype(np.int32) self.reslu = reslu self.face = faces self.rend = renderer.MeshRenderer(self.face, img_size=reslu) self.exitFlag = 0 self.drawingQueue = Queue(maxsize=20) self.fakeIntr = np.array([[reslu * 2, 0, reslu / 2], [0, reslu * 2, reslu / 2], [0, 0, 1]])
def __init__( self, center_idx=None, flat_hand_mean=True, ncomps=6, side="right", mano_root="mano/models", use_pca=True, root_rot_mode="axisang", joint_rot_mode="axisang", robust_rot=False, ): """ Args: center_idx: index of center joint in our computations, if -1 centers on estimate of palm as middle of base of middle finger and wrist flat_hand_mean: if True, (0, 0, 0, ...) pose coefficients match flat hand, else match average hand pose mano_root: path to MANO pkl files for left and right hand ncomps: number of PCA components form pose space (<45) side: 'right' or 'left' use_pca: Use PCA decomposition for pose space. joint_rot_mode: 'axisang' or 'rotmat', ignored if use_pca """ super().__init__() self.center_idx = center_idx self.robust_rot = robust_rot if root_rot_mode == "axisang": self.rot = 3 else: self.rot = 6 self.flat_hand_mean = flat_hand_mean self.side = side self.use_pca = use_pca self.joint_rot_mode = joint_rot_mode self.root_rot_mode = root_rot_mode if use_pca: self.ncomps = ncomps else: self.ncomps = 45 if side == "right": self.mano_path = os.path.join(mano_root, "MANO_RIGHT.pkl") elif side == "left": self.mano_path = os.path.join(mano_root, "MANO_LEFT.pkl") smpl_data = ready_arguments(self.mano_path) hands_components = smpl_data["hands_components"] self.smpl_data = smpl_data self.register_buffer("th_betas", torch.Tensor(smpl_data["betas"].r).unsqueeze(0)) self.register_buffer("th_shapedirs", torch.Tensor(smpl_data["shapedirs"].r)) self.register_buffer("th_posedirs", torch.Tensor(smpl_data["posedirs"].r)) self.register_buffer( "th_v_template", torch.Tensor(smpl_data["v_template"].r).unsqueeze(0), ) self.register_buffer( "th_J_regressor", torch.Tensor(np.array(smpl_data["J_regressor"].toarray())), ) self.register_buffer("th_weights", torch.Tensor(smpl_data["weights"].r)) self.register_buffer( "th_faces", torch.Tensor(smpl_data["f"].astype(np.int32)).long()) # Get hand mean hands_mean = (np.zeros(hands_components.shape[1]) if flat_hand_mean else smpl_data["hands_mean"]) hands_mean = hands_mean.copy() th_hands_mean = torch.Tensor(hands_mean).unsqueeze(0) if self.use_pca or self.joint_rot_mode == "axisang": # Save as axis-angle self.register_buffer("th_hands_mean", th_hands_mean) selected_components = hands_components[:ncomps] self.register_buffer("th_comps", torch.Tensor(hands_components)) self.register_buffer("th_selected_comps", torch.Tensor(selected_components)) else: th_hands_mean_rotmat = rodrigues_layer.batch_rodrigues( th_hands_mean.view(15, 3)).reshape(15, 3, 3) self.register_buffer("th_hands_mean_rotmat", th_hands_mean_rotmat) # Kinematic chain params self.kintree_table = smpl_data["kintree_table"] parents = list(self.kintree_table[0].tolist()) self.kintree_parents = parents
def __init__(self, center_idx=None, flat_hand_mean=True, ncomps=6, side='right', mano_root='mano/models', use_pca=True): """ Args: center_idx: index of center joint in our computations, if -1 centers on estimate of palm as middle of base of middle finger and wrist flat_hand_mean: if True, (0, 0, 0, ...) pose coefficients match flat hand, else match average hand pose mano_root: path to MANO pkl files for left and right hand ncomps: number of PCA components form pose space (<45) side: 'right' or 'left' use_pca: Use PCA decomposition for pose space. """ super().__init__() self.center_idx = center_idx self.rot = 3 self.flat_hand_mean = flat_hand_mean self.side = side self.use_pca = use_pca if use_pca: self.ncomps = ncomps else: self.ncomps = 45 if side == 'right': self.mano_path = os.path.join(mano_root, 'MANO_RIGHT.pkl') elif side == 'left': self.mano_path = os.path.join(mano_root, 'MANO_LEFT.pkl') smpl_data = ready_arguments(self.mano_path) hands_components = smpl_data['hands_components'] self.smpl_data = smpl_data self.register_buffer('th_betas', torch.Tensor(smpl_data['betas'].r).unsqueeze(0)) self.register_buffer('th_shapedirs', torch.Tensor(smpl_data['shapedirs'].r)) self.register_buffer('th_posedirs', torch.Tensor(smpl_data['posedirs'].r)) self.register_buffer( 'th_v_template', torch.Tensor(smpl_data['v_template'].r).unsqueeze(0)) self.register_buffer( 'th_J_regressor', torch.Tensor(np.array(smpl_data['J_regressor'].toarray()))) self.register_buffer('th_weights', torch.Tensor(smpl_data['weights'].r)) self.register_buffer( 'th_faces', torch.Tensor(smpl_data['f'].astype(np.int32)).long()) # Get hand mean hands_mean = np.zeros(hands_components.shape[1] ) if flat_hand_mean else smpl_data['hands_mean'] hands_mean = hands_mean.copy() th_hands_mean = torch.Tensor(hands_mean).unsqueeze(0) if self.use_pca: # Save as axis-angle self.register_buffer('th_hands_mean', th_hands_mean) selected_components = hands_components[:ncomps] self.register_buffer('th_selected_comps', torch.Tensor(selected_components)) else: th_hands_mean_rotmat = rodrigues_layer.batch_rodrigues( th_hands_mean.view(15, 3)).reshape(15, 3, 3) self.register_buffer('th_hands_mean_rotmat', th_hands_mean_rotmat) # Kinematic chain params self.kintree_table = smpl_data['kintree_table'] parents = list(self.kintree_table[0].tolist()) self.kintree_parents = parents
def __init__( self, rot_mode: str = "axisang", side: str = "right", center_idx: Optional[int] = None, mano_assets_root: str = "assets/mano", use_pca: bool = False, flat_hand_mean: bool = True, # Only used in pca mode ncomps: int = 15, # Only used in pca mode **kargs, ): super().__init__() self.center_idx = center_idx self.rot_mode = rot_mode self.side = side self.use_pca = use_pca if rot_mode == "axisang": self.rot_dim = 3 elif rot_mode == "quat": self.rot_dim = 4 if use_pca == True or flat_hand_mean == False: warnings.warn( "Quat mode doesn't support PCA pose or flat_hand_mean !") else: raise NotImplementedError( f"Unrecognized rotation mode, expect [pca|axisang|quat], got {rot_mode}" ) # load model according to side flag mano_assets_path = os.path.join( mano_assets_root, f"MANO_{side.upper()}.pkl") # eg. MANO_RIGHT.pkl # parse and register stuff smpl_data = ready_arguments(mano_assets_path) self.register_buffer( "th_betas", torch.Tensor(np.array(smpl_data["betas"].r)).unsqueeze(0)) self.register_buffer("th_shapedirs", torch.Tensor(np.array(smpl_data["shapedirs"].r))) self.register_buffer("th_posedirs", torch.Tensor(np.array(smpl_data["posedirs"].r))) self.register_buffer( "th_v_template", torch.Tensor(np.array(smpl_data["v_template"].r)).unsqueeze(0)) self.register_buffer( "th_J_regressor", torch.Tensor(np.array(smpl_data["J_regressor"].toarray()))) self.register_buffer("th_weights", torch.Tensor(np.array(smpl_data["weights"].r))) self.register_buffer( "th_faces", torch.Tensor(np.array(smpl_data["f"]).astype(np.int32)).long()) kintree_table = smpl_data["kintree_table"] self.kintree_parents = list(kintree_table[0].tolist()) hands_components = smpl_data["hands_components"] if rot_mode == "axisang": hands_mean = np.zeros( hands_components.shape[1] ) if flat_hand_mean else smpl_data["hands_mean"] hands_mean = hands_mean.copy() hands_mean = torch.Tensor(hands_mean).unsqueeze(0) self.register_buffer("th_hands_mean", hands_mean) if rot_mode == "axisang" and use_pca == True: selected_components = hands_components[:ncomps] selected_components = torch.Tensor(selected_components) self.register_buffer("th_selected_comps", selected_components)
def __init__( self, root_idx=0, flat_hand_mean=True, ncomps=6, hand_side='right', template_root='mano/models', scale_milimeter=True, ): """ Args: root_idx: index of root joint in our computations, if -1 centers on estimate of palm as middle of base of middle finger and wrist flat_hand_mean: if True, (0, 0, 0, ...) pose coefficients match flat hand, else match average hand pose template_root: path to MANO pkl files for left and right hand ncomps: number of PCA components form pose space (<45) side: 'right' or 'left' """ super(ManoLayer, self).__init__() self.root_idx = root_idx self.rot = 3 # we use axisang so use 3 number to represent rotation self.flat_hand_mean = flat_hand_mean self.hand_side = hand_side # left or right self.scale_milimeter = scale_milimeter self.ncomps = ncomps if hand_side == 'right': self.mano_path = os.path.join(template_root, 'MANO_RIGHT.pkl') elif hand_side == 'left': self.mano_path = os.path.join(template_root, 'MANO_LEFT.pkl') smpl_data = ready_arguments(self.mano_path) hands_components = smpl_data['hands_components'] self.smpl_data = smpl_data self.register_buffer('th_betas', torch.Tensor(smpl_data['betas'].r).unsqueeze(0)) self.register_buffer('th_shapedirs', torch.Tensor(smpl_data['shapedirs'].r)) self.register_buffer('th_posedirs', torch.Tensor(smpl_data['posedirs'].r)) self.register_buffer( 'th_v_template', torch.Tensor(smpl_data['v_template'].r).unsqueeze(0)) self.register_buffer( 'th_J_regressor', torch.Tensor(np.array(smpl_data['J_regressor'].toarray()))) self.register_buffer('th_weights', torch.Tensor(smpl_data['weights'].r)) self.register_buffer( 'th_faces', torch.Tensor(smpl_data['f'].astype(np.int32)).long()) # Get hand mean hands_mean = np.zeros(hands_components.shape[1] ) if flat_hand_mean else smpl_data['hands_mean'] hands_mean = hands_mean.copy() th_hands_mean = torch.Tensor(hands_mean).unsqueeze(0) # Save as axis-angle self.register_buffer('th_hands_mean', th_hands_mean) selected_components = hands_components[:ncomps] self.register_buffer('th_selected_comps', torch.Tensor(selected_components)) # Kinematic chain params self.kintree_table = smpl_data['kintree_table'] parents = list(self.kintree_table[0].tolist()) self.kintree_parents = parents
def __init__( self, center_idx=None, flat_hand_mean=True, ncomps=6, side="right", mano_root="mano/models", use_pca=True, root_rot_mode="axisang", joint_rot_mode="axisang", robust_rot=False, return_transf=False, return_full_pose=False, ): """ Args: center_idx: index of center joint in our computations, if -1 centers on estimate of palm as middle of base of middle finger and wrist flat_hand_mean: if True, (0, 0, 0, ...) pose coefficients match flat hand, else match average hand pose mano_root: path to MANO pkl files for left and right hand ncomps: number of PCA components form pose space (<45) side: 'right' or 'left' use_pca: Use PCA decomposition for pose space. root_rot_mode: 'axisang' or 'rotmat' or 'quat', joint_rot_mode: 'axisang' or 'rotmat' or 'quat', ignored if use_pca """ super().__init__() self.center_idx = center_idx self.robust_rot = robust_rot # check root_rot_mode feasible, and set self.rot if root_rot_mode == "axisang": self.rot = 3 elif root_rot_mode == "rotmat": self.rot = 6 elif root_rot_mode == "quat": self.rot = 4 else: raise KeyError( "root_rot_mode not found. shoule be one of 'axisang' or 'rotmat' or 'quat'. got {}" .format(root_rot_mode)) # todo: flat_hand_mean have issues self.flat_hand_mean = flat_hand_mean # toggle extra return information self.return_transf = return_transf self.return_full_pose = return_full_pose # record side of hands self.side = side # check use_pca and joint_rot_mode if use_pca and joint_rot_mode != "axisang": raise TypeError( "if use_pca, joint_rot_mode must be 'axisang'. got {}".format( joint_rot_mode)) # record use_pca flag and joint_rot_mode self.use_pca = use_pca self.joint_rot_mode = joint_rot_mode # self.ncomps only work in axisang mode if use_pca: self.ncomps = ncomps else: self.ncomps = 45 # do more checks on root_rot_mode, in case mode error if self.joint_rot_mode == "axisang": # add restriction to root_rot_mode if root_rot_mode not in ["axisang", "rotmat"]: err_msg = "rot_mode not compatible, " err_msg += "when joint_rot_mode is 'axisang', root_rot_mode should be one of " err_msg += "'axisang' or 'rotmat', got {}".format( root_rot_mode) raise KeyError(err_msg) else: # for 'rotmat' or 'quat', there rot_mode must be same for joint and root if root_rot_mode != self.joint_rot_mode: err_msg = "rot_mode not compatible, " err_msg += "should get the same rot mode for joint and root, " err_msg += "got {} for root and {} for joint".format( root_rot_mode, self.joint_rot_mode) raise KeyError(err_msg) # record root_rot_mode self.root_rot_mode = root_rot_mode # load model according to side flag if side == "right": self.mano_path = os.path.join(mano_root, "MANO_RIGHT.pkl") elif side == "left": self.mano_path = os.path.join(mano_root, "MANO_LEFT.pkl") # parse and register stuff smpl_data = ready_arguments(self.mano_path) hands_components = smpl_data["hands_components"] self.smpl_data = smpl_data self.register_buffer( "th_betas", torch.Tensor(np.array(smpl_data["betas"].r)).unsqueeze(0)) self.register_buffer("th_shapedirs", torch.Tensor(np.array(smpl_data["shapedirs"].r))) self.register_buffer("th_posedirs", torch.Tensor(np.array(smpl_data["posedirs"].r))) self.register_buffer( "th_v_template", torch.Tensor(np.array(smpl_data["v_template"].r)).unsqueeze(0)) self.register_buffer( "th_J_regressor", torch.Tensor(np.array(smpl_data["J_regressor"].toarray()))) self.register_buffer("th_weights", torch.Tensor(np.array(smpl_data["weights"].r))) self.register_buffer( "th_faces", torch.Tensor(np.array(smpl_data["f"]).astype(np.int32)).long()) # Get hand mean hands_mean = np.zeros(hands_components.shape[1] ) if flat_hand_mean else smpl_data["hands_mean"] hands_mean = hands_mean.copy() th_hands_mean = torch.Tensor(hands_mean).unsqueeze(0) if self.use_pca or self.joint_rot_mode == "axisang": # Save as axis-angle self.register_buffer("th_hands_mean", th_hands_mean) selected_components = hands_components[:ncomps] self.register_buffer("th_selected_comps", torch.Tensor(selected_components)) elif self.joint_rot_mode == "rotmat": th_hands_mean_rotmat = rodrigues_layer.batch_rodrigues( th_hands_mean.view(15, 3)).reshape(15, 3, 3) self.register_buffer("th_hands_mean_rotmat", th_hands_mean_rotmat) elif self.joint_rot_mode == "quat": # TODO deal with flat hand mean self.register_buffer("th_hands_mean_quat", None) else: raise KeyError( "joint_rot_mode not found. shoule be one of 'axisang' or 'rotmat' or 'quat'. got {}" .format(self.joint_rot_mode)) # Kinematic chain params self.kintree_table = smpl_data["kintree_table"] parents = list(self.kintree_table[0].tolist()) self.kintree_parents = parents