2019/02/09 14:55
阅读数 1K

cd faceswap

cd faceswap-master

python faceswap.py gui


Notice: This repository is not operated or maintained by /u/deepfakes. Please read the explanation below for details.


Faceswap is a tool that utilizes deep learning to recognize and swap faces in pictures and videos.



Faceswap is not porn.

When faceswaping using an AI was first developed and became published, the technology was groundbreaking, it was a huge step in AI development. It was also completely ignored outside of academia. The code was confusing and fragmentary, it required a thorough understanding of state of the art AI techniques and a lot of effort to get anything out of it. One individual brought it together into one cohesive collection. It ran, it worked, and as is so often the way with new technology emerging on the internet, it was immediately used to create porn. The problem was that this was the first AI code that anyone could download, run and learn by experimentation without becoming a PHD candidate in math, computer theory, psychology, and more. Before "deepfakes" these techniques were like black magic, only practiced by those who could understand all of the inner workings as described in esoteric and endlessly complicated books and papers.


"Deepfakes" changed all that and anyone could participate in AI development. To us developers, the release of this code has opened up a fantastic learning opportunity. To build on ideas developed by others, to collaborate with coders with a huge variety of skills, to experiment with AI whilst learning new skills and ultimately contribute towards an emerging technology which will only see more mainstream use as it progresses.


Are there some out there doing horrible things with similar software? Yes. And because of this, the developers have been following strict ethical standards. Many of us don't even use it to create videos at all, we just tinker with the code to see what it all does. Sadly, the media concentrates only on the unethical uses of this software. That is unfortunately a nature of how it was first exposed to the public, but it is not representative of why it was created, how we use it now, or what we see in it's future. Like any technology, it can be used for good or it can be abused. It is our intention to develop faceswap in a way that it's potential for abuse is minimized whilst maximizing it's potential as a tool for learning, experimenting and, yes, for legitimate faceswaping.


We are not trying to denigrate celebrities or to demean anyone. We are programmers, we are engineers, we are Hollywood VFX artists, we are activists, we are hobbyists, we are human beings. To this end, we feel that it's time to come out with a standard statement of what this software is and isn't as far as us developers are concerned.


  • Faceswap is not for creating porn     Faceswap不用于创建色情
  • Faceswap is not for changing faces without consent or with the intent of hiding it's use.    Faceswap不适用于未经同意或意图隐藏其用途的更改面。
  • Faceswap is not for any illicit, unethical, or questionable purposes.   faceswap不用于任何非法、不道德或可疑的目的。
  • Faceswap exists to experiment and discover AI techniques, for social or political commentary, for movies, and for any number of ethical and reasonable uses.    Faceswap的存在是为了试验和发现人工智能技术、社会或政治评论、电影以及任何道德和合理的用途。

We are very troubled by the fact that faceswap can be used for unethical and disreputable things. However, we support the development of tools and techniques that can be used ethically as well as provide education and experience in AI for anyone who wants to learn it hands-on. We will take a zero tolerance approach to anyone using this software for any unethical purposes and will actively discourage any such uses.我们很不安的事实是,faceswap可以用于不道德和不名誉的事情。然而,我们支持开发可用于伦理的工具和技术,并为任何想亲自学习人工智能的人提供教育和经验。我们将对任何出于任何不道德目的使用本软件的人采取零容忍的方法,并将积极鼓励任何此类使用。

How To setup and run the project如何设置和运行项目

Faceswap is a Python program that will run on multiple Operating Systems including Windows, Linux and MacOS.

See INSTALL.md for full installation instructions. You will need a modern GPU with CUDA support for best performance.


The project has multiple entry points. You will have to:该项目有多个入口点。你必须:

  • Gather photos (or use the one provided in the training data provided below) 收集图像(或使用下面提供的训练数据)
  • Extract faces from your raw photos 从原始照片中提取人脸
  • Train a model on your photos (or use the one provided in the training data provided below) 在照片上训练模型(或使用下面提供的训练数据中提供的模型)
  • Convert your sources with the model 用上一步训练好的模型转换你的源

Check out USAGE.md for more detailed instructions. 查看USAGE.md里面有更详细的指导


From your setup folder, run python faceswap.py extract. This will take photos from src folder and extract faces into extract folder.

从安装文件夹中,运行python faceswap.py extract。这将从SRC文件夹拍摄照片,并将面提取到extract文件夹中。


From your setup folder, run python faceswap.py train. This will take photos from two folders containing pictures of both faces and train a model that will be saved inside the models folder.

从设置文件夹中,运行python faceswap.py train。这将从包含两个面的图片的两个文件夹中拍摄照片,并训练将保存在model文件夹中的模型。


From your setup folder, run python faceswap.py convert. This will take photos from original folder and apply new faces into modified folder.

从安装文件夹中,运行python faceswap.py convert。这将从原始文件夹拍摄照片,并将新面应用到modified文件夹中。


Alternatively you can run the GUI by running python faceswap.py gui

或者,您可以通过运行python faceswap.py gui来运行gui。

General notes:

  • All of the scripts mentioned have -h/--help options with arguments that they will accept. You're smart, you can figure out how this works, right?!

NB: there is a conversion tool for video. This can be accessed by running python tools.py effmpeg -h. Alternatively you can use ffmpeg to convert video into photos, process images, and convert images back to video.

Some tips:

Reusing existing models will train much faster than starting from nothing.
If there is not enough training data, start with someone who looks similar, then switch the data.

Help I need support!

Discord Server

Your best bet is to join the Faceswap Discord server where there are plenty of users willing to help. Please note that, like this repo, this is a SFW Server!


Alternatively you can post questions in the Faceswap Playground. Please do not post general support questions in this repo.

How to contribute

For people interested in the generative models

  • Go to the 'faceswap-model' to discuss/suggest/commit alternatives to the current algorithm.

For devs

  • Read this README entirely
  • Fork the repo
  • Download the data with the link provided above
  • Play with it
  • Check issues with the 'dev' tag
  • For devs more interested in computer vision and openCV, look at issues with the 'opencv' tag. Also feel free to add your own alternatives/improvments

For non-dev advanced users

  • Read this README entirely
  • Clone the repo
  • Download the data with the link provided above
  • Play with it
  • Check issues with the 'advuser' tag
  • Also go to the 'faceswap-playground' repo and help others.

For end-users

  • Get the code here and play with it if you can
  • You can also go to the 'faceswap-playground' repo and help or get help from others.
  • Be patient. This is relatively new technology for developers as well. Much effort is already being put into making this program easy to use for the average user. It just takes time!
  • Notice Any issue related to running the code has to be open in the 'faceswap-playground' project!

For haters

Sorry, no time for that.

About github.com/deepfakes

What is this repo?

It is a community repository for active users.

Why this repo?

The joshua-wu repo seems not active. Simple bugs like missing http:// in front of urls have not been solved since days.

Why is it named 'deepfakes' if it is not /u/deepfakes?

  1. Because a typosquat would have happened sooner or later as project grows
  2. Because we wanted to recognize the original author
  3. Because it will better federate contributors and users

What if /u/deepfakes feels bad about that?

This is a friendly typosquat, and it is fully dedicated to the project. If /u/deepfakes wants to take over this repo/user and drive the project, he is welcomed to do so (Raise an issue, and he will be contacted on Reddit). Please do not send /u/deepfakes messages for help with the code you find here.

About machine learning

How does a computer know how to recognise/shape a faces? How does machine learning work? What is a neural network?

It's complicated. Here's a good video that makes the process understandable: 

Here's a slightly more in depth video that tries to explain the basic functioning of a neural network: 

tl;dr: training data + trial and error








Before attempting any of this, please make sure you have read, understood and completed the installation instructions. If you are experiencing issues, please raise them in the faceswap-playground repository instead of the main repo.


So, you want to swap faces in pictures and videos? Well hold up, because first you gotta understand what this collection of scripts will do, how it does it and what it can't currently do.

The basic operation of this script is simple. It trains a machine learning model to recognize and transform two faces based on pictures. The machine learning model is our little "bot" that we're teaching to do the actual swapping and the pictures are the "training data" that we use to train it. Note that the bot is primarily processing faces. Other objects might not work.

So here's our plan. We want to create a reality where Donald Trump lost the presidency to Nic Cage; we have his inauguration video; let's replace Trump with Cage.

Gathering raw data

In order to accomplish this, the bot needs to learn to recognize both face A (Trump) and face B (Nic Cage). By default, the bot doesn't know what a Trump or a Nic Cage looks like. So we need to show it some pictures and let it guess which is which. So we need pictures of both of these faces first.

A possible source is Google, DuckDuckGo or Bing image search. There are scripts to download large amounts of images. Alternatively, if you have a video of the person you're looking for (from interviews, public speeches, or movies), you can convert this video to still images and use those. see Extracting video frames for more information.

Feel free to list your image sets in the faceswap-playground, or add more methods to this file.

So now we have a folder full of pictures of Trump and a separate folder of Nic Cage. Let's save them in our directory where we put the faceswap project. Example: ~/faceswap/photo/trump and ~/faceswap/photo/cage


So here's a problem. We have a ton of pictures of both our subjects, but they're just pictures of them doing stuff or in an environment with other people. Their bodies are on there, they're on there with other people... It's a mess. We can only train our bot if the data we have is consistent and focusses on the subject we want to swap. This is where faceswap first comes in.

# To convert trump:
python faceswap.py extract -i ~/faceswap/photo/trump -o ~/faceswap/data/trump
# To convert cage: python faceswap.py extract -i ~/faceswap/photo/cage -o ~/faceswap/data/cage

We specify our photo input directory and the output folder where our training data will be saved. The script will then try its best to recognize face landmarks, crop the image to that size, and save it to the output folder. Note: this script will make grabbing test data much easier, but it is not perfect. It will (incorrectly) detect multiple faces in some photos and does not recognize if the face is the person who we want to swap. Therefore: Always check your training data before you start training. The training data will influence how good your model will be at swapping.

You can see the full list of arguments for extracting via help flag. i.e.

python faceswap.py extract -h


The training process will take the longest, especially on CPU. We specify the folders where the two faces are, and where we will save our training model. It will start hammering the training data once you run the command. I personally really like to go by the preview and quit the processing once I'm happy with the results.

python faceswap.py train -A ~/faceswap/data/trump -B ~/faceswap/data/cage -m ~/faceswap/models/
# or -p to show a preview python faceswap.py train -A ~/faceswap/data/trump -B ~/faceswap/data/cage -m ~/faceswap/models/ -p 

If you use the preview feature, select the preview window and press ENTER to save your processed data and quit gracefully. Without the preview enabled, you might have to forcefully quit by hitting Ctrl+C to cancel the command. Note that it will save the model once it's gone through about 100 iterations, which can take quite a while. So make sure you save before stopping the process.

You can see the full list of arguments for training via help flag. i.e.

python faceswap.py train -h


Now that we're happy with our trained model, we can convert our video. How does it work? Similarly to the extraction script, actually! The conversion script basically detects a face in a picture using the same algorithm, quickly crops the image to the right size, runs our bot on this cropped image of the face it has found, and then (crudely) pastes the processed face back into the picture.

Remember those initial pictures we had of Trump? Let's try swapping a face there. We will use that directory as our input directory, create a new folder where the output will be saved, and tell them which model to use.

python faceswap.py convert -i ~/faceswap/photo/trump/ -o ~/faceswap/output/ -m ~/faceswap/models/

It should now start swapping faces of all these pictures.

You can see the full list of arguments available for converting via help flag. i.e.

python faceswap.py convert -h


All of the above commands and options can be run from the GUI. This is launched with:

python faceswap.py gui


A video is just a series of pictures in the form of frames. Therefore you can gather the raw images from them for your dataset or combine your results into a video.


You can perform various video processes with the built in effmpeg tool. You can see the full list of arguments available by running:

python tools.py effmpeg -h

Extracting video frames with FFMPEG

Alternatively you can split a video into seperate frames using ffmpeg for instance. Below is an example command to process a video to seperate frames.

ffmpeg -i /path/to/my/video.mp4 /path/to/output/video-frame-%d.png

Generating a video

If you split a video, using ffmpeg for example, and used them as a target for swapping faces onto you can combine these frames again. The command below stitches the png frames back into a single video again.

ffmpeg -i video-frame-%0d.png -c:v libx264 -vf "fps=25,format=yuv420p" out.mp4


This guide is far from complete. Functionality may change over time, and new dependencies are added and removed as time goes on.

If you are experiencing issues, please raise them in the faceswap-playground repository instead of the main repo.

0 收藏
0 评论
0 收藏