def align_2p(img, left_eye, right_eye):
width = 256
eye_width = 70
transform = np.matrix([
[1, 0, left_eye[0]],
[0, 1, left_eye[1]],
[0, 0, 1]
], dtype='float')
th = np.pi + -np.arctan2(left_eye[1] - right_eye[1], left_eye[0] - right_eye[0])
transform *= np.matrix([
[np.cos(th), np.sin(th), 0],
[-np.sin(th), np.cos(th), 0],
[0, 0, 1]
], dtype='float')
scale = np.sqrt((left_eye[1] - right_eye[1]) ** 2 + (left_eye[0] - right_eye[0]) ** 2) / eye_width
transform *= np.matrix([
[scale, 0, 0],
[0, scale, 0],
[0, 0, 1]
], dtype='float')
transform *= np.matrix([
[1, 0, -(width - eye_width) / 2],
[0, 1, -width / 2.42],
[0, 0, 1]
], dtype='float')
transform = np.linalg.inv(transform)
jmg = cv2.warpAffine(img, transform[:2], (width, width))
return jmg
评论列表
文章目录