Python 实现人脸融合与虚拟人物生成,只需要准备两张人脸图片,最好是大头照,这样融合效果才会更好。
实现步骤:
(1)准备两张图片(人脸)
(2)准备好如下过程Python利用paddlehub实现批量抠图和更换证件照背景底色_SmartGridequation的博客-优快云博客
(3)利用OpenCV去除图的单一颜色
Python 去除图片中多种颜色或者单一颜色_SmartGridequation的博客-优快云博客
融合人脸函数代码如下:
import numpy as np
import cv2
import dlib
from scipy.spatial import Delaunay
predictor_model = 'shape_predictor_68_face_landmarks.dat'
#-i https://mirrors.aliyun.com/pypi/simple/
def get_points(image): # 用 dlib 来得到人脸的特征点
global detected_face
face_detector = dlib.get_frontal_face_detector() # 正向人脸检测器,进行人脸检测,提取人脸外部矩形框
face_pose_predictor = dlib.shape_predictor(predictor_model)
try:
detected_face = face_detector(image, 1)[0]
except:
print('No face detected in image {}'.format(image))
pose_landmarks = face_pose_predictor(image, detected_face) # 获取landmark
points = []
for p in pose_landmarks.parts():
points.append([p.x, p.y])
# 加入四个顶点和四条边的中点
x = image.shape[1] - 1
y = image.shape[0] - 1
points.append([0, 0])
points.append([x // 2, 0])
points.append([x, 0])
points.append([x, y // 2])
points.append([x, y])
points.append([x // 2, y])
points.append([0, y])
points.append([0, y // 2])
return np.array(points)
def get_triangles(points): # 在特征点上使用 Delaunay 三角剖分,将点集连接成一定大小的三角形,且分配要相对合理,才能呈现出漂亮的三角化
return Delaunay(points).simplices
def affine_trans

最低0.47元/天 解锁文章
2万+

被折叠的 条评论
为什么被折叠?



