in handmocap/hand_mocap_api.py [0:0]
def regress(self, img_original, hand_bbox_list, add_margin=False):
"""
args:
img_original: original raw image (BGR order by using cv2.imread)
hand_bbox_list: [
dict(
left_hand = [x0, y0, w, h] or None
right_hand = [x0, y0, w, h] or None
)
...
]
add_margin: whether to do add_margin given the hand bbox
outputs:
To be filled
Note:
Output element can be None. This is to keep the same output size with input bbox
"""
pred_output_list = list()
hand_bbox_list_processed = list()
for hand_bboxes in hand_bbox_list:
if hand_bboxes is None: # Should keep the same size with bbox size
pred_output_list.append(None)
hand_bbox_list_processed.append(None)
continue
pred_output = dict(
left_hand = None,
right_hand = None
)
hand_bboxes_processed = dict(
left_hand = None,
right_hand = None
)
for hand_type in hand_bboxes:
bbox = hand_bboxes[hand_type]
if bbox is None:
continue
else:
img_cropped, norm_img, bbox_scale_ratio, bbox_processed = \
self.__process_hand_bbox(img_original, hand_bboxes[hand_type], hand_type, add_margin)
hand_bboxes_processed[hand_type] = bbox_processed
with torch.no_grad():
# pred_rotmat, pred_betas, pred_camera = self.model_regressor(norm_img.to(self.device))
self.model_regressor.set_input_imgonly({'img': norm_img.unsqueeze(0)})
self.model_regressor.test()
pred_res = self.model_regressor.get_pred_result()
##Output
cam = pred_res['cams'][0, :] #scale, tranX, tranY
pred_verts_origin = pred_res['pred_verts'][0]
faces = self.model_regressor.right_hand_faces_local
pred_pose = pred_res['pred_pose_params'].copy()
pred_joints = pred_res['pred_joints_3d'].copy()[0]
if hand_type == 'left_hand':
cam[1] *= -1
pred_verts_origin[:, 0] *= -1
faces = faces[:, ::-1]
pred_pose[:, 1::3] *= -1
pred_pose[:, 2::3] *= -1
pred_joints[:, 0] *= -1
pred_output[hand_type] = dict()
pred_output[hand_type]['pred_vertices_smpl'] = pred_verts_origin # SMPL-X hand vertex in bbox space
pred_output[hand_type]['pred_joints_smpl'] = pred_joints
pred_output[hand_type]['faces'] = faces
pred_output[hand_type]['bbox_scale_ratio'] = bbox_scale_ratio
pred_output[hand_type]['bbox_top_left'] = np.array(bbox_processed[:2])
pred_output[hand_type]['pred_camera'] = cam
pred_output[hand_type]['img_cropped'] = img_cropped
# pred hand pose & shape params & hand joints 3d
pred_output[hand_type]['pred_hand_pose'] = pred_pose # (1, 48): (1, 3) for hand rotation, (1, 45) for finger pose.
pred_output[hand_type]['pred_hand_betas'] = pred_res['pred_shape_params'] # (1, 10)
#Convert vertices into bbox & image space
cam_scale = cam[0]
cam_trans = cam[1:]
vert_smplcoord = pred_verts_origin.copy()
joints_smplcoord = pred_joints.copy()
vert_bboxcoord = convert_smpl_to_bbox(
vert_smplcoord, cam_scale, cam_trans, bAppTransFirst=True) # SMPL space -> bbox space
joints_bboxcoord = convert_smpl_to_bbox(
joints_smplcoord, cam_scale, cam_trans, bAppTransFirst=True) # SMPL space -> bbox space
hand_boxScale_o2n = pred_output[hand_type]['bbox_scale_ratio']
hand_bboxTopLeft = pred_output[hand_type]['bbox_top_left']
vert_imgcoord = convert_bbox_to_oriIm(
vert_bboxcoord, hand_boxScale_o2n, hand_bboxTopLeft,
img_original.shape[1], img_original.shape[0])
pred_output[hand_type]['pred_vertices_img'] = vert_imgcoord
joints_imgcoord = convert_bbox_to_oriIm(
joints_bboxcoord, hand_boxScale_o2n, hand_bboxTopLeft,
img_original.shape[1], img_original.shape[0])
pred_output[hand_type]['pred_joints_img'] = joints_imgcoord
pred_output_list.append(pred_output)
hand_bbox_list_processed.append(hand_bboxes_processed)
assert len(hand_bbox_list_processed) == len(hand_bbox_list)
return pred_output_list