def im_rotate(im, landmark):
"""Rotate the image according to the angle of two eyes.
Args:
landmark: 5 points, left_eye, right_eye, nose, leftmouth, right_mouth
im: image matrix
Returns:
A rotated image matrix.
Rotated angle.
Rotated landmark points.
"""
ang = math.atan2(landmark[3] - landmark[1], landmark[2] - landmark[0])
angle = ang / math.pi * 180
center = tuple(np.array((im.shape[1] / 2.0, im.shape[0] / 2.0)))
scale = 1.0
rot_mat = cv2.getRotationMatrix2D(center, angle, scale)
dst = cv2.warpAffine(im, rot_mat, (im.shape[1], im.shape[0]))
# rotate 5 landmark points
left_eye = point_trans(landmark[0:2], -ang, im.shape, im.shape)
right_eye = point_trans(landmark[2:4], -ang, im.shape, im.shape)
nose = point_trans(landmark[4:6], -ang, im.shape, im.shape)
left_mouth = point_trans(landmark[6:8], -ang, im.shape, im.shape)
right_mouth = point_trans(landmark[8:10], -ang, im.shape, im.shape)
n_landmark = np.concatenate([left_eye, right_eye, nose, left_mouth, right_mouth])
return dst, ang, n_landmark
评论列表
文章目录