This is an open-source face swapping project based on roop. It utilizes the inswapper_128.onnx model for core face swapping operations and integrates the powerful GFPGANv1.4 model to enhance the details and clarity of the generated faces.
The entire application is containerized using Cog to ensure a consistent and reproducible environment. It can be deployed to Replicate with a single command to run as a stable and scalable API service.
From left to right: Source Face Image, Target Image, Swapped Image
- Accurate Face Swapping: Employs the
inswapper_128.onnxmodel frominsightfacefor precise face detection and replacement. - Quality Enhancement: Integrates
GFPGANv1.4, an advanced face restoration algorithm, to significantly improve the clarity and realism of the swapped face, fixing artifacts and blurriness. - Containerized with Cog: Packaged with
cog, which freezes all complex dependencies (CUDA, Python libraries, system packages) into a container, eliminating configuration hassles. - One-Click Deployment: Perfectly compatible with the
Replicateplatform, allowing you to deploy the model as a production-grade API service with a single command, without worrying about server maintenance.
This project and its source code are intended for academic research and technical study only.
This project is developed based on roop and therefore inherits its strict Non-Commercial license. It is strictly forbidden to use this project or any content generated by it for commercial purposes. This includes, but is not limited to, selling, integrating into paid products, or using it in commercial advertising or marketing materials.
Users must assume all ethical and legal responsibilities when using this technology. It is strictly forbidden to use this tool to create any form of pornographic, violent, defamatory, privacy-invading, or deceptive content. The developers are not responsible for any negative consequences resulting from the misuse of this tool.
| Component | Role | Description |
|---|---|---|
roop |
Core Framework | Provides the fundamental logic and workflow for face swapping. |
insightface |
Swapping Engine | Uses its inswapper_128.onnx model, a well-regarded high-performance face analysis library. |
GFPGAN |
Quality Enhancer | Uses the GFPGANv1.4.pth model, a powerful blind face restoration model, to improve the output quality. |
Cog |
Model Packaging | A tool that packages machine learning models into standard, portable containers, greatly simplifying deployment. |
Replicate |
Cloud Deployment | A cloud platform that allows developers to easily run and share machine learning models. |
Before you begin, ensure your system has the following software installed:
-
NVIDIA GPU Drivers
-
Docker: Cog relies on Docker to build and run containers.
-
Cog: The official model packaging tool from Replicate.
# Linux / WSL sudo curl -o /usr/local/bin/cog -L "[https://github.com/replicate/cog/releases/latest/download/cog_$(uname](https://github.com/replicate/cog/releases/latest/download/cog_$(uname) -s)_$(uname -m)" sudo chmod +x /usr/local/bin/cog # macOS (Homebrew) brew install cog
Clone this repository to your local machine.
git clone [https://github.com/VmodelAI/face-swap.git](https://github.com/VmodelAI/face-swap.git)
cd face-swapYou do not need to manually download any model files. The model_manager.py script in the project will automatically check for the existence of inswapper_128.onnx and GFPGANv1.4.pth on the first run. If they are missing, it will download them from the internet to the project's root directory.
cog.yaml is the project's configuration file. It tells Cog how to build the runtime environment.
build:
gpu: true
cuda: "11.8"
python_version: "3.9"
python_packages:
- "opencv-contrib-python==4.7.0.72"
- "ftfy==6.1.1"
- "scipy==1.9.3"
- "Pillow==9.4.0"
- "mediapipe"
- "numpy==1.24.3"
- "opencv-python==4.7.0.72"
- "onnx==1.14.0"
- "insightface==0.7.3"
- "psutil==5.9.5"
- "tensorflow>=2.0.0"
- "opennsfw2==0.10.2"
- "gfpgan==1.3.8"
- "realesrgan"
- "torchvision==0.12.0"
- "onnxruntime-gpu==1.15.0"
system_packages:
- ffmpeg
- libsm6
- libxext6
predict: "predict.py:Predictor"Use the following command to run a complete face swap prediction locally. Cog will handle everything inside a Docker container.
cog predict \
-i source="[https://vmodel.ai/data/model/vmodel/photo-face-swap-pro/target_image.png](https://vmodel.ai/data/model/vmodel/photo-face-swap-pro/target_image.png)" \
-i target="[https://data.vmodel.ai/data/model-example/vmodel/photo-face-swap-pro/swap_image.png](https://data.vmodel.ai/data/model-example/vmodel/photo-face-swap-pro/swap_image.png)" \
-i is_enhancer="True"-i source: URL of the image providing the source face.-i target: URL of the target image to have its face replaced.-i is_enhancer: Whether to enableGFPGANfor quality enhancement. Accepts"True"or"False".
Upon successful prediction, the resulting image will be saved as output.png in the current directory by default.
Deploying your model to Replicate is straightforward and gives you a public API with just a few commands.
-
Log in to Replicate
cog login
(Paste your Replicate API Token when prompted)
-
Push the Model Push the model to the repository you created on Replicate. Replace
vmodelai/face-swapwith your own username and model name.cog push r8.im/vmodelai/face-swap
Cog will automatically build the image and push it to Replicate. Once completed, your model will be available online!
To help users make an informed choice, we are comparing this open-source project with our other commercial-grade model, Face Swap Pro.
| Feature | Face Swap (This Project) | Face Swap Pro (Commercial) |
|---|---|---|
| Core Technology | Open-source solution based on roop |
Proprietary, advanced commercial algorithm |
| Effect Quality | Good, enhanced with GFPGAN | Superior. Photo-realistic results with richer details and fewer artifacts. |
| Execution Speed | Moderate | Faster. Optimized for commercial applications with significantly higher inference speed. |
| Use Case | Academic research, technical learning, personal projects | Commercial product integration, large-scale applications, professional content creation. |
| License | Strictly Non-Commercial | Commercial License, available for use in for-profit products. |
Conclusion: This project (face-swap) is an excellent tool for learning and research. If you require higher quality, faster speed, and plan for commercial use, we strongly recommend using Face Swap Pro.
The creation of this project would not have been possible without the support of the following outstanding open-source communities and projects:
- roop: Provided the foundation for the project.
- insightface: Provided the powerful face analysis models.
- TencentARC (GFPGAN): Provided the excellent face restoration algorithm.
- Replicate (Cog): Provided the convenient model deployment tool.