<Python>PyQt5+OpenCv+Dlib人脸合成程序

Dlib人脸合成

  • 1 概述:
  • 2 软件环境版本
  • 3 程序实现
  • 4 视频演示:

更新日志:
202211031332:添加初版

1 概述:

Dlib是开源且免费的代码,其中有用于人脸识别、合成的功能。
无论对人的脸部进行任何处理,都是在处理图像,所以,人脸合成实际上就是对像素的操控,这里面当然涉及图像的算法,不用去深究,因为我们不是研究人员,而是要使用已经造好的轮子,只需要知道怎么去使用即可。

人脸合成流程大致如下:
加载脸部图像-生成脸部特征蒙版-进行匹配转换-生成替换蒙版-进行色彩匹配-合成。这每一个流程都可以在dlib的官网找到源程序,dlib官网链接:
Dlib官网:http://dlib.net/
<Python>PyQt5+OpenCv+Dlib人脸合成程序_第1张图片
所以,我们想要实现人脸合成,核心就是dlib的源码。在dlib代码的基础上,加上pyqt5制作UI界面,方便操作:
<Python>PyQt5+OpenCv+Dlib人脸合成程序_第2张图片
UI界面大致如上图所示。

其中,对于图像的读取、处理,用到opencv和numpy,所以,在开始之前,你需要先安装以下模块:

pip install dlib
pip install opencv-python
pip install numpy
pip install PyQt5

等等,其他需要的模块,都安装一下即可。
这里面dlib的安装可能要复杂一些,如果你直接安装dlib,可能会报错,提示你需要先安装cmake:

pip install cmake

安装成功后,再安装dlib,我这边测试了一下,python3.11安装不了dlib,python3.10是可以的。

安装成功:
<Python>PyQt5+OpenCv+Dlib人脸合成程序_第3张图片

2 软件环境版本

1、visual studio code:1.73.0
2、window10:专业版
3、python:3.10.8
4、dlib:19.24.0(一般都是最新版本)
<Python>PyQt5+OpenCv+Dlib人脸合成程序_第4张图片

3 程序实现

1、UI界面:
主要用PyQt5实现,方便且简单。

class face_make(QMainWindow):
    """主窗口"""
    def __init__(self):
        super().__init__()
        self.initUI()
    def initUI(self):
        """UI初始化"""

        #QSS样式
        self.stylefile='F01_face_detect_pro2022\qss_main_style.qss'
        self.qss1=QssRead.readQSS(self.stylefile)

        #UI布局
        self.btn_source_img=QPushButton('选择源图片',self)
        self.btn_source_img.setGeometry(100,100,80,20)
        self.btn_source_img.clicked.connect(self.sourceimg_load_func)
        
        self.lbl_source_imgpath=QLabel(self)
        self.lbl_source_imgpath.setGeometry(100,130,80,20)
        self.lbl_source_imgpath.setText('源图片路径:')
        
        self.lbl_source_imgpath_2=QLabel(self)
        self.lbl_source_imgpath_2.setGeometry(200,130,400,20)
        self.lbl_source_imgpath_2.setStyleSheet("background-color:#EEEBEB;")
        self.lbl_source_imgpath_2.setText('')
        
        self.btn_second_img=QPushButton('选择合成图片',self)
        self.btn_second_img.setGeometry(100,160,80,20)
        self.btn_second_img.clicked.connect(self.secondimg_load_func)
        
        self.lbl_second_imgpath=QLabel(self)
        self.lbl_second_imgpath.setGeometry(100,190,80,20)
        self.lbl_second_imgpath.setText('合成图片路径:')
        
        self.lbl_second_imgpath_2=QLabel(self)
        self.lbl_second_imgpath_2.setGeometry(200,190,400,20)
        self.lbl_second_imgpath_2.setStyleSheet("background-color:#eeebeb;")
        self.lbl_second_imgpath_2.setText('')
        
        self.btn_dest_img=QPushButton('设置目标路径',self)
        self.btn_dest_img.setGeometry(100,220,80,20)
        self.btn_dest_img.clicked.connect(self.dest_imgpath_set_func)
        
        self.lbl_dest_imgpath=QLabel(self)
        self.lbl_dest_imgpath.setGeometry(100,250,90,20)
        self.lbl_dest_imgpath.setText('目标图片路径:')
        
        self.lbl_dest_imgpath_2=QLabel(self)
        self.lbl_dest_imgpath_2.setGeometry(200,250,400,20)
        self.lbl_dest_imgpath_2.setStyleSheet("background-color:#EEEBEB;")
        self.lbl_dest_imgpath_2.setText('')
        
        self.btn_start_make=QPushButton('开始合成',self)
        self.btn_start_make.setGeometry(100,280,80,20)
        self.btn_start_make.clicked.connect(self.facemake_start_func)
        
        self.setWindowTitle('人脸合成程序')
        self.setGeometry(100,100,1000,600)
        self.setWindowIcon(QIcon('F01_face_detect_pro2022\\face1.png'))
        self.setStyleSheet(self.qss1)
        self.show()

2、dlib代码:

获取蒙版:

def get_landmarks(im):
    """获取蒙版"""
    rects = detector(im, 1)

    if len(rects) > 1:
        print(len(rects))
        # raise TooManyFaces
    if len(rects) == 0:
        print(len(rects))
        # raise NoFaces

    return numpy.matrix([[p.x, p.y] for p in predictor(im, rects[0]).parts()])

匹配脸型:

def warp_im(im, M, dshape):
    """匹配脸型"""
    
    output_im = numpy.zeros(dshape, dtype=im.dtype)
    cv2.warpAffine(im,
                   M[:2],
                   (dshape[1], dshape[0]),
                   dst=output_im,
                   borderMode=cv2.BORDER_TRANSPARENT,
                   flags=cv2.WARP_INVERSE_MAP)
    return output_im

完整程序

#!/usr/bin/python

# Copyright (c) 2015 Matthew Earl
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
#     The above copyright notice and this permission notice shall be included
#     in all copies or substantial portions of the Software.
#
#     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
#     OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
#     MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
#     NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
#     DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
#     OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
#     USE OR OTHER DEALINGS IN THE SOFTWARE.

"""
This is the code behind the Switching Eds blog post:
    http://matthewearl.github.io/2015/07/28/switching-eds-with-python/
See the above for an explanation of the code below.
To run the script you'll need to install dlib (http://dlib.net) including its
Python bindings, and OpenCV. You'll also need to obtain the trained model from
sourceforge:
    http://sourceforge.net/projects/dclib/files/dlib/v18.10/shape_predictor_68_face_landmarks.dat.bz2
Unzip with `bunzip2` and change `PREDICTOR_PATH` to refer to this file. The
script is run like so:
    ./faceswap.py  
If successful, a file `output.jpg` will be produced with the facial features
from `` replaced with the facial features from ``.
"""

import cv2
import dlib
import numpy
import sys
import os
import matplotlib.pyplot as plt
from matplotlib.image import *
from matplotlib.font_manager import FontProperties
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from qss_read import QssRead
from PIL import Image

#用于matplotlib显示图片标题可现实中文
#第一种方法
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False

#第二种方法
#pltFont=FontProperties(fname=r"C:\Windows\\Fonts\simsun.ttc",size=20)


PREDICTOR_PATH = "F01_face_detect_pro2022\shape_predictor_68_face_landmarks.dat"
SCALE_FACTOR = 1
FEATHER_AMOUNT = 11

FACE_POINTS = list(range(17, 68))
MOUTH_POINTS = list(range(48, 61))
RIGHT_BROW_POINTS = list(range(17, 22))
LEFT_BROW_POINTS = list(range(22, 27))
RIGHT_EYE_POINTS = list(range(36, 42))
LEFT_EYE_POINTS = list(range(42, 48))
NOSE_POINTS = list(range(27, 35))
JAW_POINTS = list(range(0, 17))

# Points used to line up the images.
ALIGN_POINTS = (LEFT_BROW_POINTS + RIGHT_EYE_POINTS + LEFT_EYE_POINTS +
                RIGHT_BROW_POINTS + NOSE_POINTS + MOUTH_POINTS)

# Points from the second image to overlay on the first. The convex hull of each
# element will be overlaid.
OVERLAY_POINTS = [
    LEFT_EYE_POINTS + RIGHT_EYE_POINTS + LEFT_BROW_POINTS + RIGHT_BROW_POINTS,
    NOSE_POINTS + MOUTH_POINTS,
]

# Amount of blur to use during colour correction, as a fraction of the
# pupillary distance.
COLOUR_CORRECT_BLUR_FRAC = 0.6

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(PREDICTOR_PATH)


class TooManyFaces(Exception):
    pass


class NoFaces(Exception):
    pass



def get_landmarks(im):
    """获取蒙版"""
    rects = detector(im, 1)

    if len(rects) > 1:
        print(len(rects))
        # raise TooManyFaces
    if len(rects) == 0:
        print(len(rects))
        # raise NoFaces

    return numpy.matrix([[p.x, p.y] for p in predictor(im, rects[0]).parts()])


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


def draw_convex_hull(im, points, color):
    points = cv2.convexHull(points)
    cv2.fillConvexPoly(im, points, color=color)


def get_face_mask(im, landmarks):
    """获取人脸蒙版"""
    im = numpy.zeros(im.shape[:2], dtype=numpy.float64)

    for group in OVERLAY_POINTS:
        draw_convex_hull(im,
                         landmarks[group],
                         color=1)

    im = numpy.array([im, im, im]).transpose((1, 2, 0))

    im = (cv2.GaussianBlur(im, (FEATHER_AMOUNT, FEATHER_AMOUNT), 0) > 0) * 1.0
    im = cv2.GaussianBlur(im, (FEATHER_AMOUNT, FEATHER_AMOUNT), 0)

    #cv2.imshow('res',im)
    #cv2.waitKey(0)

    return im


def transformation_from_points(points1, points2):
    """
    Return an affine transformation [s * R | T] such that:
        sum ||s*R*p1,i + T - p2,i||^2
    is minimized.
    """
    # Solve the procrustes problem by subtracting centroids, scaling by the
    # standard deviation, and then using the SVD to calculate the rotation. See
    # the following for more details:
    #   https://en.wikipedia.org/wiki/Orthogonal_Procrustes_problem

    points1 = points1.astype(numpy.float64)
    points2 = points2.astype(numpy.float64)

    c1 = numpy.mean(points1, axis=0)
    c2 = numpy.mean(points2, axis=0)
    points1 -= c1
    points2 -= c2

    s1 = numpy.std(points1)
    s2 = numpy.std(points2)
    points1 /= s1
    points2 /= s2

    U, S, Vt = numpy.linalg.svd(points1.T * points2)

    # The R we seek is in fact the transpose of the one given by U * Vt. This
    # is because the above formulation assumes the matrix goes on the right
    # (with row vectors) where as our solution requires the matrix to be on the
    # left (with column vectors).
    R = (U * Vt).T

    return numpy.vstack([numpy.hstack(((s2 / s1) * R,
                                       c2.T - (s2 / s1) * R * c1.T)),
                         numpy.matrix([0., 0., 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)

    # cv2.imshow('res',im)
    # cv2.waitKey(0)

    return im, s


def warp_im(im, M, dshape):
    """匹配脸型"""
    
    output_im = numpy.zeros(dshape, dtype=im.dtype)
    cv2.warpAffine(im,
                   M[:2],
                   (dshape[1], dshape[0]),
                   dst=output_im,
                   borderMode=cv2.BORDER_TRANSPARENT,
                   flags=cv2.WARP_INVERSE_MAP)
    return output_im


def correct_colours(im1, im2, landmarks1):
    """修正颜色"""
    blur_amount = COLOUR_CORRECT_BLUR_FRAC * numpy.linalg.norm(
        numpy.mean(landmarks1[LEFT_EYE_POINTS], axis=0) -
        numpy.mean(landmarks1[RIGHT_EYE_POINTS], axis=0))
    blur_amount = int(blur_amount)
    if blur_amount % 2 == 0:
        blur_amount += 1
    im1_blur = cv2.GaussianBlur(im1, (blur_amount, blur_amount), 0)
    im2_blur = cv2.GaussianBlur(im2, (blur_amount, blur_amount), 0)

    # Avoid divide-by-zero errors.
    im2_blur += (128 * (im2_blur <= 1.0)).astype(im2_blur.dtype)

    return (im2.astype(numpy.float64) * im1_blur.astype(numpy.float64) /
            im2_blur.astype(numpy.float64))


def face_face_dect(in1, in2, out1):
    """人脸合成"""
    im1, landmarks1 = read_im_and_landmarks(in1)
    im2, landmarks2 = read_im_and_landmarks(in2)

    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
    output_im = cv2.resize(output_im, (632, 632), interpolation=cv2.INTER_NEAREST)

    cv2.imwrite(out1, output_im)
    img123=cv2.imread(out1)
    #cv2.imshow('outimg',img123)
    #cv2.waitKey(0)
    QMessageBox.information(None,'hh','合成OK!')
    plt.subplot()
    plt.title('合成图片')
    plt.imshow(img123)
    plt.show()
    #print('人脸合成OK')


#face_face_dect('wzx1.jpeg', 'ym3.jpeg', 'output2021.png')
class face_make(QMainWindow):
    """主窗口"""
    def __init__(self):
        super().__init__()
        self.initUI()
    def initUI(self):
        """UI初始化"""

        #QSS样式
        self.stylefile='F01_face_detect_pro2022\qss_main_style.qss'
        self.qss1=QssRead.readQSS(self.stylefile)

        #UI布局
        self.btn_source_img=QPushButton('选择源图片',self)
        self.btn_source_img.setGeometry(100,100,80,20)
        self.btn_source_img.clicked.connect(self.sourceimg_load_func)
        
        self.lbl_source_imgpath=QLabel(self)
        self.lbl_source_imgpath.setGeometry(100,130,80,20)
        self.lbl_source_imgpath.setText('源图片路径:')
        
        self.lbl_source_imgpath_2=QLabel(self)
        self.lbl_source_imgpath_2.setGeometry(200,130,400,20)
        self.lbl_source_imgpath_2.setStyleSheet("background-color:#EEEBEB;")
        self.lbl_source_imgpath_2.setText('')
        
        self.btn_second_img=QPushButton('选择合成图片',self)
        self.btn_second_img.setGeometry(100,160,80,20)
        self.btn_second_img.clicked.connect(self.secondimg_load_func)
        
        self.lbl_second_imgpath=QLabel(self)
        self.lbl_second_imgpath.setGeometry(100,190,80,20)
        self.lbl_second_imgpath.setText('合成图片路径:')
        
        self.lbl_second_imgpath_2=QLabel(self)
        self.lbl_second_imgpath_2.setGeometry(200,190,400,20)
        self.lbl_second_imgpath_2.setStyleSheet("background-color:#eeebeb;")
        self.lbl_second_imgpath_2.setText('')
        
        self.btn_dest_img=QPushButton('设置目标路径',self)
        self.btn_dest_img.setGeometry(100,220,80,20)
        self.btn_dest_img.clicked.connect(self.dest_imgpath_set_func)
        
        self.lbl_dest_imgpath=QLabel(self)
        self.lbl_dest_imgpath.setGeometry(100,250,90,20)
        self.lbl_dest_imgpath.setText('目标图片路径:')
        
        self.lbl_dest_imgpath_2=QLabel(self)
        self.lbl_dest_imgpath_2.setGeometry(200,250,400,20)
        self.lbl_dest_imgpath_2.setStyleSheet("background-color:#EEEBEB;")
        self.lbl_dest_imgpath_2.setText('')
        
        self.btn_start_make=QPushButton('开始合成',self)
        self.btn_start_make.setGeometry(100,280,80,20)
        self.btn_start_make.clicked.connect(self.facemake_start_func)
        
        self.setWindowTitle('人脸合成程序')
        self.setGeometry(100,100,1000,600)
        self.setWindowIcon(QIcon('F01_face_detect_pro2022\\face1.png'))
        self.setStyleSheet(self.qss1)
        self.show()
        
    def sourceimg_load_func(self):
        """源图片加载"""
        self.sourceimg_path,_=QFileDialog.getOpenFileName(self,'open file','','图片file(*.png *.bmp *.jpg *.jpeg)')
        if os.path.exists(self.sourceimg_path):
            self.lbl_source_imgpath_2.setText(self.sourceimg_path)
            #img=Image.open(self.sourceimg_path)
            #img.show()
            img=imread(self.sourceimg_path)
            #plt.figure(2)
            plt.subplot(221)
            #plt.title('源图片',fontproperties=pltFont)
            plt.title('源图片')
            plt.imshow(img)
            #plt.show()
        
    def secondimg_load_func(self):
        """待合成图片加载"""
        self.secondimg_path,_=QFileDialog.getOpenFileName(self,'open file','','图片file(*.png *.bmp *.jpg *.jpeg)')
        if os.path.exists(self.secondimg_path):
            self.lbl_second_imgpath_2.setText(self.secondimg_path)
            
            img=imread(self.secondimg_path)
            
            #plt.figure(1)
            plt.subplot(222)
            plt.title('待合成图片')
            plt.imshow(img)
            plt.show()
        
    def dest_imgpath_set_func(self):
        self.destimg_path,_=QFileDialog.getSaveFileName(self,'保存路径','','图片file(*.png *.bmp *.jpg)')
        self.lbl_dest_imgpath_2.setText(self.destimg_path)
        
    def facemake_start_func(self):
        """开始合成图片"""
        
        face_face_dect(self.sourceimg_path,self.secondimg_path,self.destimg_path)
        
if __name__ == '__main__':

    app=QApplication(sys.argv)
    fm1=face_make()
    sys.exit(app.exec_())

4 视频演示:

dlib人脸合成演示

你可能感兴趣的:(python,编程世界,opencv,python,dlib,qt,pyqt5)