def align_face_3(im, key_points):
'''
Align face image by affine transfromation. The transformation matrix is computed by
3 pairs of points
input:
im: input image
key_points: [(xi, yi)], list of 21-key-point or 106-key-point
output:
im_out
'''
key_points = np.array(key_points, dtype = np.float32)
dst_points = np.array([[70.745, 112.0], [108.237, 112.0], [89.4324, 153.514]], dtype = np.float32)
dst_sz = (178, 218)
src_points = np.zeros((3, 2), dtype = np.float32)
if key_points.shape[0] == 21:
src_points[0] = key_points[16]
src_points[1] = key_points[17]
src_points[2] = (key_points[19] + key_points[20]) / 2.0
elif key_points[0] == 106:
src_points[0] = key_points[104]
src_points[1] = key_points[105]
src_points[2] = (key_points[84] + key_points[90]) / 2.0
else:
raise Exception('invalid number of face keypoints')
trans_mat = cv2.getAffineTransform(src_points, dst_points)
im_out = cv2.warpAffine(im, trans_mat, dsize = dst_sz, borderMode = cv2.BORDER_REPLICATE)
return im_out
评论列表
文章目录