Build a face swap demo based on insightface, gfpgan and streamlit.
This demo is tested on Ubuntu 22.04.2 with Python 3.10.12
DISCLAIMER: all the images in the static
folder are from Google image search. Those are only used for testing purposes.
This demo depends on multiple models.
Model Name | Description | Location |
---|---|---|
inswapper_128.onnx | insightface face swapper model | https://huggingface.co/deepinsight/inswapper/resolve/main/inswapper_128.onnx |
GFPGANv1.4.pth | GFPGAN face restore models | https://github.com/TencentARC/GFPGAN/releases/download/v1.3.4/GFPGANv1.4.pth |
detection_Resnet50_Final.pth | GFPGAN uses this model for face detection internally | Downloaded by GFPGAN at runtime |
parsing_parsenet.pth | GFPGAN uses this model internally | Downloaded by GFPGAN at runtime |
buffalo_l.zip | Face analysis | https://github.com/deepinsight/insightface/releases/download/v0.7/buffalo_l.zip |
Please:
- Download and save the
inswapper_128.onnx
andGFPGANv1.4.pth
to the./models
folder, referred to in the demo. detection_Resnet50_Final.pth
andparsing_parsenet.pth
will be automatically downloaded to./gfpgan/weights
folder when you run the demo.buffalo_l.zip
will be automatically downloaded to the~/.insightface/models
folder when you run the demo.
$ mkdir ./models
$ cd ./models
# download inswapper_128.onnx and GFPGANv1.4.pth
$ wget https://huggingface.co/deepinsight/inswapper/resolve/main/inswapper_128.onnx
$ wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.4/GFPGANv1.4.pth
We use pyenv + poetry to create a virtual environment to run the demo.
Assume pyenv and poetry have been installed on your machine.
# Before you use pyenv to install python 3.10, you must ensure some dependencies are installed on your Ubuntu
$ sudo apt update && sudo apt install lzma liblzma-dev libbz2-dev
# If you see any warning like 'WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib?', please apt install the related dependencies and try again.
$ pyenv install 3.10
# set your faceoff folder with Python 3.10
$ cd /path/to/faceoff_folder
$ pyenv local 3.10
# poetry will use Python 3.10 to set up your virtual environment
$ poetry install
# Start it
$ poetry run streamlit run app.py
If you want to remove this environment, you should do:
# Find your env name
$ poetry env list
faceoff-LR4bX88f-py3.10
# Delete it
$ poetry env remove faceoff-LR4bX88f-py3.10
You can also build everything into a docker image and run it everywhere. Before you build the image, you need to ensure all the models are ready locally because we will copy the models into the image for app launch acceleration. You can see copy to docker
instruction in Dockerfile
like
COPY ./models/buffalo_l /root/.insightface/models/buffalo_l
COPY ./models/*.pth ./models
COPY ./models/*.onnx ./models
COPY ./gfpgan /workdir/gfpgan
So please ensure all the models at the positions. Especially:
- Run the demo locally once to trigger downloading GFPGAN and insightface face analyzer models to your local folder.
- Copy
~/.insightface/models
(insightface downloads its models here originally) to./models
.
# Build it!
$ chmod +x ./scripts/build/dev.sh
$ ./scripts/build/dev.sh
# Run it!
$ ./scripts/run/dev.sh