def estimate_pitch_yaw_roll(aligned_landmarks, size=256): """ returns pitch,yaw,roll [-pi/2...+pi/2] """ shape = (size,size) focal_length = shape[1] camera_center = (shape[1] / 2, shape[0] / 2) camera_matrix = np.array( [[focal_length, 0, camera_center[0]], [0, focal_length, camera_center[1]], [0, 0, 1]], dtype=np.float32) (_, rotation_vector, _) = cv2.solvePnP( np.concatenate( (landmarks_68_3D[:27], landmarks_68_3D[30:36]) , axis=0) , np.concatenate( (aligned_landmarks[:27], aligned_landmarks[30:36]) , axis=0).astype(np.float32), camera_matrix, np.zeros((4, 1)) ) pitch, yaw, roll = mathlib.rotationMatrixToEulerAngles( cv2.Rodrigues(rotation_vector)[0] ) half_pi = math.pi / 2.0 pitch = np.clip ( pitch, -half_pi, half_pi ) yaw = np.clip ( yaw , -half_pi, half_pi ) roll = np.clip ( roll, -half_pi, half_pi ) return -pitch, yaw, roll
def estimate_pitch_yaw_roll(aligned_256px_landmarks): """ returns pitch,yaw,roll [-pi...+pi] """ shape = (256,256) focal_length = shape[1] camera_center = (shape[1] / 2, shape[0] / 2) camera_matrix = np.array( [[focal_length, 0, camera_center[0]], [0, focal_length, camera_center[1]], [0, 0, 1]], dtype=np.float32) (_, rotation_vector, translation_vector) = cv2.solvePnP( landmarks_68_3D, aligned_256px_landmarks.astype(np.float32), camera_matrix, np.zeros((4, 1)) ) pitch, yaw, roll = mathlib.rotationMatrixToEulerAngles( cv2.Rodrigues(rotation_vector)[0] ) pitch = np.clip ( pitch, -math.pi, math.pi ) yaw = np.clip ( yaw , -math.pi, math.pi ) roll = np.clip ( roll, -math.pi, math.pi ) return -pitch, yaw, roll #if remove_align: # bbox = transform_points ( [ (0,0), (0,output_size), (output_size, output_size), (output_size,0) ], mat, True) # #import code # #code.interact(local=dict(globals(), **locals())) # area = mathlib.polygon_area(bbox[:,0], bbox[:,1] ) # side = math.sqrt(area) / 2 # center = transform_points ( [(output_size/2,output_size/2)], mat, True) # pts1 = np.float32(( center+[-side,-side], center+[side,-side], center+[side,-side] )) # pts2 = np.float32([[0,0],[output_size,0],[0,output_size]]) # mat = cv2.getAffineTransform(pts1,pts2) #if full_face_align_top and (face_type == FaceType.FULL or face_type == FaceType.FULL_NO_ALIGN): # #lmrks2 = expand_eyebrows(image_landmarks) # #lmrks2_ = transform_points( [ lmrks2[19], lmrks2[24] ], mat, False ) # #y_diff = np.float32( (0,np.min(lmrks2_[:,1])) ) # #y_diff = transform_points( [ np.float32( (0,0) ), y_diff], mat, True) # #y_diff = y_diff[1]-y_diff[0] # # x_diff = np.float32((0,0)) # # lmrks2_ = transform_points( [ image_landmarks[0], image_landmarks[16] ], mat, False ) # if lmrks2_[0,0] < 0: # x_diff = lmrks2_[0,0] # x_diff = transform_points( [ np.float32( (0,0) ), np.float32((x_diff,0)) ], mat, True) # x_diff = x_diff[1]-x_diff[0] # elif lmrks2_[1,0] >= output_size: # x_diff = lmrks2_[1,0]-(output_size-1) # x_diff = transform_points( [ np.float32( (0,0) ), np.float32((x_diff,0)) ], mat, True) # x_diff = x_diff[1]-x_diff[0] # # mat = cv2.getAffineTransform( l_t+y_diff+x_diff ,pts2)
def estimate_pitch_yaw_roll(aligned_256px_landmarks): """ returns pitch,yaw,roll [-pi...+pi] """ shape = (256, 256) focal_length = shape[1] camera_center = (shape[1] / 2, shape[0] / 2) camera_matrix = np.array([[focal_length, 0, camera_center[0]], [0, focal_length, camera_center[1]], [0, 0, 1]], dtype=np.float32) (_, rotation_vector, translation_vector) = cv2.solvePnP( landmarks_68_3D, aligned_256px_landmarks.astype(np.float32), camera_matrix, np.zeros((4, 1))) pitch, yaw, roll = mathlib.rotationMatrixToEulerAngles( cv2.Rodrigues(rotation_vector)[0]) pitch = np.clip(pitch, -math.pi, math.pi) yaw = np.clip(yaw, -math.pi, math.pi) roll = np.clip(roll, -math.pi, math.pi) return -pitch, yaw, roll