inswapper Documentation

repository·main·Indexed 18 days ago

https://github.com/haofanwang/inswapper

A one-click face swapper and restoration tool powered by insightface. It supports swapping faces from source images onto target images with optional high-quality restoration using CodeFormer, background enhancement, and upscaling via a Python API or CLI.

Tokens
926
Snippets
4
Records
4
Agent score
23%

What's inside inswapper

  1. Download required checkpoints

    main

    The face swap model must be placed in a ./checkpoints directory. For high-quality results, it is recommended to use CodeFormer for face restoration. The required models for CodeFormer will be downloaded automatically during the first inference run if you clone the repository as shown below.

    mkdir checkpoints
    wget -O ./checkpoints/inswapper_128.onnx https://github.com/facefusion/facefusion-assets/releases/download/models/inswapper_128.onnx
    
    cd ..
    git lfs install
    git clone https://huggingface.co/spaces/sczhou/CodeFormer
  2. Install inswapper

    main

    To set up the environment, clone the repository, create a Python virtual environment, and install the required dependencies.

    Note on Hardware Acceleration:

    • For CPU-only inference: The default installation via requirements.txt will use onnxruntime.
    • For GPU inference: You must manually install onnxruntime-gpu after following the standard installation steps.
    # git clone this repository
    git clone https://github.com/haofanwang/inswapper.git
    cd inswapper
    
    # create a Python venv
    python3 -m venv venv
    
    # activate the venv
    source venv/bin/activate
    
    # install required packages
    pip install -r requirements.txt
  3. Perform quick face swapping via Python API

    main

    You can use the process function from the swapper module to perform face swapping.

    Parameters:

    • source_img: A list of PIL Image objects representing the source faces.
    • target_img: A PIL Image object representing the target image.
    • model: Path to the ./checkpoints/inswapper_128.onnx model file.
    • Additional integer parameters (e.g., -1, -1) are used for internal processing logic.
    from swapper import *
    
    source_img = [Image.open("./data/man1.jpeg"), Image.open("./data/man2.jpeg")]
    target_img = Image.open("./data/mans1.jpeg")
    
    model = "./checkpoints/inswapper_128.onnx"
    result_image = process(source_img, target_img, -1, -1, model)
    result_image.save("result.png")
  4. Run face swapping via CLI with restoration and enhancement

    main

    The CLI allows for advanced face swapping including restoration, background enhancement, and upscaling.

    CLI Arguments:

    • --source_img: Semicolon-separated list of paths to source images (e.g., ./path1.jpg;./path2.jpg).
    • --target_img: Path to the target image.
    • --face_restore: Enables face restoration (using CodeFormer).
    • --background_enhance: Enables background enhancement.
    • --face_upsample: Enables face upsampling.
    • --upscale: Scaling factor for upsampling (e.g., 2).
    • --codeformer_fidelity: Fidelity setting for CodeFormer (e.g., 0.5).
    python swapper.py \
    --source_img="./data/man1.jpeg;./data/man2.jpeg" \
    --target_img "./data/mans1.jpeg" \
    --face_restore \
    --background_enhance \
    --face_upsample \
    --upscale=2 \
    --codeformer_fidelity=0.5