```bash
简单换脸、人脸对齐、关键点定位与画图
有人将其进行中文翻译也有将其进行一定改编有以下两个案例:
1.《川普撞脸希拉里(基于 OpenCV 的面部特征交换)-2》
变脸
变脸贴图:
从这张: 这里写图片描述 变为这张: 这里写图片描述
因为原文里面内容丰富,我觉得可以提取出很多有用的小模块,于是乎: .
提取一:关键点定位与画图
import cv2
import dlib
import numpy
import sys
import matplotlib.pyplot as plt
SCALE_FACTOR = 1
def read_im_and_landmarks(fname):
im = cv2.imread(fname, cv2.IMREAD_COLOR)
im = cv2.resize(im, (im.shape[1] * SCALE_FACTOR,
im.shape[0] * SCALE_FACTOR))
s = get_landmarks(im)
return im, s
def annotate_landmarks(im, landmarks):
'''
人脸关键点,画图函数
'''
im = im.copy()
for idx, point in enumerate(landmarks):
pos = (point[0, 0], point[0, 1])
cv2.putText(im, str(idx), pos,
fontFace=cv2.FONT_HERSHEY_SCRIPT_SIMPLEX,
fontScale=0.4,
color=(0, 0, 255))
cv2.circle(im, pos, 3, color=(0, 255, 255))
return im
然后实践就是载入原图:
im1, landmarks1 = read_im_and_landmarks('02.jpg')
im1 = annotate_landmarks(im1, landmarks1)
%matplotlib inline
plt.subplot(111)
plt.imshow(im1)
.
提取二:人脸对齐
需要一张模板图来作为靠拢的对象图。
def face_Align(Base_path,cover_path):
im1, landmarks1 = read_im_and_landmarks(Base_path)
im2, landmarks2 = read_im_and_landmarks(cover_path)
if len(landmarks1) == 0 & len(landmarks2) == 0 :
raise ImproperNumber("Faces detected is no face!")
if len(landmarks1) > 1 & len(landmarks2) > 1 :
raise ImproperNumber("Faces detected is more than 1!")
M = transformation_from_points(landmarks1[ALIGN_POINTS],
landmarks2[ALIGN_POINTS])
warped_im2 = warp_im(im2, M, im1.shape)
return warped_im2
这里的步骤是:
提取模板图、对齐图的landmarks;
通过transformation_from_points计算对齐图向模板图的转移矩阵M,变换矩阵是根据以下公式计算出来的;
warp_im,将 im2 的掩码进行变化,使之与 im1 相符
实践的话就是:
FEATHER_AMOUNT = 19
Base_path = '01.jpg'
cover_path = '02.jpg'
warped_mask = face_Align(Base_path,cover_path)
.
提取三:换脸
主要函数:
def Switch_face(Base_path,cover_path):
im1, landmarks1 = read_im_and_landmarks(Base_path)
im2, landmarks2 = read_im_and_landmarks(cover_path)
if len(landmarks1) == 0 & len(landmarks2) == 0 :
raise ImproperNumber("Faces detected is no face!")
if len(landmarks1) > 1 & len(landmarks2) > 1 :
raise ImproperNumber("Faces detected is more than 1!")
M = transformation_from_points(landmarks1[ALIGN_POINTS],
landmarks2[ALIGN_POINTS])
mask = get_face_mask(im2, landmarks2)
warped_mask = warp_im(mask, M, im1.shape)
combined_mask = numpy.max([get_face_mask(im1, landmarks1), warped_mask],
axis=0)
warped_im2 = warp_im(im2, M, im1.shape)
warped_corrected_im2 = correct_colours(im1, warped_im2, landmarks1)
output_im = im1 * (1.0 - combined_mask) + warped_corrected_im2 * combined_mask
return output_im
主要步骤:
提取模板图、对齐图的landmarks;
M,通过transformation_from_points计算对齐图向模板图的转移矩阵M;
matrix([[ 0.62876962, 0.20978991, -101.32973923],
[ -0.20978991, 0.62876962, 79.11235991],
[ 0. , 0. , 1. ]])
mask,得到基于对齐图的掩膜,get_face_mask函数,获取 im2 的面部掩码,mask长成这样: 这里写图片描述
warped_mask ,warp_im函数,将 im2 的掩码进行变化,使之与 im1 相符,跟上面的mask张一样(一个鼻子)
combined_mask ,将二者的掩码进行连通(跟warped_mask 长一样)
warped_im2 ,warp_im函数,第二次,将第二幅图像调整到与第一幅图像相符(对齐图片,斜了点)
warped_corrected_im2 ,correct_colours函数,将 im2 的皮肤颜色进行修正,使其和 im1 的颜色尽量协调(类似下图)
output_im 组合图像,获得结果
实践:
FEATHER_AMOUNT = 23
Base_path = '03.jpg'
cover_path = '02.jpg'
output_im = Switch_face(Base_path,cover_path)