摘要:人脸检测模型输出人脸目标框坐标和5个人脸关键点,在进行人脸比对前,需要对检测得到的人脸框进行对齐(纠正),本文将通过5个人脸关键点信息对人脸就行对齐(纠正)。
一、输入图像就行人脸检测:
人脸检测模型输出每个人脸的目标框坐标以及5个关键点坐标。
二、利用5个特征点进行人脸对齐(纠正)
人脸1:
人脸1纠正结果:
人脸2:
人脸2纠正后结果:
人脸3:
人脸3纠正后结果:
三、人脸对齐(纠正)代码示例:
python
import cv2
import numpy as np
import skimage.transform
ST_KEYPOINT = np.array(
[
[0.34191607, 0.46157411],
[0.65653393, 0.45983393],
[0.500225, 0.64050536],
[0.37097589, 0.82469196],
[0.63151696, 0.82325089]]
, dtype=np.float32)
def get_aligned_face(image, keypoint, align_size):
'''
Args:
image: origin imsge
keypoint: five keypoints with shape of (5, 2)
align_size: output size of (w, h), exp: (112, 112)
Returns:
aligned face with the size of align_size
'''
st_kp = ST_KEYPOINT * np.array(align_size)
st = skimage.transform.SimilarityTransform() # define the function of affine transformation
st.estimate(keypoint, st_kp) # calculate the matrix of affine transformation
align_face = cv2.warpAffine(image, st.params[0:2, :], align_size, flags=cv2.INTER_LINEAR, borderValue=0.0) # face align
return align_face