# FaceSwap #
FaceSwap is an app that I have originally created as an exercise for my students in "Mathematics in Multimedia" on the Warsaw University of Technology.
The app is written in Python and uses face alignment, Gauss Newton optimization and image blending to swap the face of a person seen by the camera with a face of a person in a provided image.
You will find a short presentation the program's capabilities in the video below (click to go to YouTube):
[![click to go to YouTube](http://img.youtube.com/vi/yZRuSsyxvos/0.jpg)](http://www.youtube.com/watch?v=yZRuSsyxvos)
## How to use it ##
To start the program you will have to run a file named zad2.py (Polish for exercise 2), which will require:
* Python 2.7 (I recommend Anaconda)
* OpenCV (I used 2.4.13)
* Numpy
* dlib
* pygame
* PyOpenGL
You can download all of the libraries above either from PIP or from Christoph Gohlke's excellent website: http://www.lfd.uci.edu/~gohlke/pythonlibs/
You will also have to download the face alignment model from here: http://sourceforge.net/projects/dclib/files/dlib/v18.10/shape_predictor_68_face_landmarks.dat.bz2 and unpack it to the main project directory.
## A faster and more stable version ##
A faster and more stable version of FaceSwap is available on Dropbox [here](https://www.dropbox.com/sh/yfq0hcu0we2hed0/AABXckFonehfgLfdzicjqqJYa?dl=0). This new version is based on the [Deep Alignment Network](https://github.com/MarekKowalski/DeepAlignmentNetwork) method, which is faster than the currently used method if ran on a GPU and provides more stable and more precise facial landmarks. Please see the [GitHub repository of Deep Alignment Network](https://github.com/MarekKowalski/DeepAlignmentNetwork) for setup instructions.
I hope to find time to include this faster version in the repo code soon.
## How it works ##
The general outline of the method is as follows:
First we take the input image (the image of a person we want to see on our own face) and find the face region and its landmarks. Once we have that we fit the 3D model to those landmarks (more on that later) the vertices of that model projected to the image space will be our texture coordinates.
Once that is finished and everything is initialized the camera starts capturing images. For each captured images the following steps are taken:
1. The face region is detected and the facial landmarks are located.
2. The 3D models is fitted to the located landmarks.
3. The 3D models is rendered using pygame with the texture obtained during initialization.
4. The image of the rendered model is blended with the image obtained from the camera using feathering (alpha blending) and very simple color correction.
5. The final image is shown to the user.
The most crucial element of the entire process is the fitting of the 3D model. The model itself consists of:
* the 3D shape (set of vertices) of a neutral face,
* a number of blendshapes that can be added to the neutral face to produce mouth opening, eyebrow raising, etc.,
* a set of triplets of indices into the face shape that form the triangular mesh of the face,
* two sets of indices which establish correspondence between the landmarks found by the landmark localizer and the vertices of the 3D face shape.
The model is projected into the image space using the following equation:
![equation](http://home.elka.pw.edu.pl/~mkowals6/lib/exe/fetch.php?media=faceswap_equation.png)
where *s* is the projected shape, *a* is the scaling parameter, *P* are the first two rows of a rotation matrix that rotates the 3D face shape, *S_0* is the neutral face shape, *w_1-n* are the blendshape weights, *S_1-n* are the blendshapes, *t* is a 2D translation vector and *n* is the number of blendshapes.
The model fitting is accomplished by minimizing the difference between the projected shape and the localized landmarks. The minimization is accomplished with respect to the blendshape weights, scaling, rotation and translation, using the [Gauss Newton method](https://en.wikipedia.org/wiki/Gauss%E2%80%93Newton_algorithm).
## Licensing ##
The code is licensed under the MIT license, some of the data in the project is downloaded from 3rd party websites:
* brad pitt.jpg - https://en.wikipedia.org/wiki/Brad_Pitt#/media/File:Brad_Pitt_Fury_2014.jpg
* einstein.jpg - https://www.viewfoo.com/uploads/images/702_1433440837_albert-einstein.jpg
* jolie.jpg - http://cdni.condenast.co.uk/720x1080/a_c/Angelina-Jolie_glamour_2mar14_rex_b_720x1080.jpg
* hand.png - http://pngimg.com/upload/hands_PNG905.png
* eye.png - http://cache4.asset-cache.net/xd/521276062.jpg?v=1&c=IWSAsset&k=2&d=62CA815BFB1CE4807BD8B4D34504661CD6D7111452E48A17257DA6DB0BD6EA6DE35742C781328F67
* candide 3D face model source - http://www.icg.isy.liu.se/candide/
## Contact ##
If need help or you found the app useful, do not hesitate to let me know.
Marek Kowalski <m.kowalski@ire.pw.edu.pl>, homepage: http://home.elka.pw.edu.pl/~mkowals6/
没有合适的资源?快使用搜索试试~ 我知道了~
Python实现的3D面部交换3D换脸
共41个文件
pyc:18个
py:8个
jpg:4个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
5星 · 超过95%的资源 0 下载量 48 浏览量
2023-01-08
10:43:15
上传
评论 1
收藏 73.99MB ZIP 举报
温馨提示
Python实现的3D面部交换3D换脸
资源推荐
资源详情
资源评论
收起资源包目录
新建文件夹.zip (41个子文件)
新建文件夹
eyeHandBlend.jpg 94KB
shape_predictor_68_face_landmarks.dat 95.08MB
FaceSwap.sln 809B
data
einstein.jpg 1.42MB
hand.png 453KB
pitt.jpg 701KB
mask.png 3KB
eye.png 49KB
jolie.jpg 241KB
LICENSE 1KB
candide.npz 43KB
.gitignore 4KB
FaceSwap
utils.py 3KB
NonLinearLeastSquares.py 2KB
drawing.py 2KB
ImageProcessing.py 2KB
models.pyc 4KB
models.py 4KB
NonLinearLeastSquares.pyc 3KB
FaceRendering.pyc 3KB
FaceRendering.py 2KB
utils.pyc 3KB
ImageProcessing.pyc 2KB
zad1.py 438B
drawing.pyc 3KB
__pycache__
NonLinearLeastSquares.cpython-38.pyc 2KB
drawing.cpython-38.pyc 2KB
models.cpython-38.pyc 4KB
ImageProcessing.cpython-36.pyc 1KB
NonLinearLeastSquares.cpython-36.pyc 2KB
ImageProcessing.cpython-38.pyc 1KB
utils.cpython-38.pyc 3KB
FaceRendering.cpython-38.pyc 2KB
drawing.cpython-36.pyc 2KB
FaceRendering.cpython-36.pyc 2KB
models.cpython-36.pyc 3KB
utils.cpython-36.pyc 3KB
FaceSwap.pyproj 2KB
zad2.py 3KB
README.md 5KB
dlib-19.8.1-cp36-cp36m-win_amd64.whl 2.3MB
共 41 条
- 1
babyai997
- 粉丝: 727
- 资源: 175
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页