Implementation of a/faceswap based on a/InstantID for ComfyUI.
<sub>About | Installation guide | Custom nodes | Workflows | Tips | Changelog</sub>
Implementation of faceswap based on InstantID for ComfyUI.
Since version 0.1.0 it also allows generating people based on text.
</br>
Works ONLY with SDXL checkpoints
</br>
</br>
<sub>About | Installation guide | Custom nodes | Workflows | Tips | Changelog</sub>
pip install -r requirements.txt
to install dependenciesantelopev2 models and put them into ComfyUI/models/insightface/models/antelopev2 folder
Newly added files hierarchy should look like this:
ComfyUI
\---models
\---ipadapter
ipadapter.bin
\---controlnet
\--- FOLDER_YOU_CREATED
config.json
diffusion_pytorch_model.safetensors
\---insightface
\---models
\antelopev2
1k3d68.onnx
2d106det.onnx
genderage.onnx
glintr100.onnx
scrfd_10g_bnkps.onnx
*Note You don't need to add the 'ipadapter', and 'controlnet' folders to this specific location if you already have them somewhere else (also you can rename ipadapter.bin and ControlNetModel to something of your liking).
Instead, You can edit ComfyUI/extra_model_paths.yaml
and add folders containing those files to the config.
<sub>About | Installation guide | Custom nodes | Workflows | Tips | Changelog</sub>
Loads Insightface. Models need to be in a specific location. Check the Installation guide for details.
Loads the InstantId adapter and resampler. The model needs to be in a specific location. Check the Installation guide for details. The resampler is used to prepare face embeds for ControlNet and the adapter.
Applies the InstantId adapter to the model. This is optional—you can achieve good results without using this node.
Params:
Applies InstantId ControlNet.
Params:
A subgraph node that bundles several operations into a single node for convenience. It includes the following nodes: LoadInstantIdAdapter, FaceEmbedCombine, ControlNetLoader, InstantIdAdapterApply, and ControlNetInstantIdApply.
This node streamlines the process by loading the InstantId adapter, combining face embeddings, loading the ControlNet, and applying both the InstantId adapter and ControlNet in one step.
Prepares face embeds for generation. You can chain multiple face embeds.
Params:
Prepares face embeds for ControlNet and the adapter.
Params:
Returns the angle (in degrees) by which the image must be rotated counterclockwise to align the face. Since there can be more than one face in the image, face search is performed only in the area of the drawn mask, enlarged by the pad parameter.
Note: If the face is rotated by an extreme angle, insightface won't be able to find the correct position of face keypoints, so the rotation angle might not always be accurate. In these cases, manually draw your own KPS.
Params:
Returns the angle (in degrees) by which the image must be rotated counterclockwise to align the face.
Params:
Rotates the image by the given angle and expands it.
Params:
Removes the expanded region added by two rotations (first to align the face, and second to return to the original position).
Params:
Allows you to draw your own keypoints (KPS), useful when you get the error "No face detected in pose image"
or when using InstantId to generate images from prompts only. Click and drag the KPS to move them around.
When you place your KPS in the desired position, this node will show the angle by which the image should be rotated to align the face.
You can adjust the opacity of each keypoint to sacrifice likeness, for example, when adding "glasses".
Shortcuts:
CTRL + DRAG - move around
CTRL + WHEEL - zoom in / out
ALT + WHEEL - decrease / increase distance of other points from blue point (nose kps)
Params:
image_reference
is provided)image_reference
is provided)Allows you to extract 3D keypoints (KPS) of a face from an image. This is useful when generating content from text prompts and wanting to rotate the face while preserving the distance between the eyes, nose, and mouth.
To use it, connect the image
node, then click the "Get KPS From Image" button. Afterward, you can adjust the scale, position, and rotation of the face by clicking the "Change KPS" button.
IMPORTANT: - Clicking Get KPS From Image will run InsightFace to extract KPS data, so it’s best not to use this in the middle of the generation process (this depends on your system's performance). - You cannot manually change the distance between KPS in this node.
Once your KPS are placed in the desired position, the node will show the angle by which the image should be rotated to align the face.
You can adjust the opacity of each keypoint to sacrifice likeness, for example, when adding "glasses".
Shortcuts:
CTRL + DRAG - move around
CTRL + WHEEL - zoom in / out
ALT + WHEEL - scale KPS
Params:
Cuts out the mask area wrapped in a square, enlarges it in each direction by the pad
parameter, and resizes it (to dimensions rounded down to multiples of 8). It also creates a control image for InstantId ControlNet.
Note: If the face is rotated by an extreme angle, the prepared control_image
may be drawn incorrectly.
If the insightface
param is not provided, it will not create a control image, and you can use this node as a regular node for inpainting (to cut the masked region with padding and later compose it).
Params:
resize_mode
)resize_mode
width
x height
.
For SDXL, you probably want to use this option with:
width: 1024, height: 1024height
and calculates it based on the aspect ratiowidth
and calculates it based on the aspect ratiowidth
and height
Same as Preprocess Image for InstantId with five additional parameters.
Params:
Randomizes the position, angle, and rotation of the KPS based on the provided parameters.
Params:
Randomizes the rotation of the KPS around three axes. Setting any of the parameters will randomly select a rotation angle around the corresponding axis. The rotation point is the center of the KPS. Example: Setting rotate_x to 20 will rotate the KPS by a random angle between -20 and 20 degrees.
Params:
Scales the KPS data by a given factor.
Params:
Scales the KPS data to the specified width and height.
Params:
Rotates the KPS by the given angle and expands it.
Params:
Crops the KPS.
Params:
Creates a control_image from kps_data
Creates a mask based on the KPS position.
Params:
<sub>About | Installation guide | Custom nodes | Workflows | Tips | Changelog</sub>
You can find example workflows in the /workflows
folder.
Nodes colors legend:
yellow - node from this extension,
blue - inputs, load your controlnets, models, images ...
purple - you might want to configure those
cyan - output images
green - positive prompts
red - negative prompts
If you set the mask blur options remember that it will shrink the area you masked
Face swap: Set your pose image, draw a mask, set your face reference (the face that will replace the masked area in the pose image), and that's it.
Same as simple.json
with an additional node Apply instantId adapter
Same as simple.json
, but allows you to provide two face references. You can use this to merge two different faces or just provide a second reference for the first face.
Face swap: Set your pose image, draw a mask, set your face reference (the face that will replace the masked area in the pose image), and then click the "draw KPS" button on the Draw KPS
node to set your KPS.
Same as draw_kps.json
, but it will also rotate the pose image. After setting your KPS, you should set the angle by which you want to rotate the image to align the face properly.
Same as simple_with_adapter.json
, but it will automatically detect the angle of rotation based on the mask and padding set in the Get Angle from Face
node.
Generates an image based only on the face reference and prompts. Set your face reference, draw the KPS where the face should be drawn, and add prompts like "man sitting in the park."
Same as prompt2image.json
, but this one expects the KPS you draw to be small, so the face is not detailed (or may even be deformed). Second pass should fix the face.
A workflow that generates two faces in one image and enhances them one by one. Set your face references and KPS for one image, then set a second KPS in another region of the picture. Good results depend on your prompts.
<details> <summary>View Example Image</summary>Since you can use the Preprocess Image for InstantId
and Preprocess Image for InstantId (Advanced)
nodes to resize your images with a mask, this workflow is useful for inpainting in general. This workflow shows you how to do it.
Similar to promp2image_detail_pass.json
, this workflow allows you to draw your KPS. The workflow will run a first pass at 50%, upscale the latent by 1.4
, and finish with a detail pass on the face area.
Same as prop2image_latent_upscale.json
, but it will randomize the position of the face within the image.
Similar to prop2image_latent_upscale_with_2d_randomizer.json
, but instead of drawing your KPS, you retrieve them from a face image (in 3d).
Click the "Get KPS from image" button on the 3D KPS from Image node, then use the "Change KPS" button to adjust the position of your KPS. You can also randomize the 3D rotation in this workflow.
Exactly the same as prop2image_latent_upscale_with_3d_and_2d_randomizer.json
, with one small addition: you can set the face rotation method during the last pass. The workflow will rotate the face to a "straight" position, process the image, and then composite it into the final result.
The default rotation is set to "any," so you might encounter some artifacts from the rotation. You can adjust this setting as needed.
<sub>About | Installation guide | Custom nodes | Workflows | Tips | Changelog</sub>
No face detected in pose image
, try drawing a larger mask or increasing the pad
parameter or draw KPS yourself.<sub>About | Installation guide | Custom nodes | Workflows | Tips | Changelog</sub>
ControlNet Scale
but for specific parts of the face (e.g., adding glasses).Note: Some old workflows will not be compatible with this version.
Note: Old workflows will not work with this version.
mask_strength
parameter has been fixed; it now functions correctly. Previously, it was stuck at 0.9999 regardless of the chosen value.ip_adapter_scale
parameter has been fixed. If you were using the xformers, this parameter could be stuck at 50.rotate_face
parameter. It will attempt to rotate the image to keep the face straight before processing and rotate it back to the original position afterward.--fp16-vae
argument to disable the default VAE upcasting to float32.resize
and resize_to
options into just resize
for the Faceswap generate node. To emulate the old behavior where resize was unchecked, select don't
.mask_strength
to range from 0.00 to 1.00.pip install -r requirements.txt
or
pip install -U diffusers~=0.26.0