1 换脸难点
(1)人类面部几何形状差异明显,并且存在一些异常值;
(2)光照使得面部肤色色调发生偏移;
(3)头部姿态影响;
(4)皮肤纹理差异。
2 换脸方案
%matplotlib inline
from utils.plot_imgs import *
#from utils.delaunay_triangulation import *
from utils.dlib_face_api import *
import cv2
import glob
import os
2.1 面部对齐
用源图像 g ( x , y ) g(x, y) g(x,y)面部区域覆盖目标图像 f ∗ ( x , y ) f^{\ast}(x, y) f∗(x,y)面部区域。
# 读取示例图像
filename_src = "./img/ted_cruz.jpg"
filename_dst = "./img/donald_trump.jpg"
img_src = cv2.imread(filename_src)
img_dst = cv2.imread(filename_dst)
img_dst_warped = np.copy(img_dst)
idx_fig = 1
plot_imgs(idx_fig, [img_src, img_dst])
2.1.1 面部关键点定位
(1)面部关键点定位(68,dlib)
dlib.get_frontal_face_detector()
dlib.shape_predictor()
(2)狄洛尼三角剖分
面部外轮廓关键点(jaw[1 - 17]、right_brow[18 - 22]、left_brow[23 - 27])
# 读取关键点
def read_points(str_file_path):
lst_points = []
with open(str_file_path) as fr:
for str_line in fr.readlines():
lst_points.append(
[int(item) for item in str_line.strip().split(" ")])
return lst_points
# 面部关键点
points_src = read_points(filename_src + ".txt")
points_dst = read_points(filename_dst + ".txt")
2.1.2 凸包
凸包(convex hull):没有凹陷的边界
cv2.convexHull()
# 凸包
hull_pt_src = []
hull_pt_dst = []
hull_pt_indices = cv2.convexHull(np.array(points_dst),
returnPoints = False)
hull_pt_indices = hull_pt_indices.