Face Swap Workflow
A ComfyUI workflow for de-aging and/or face replacements that combines AI-driven masking, reference-based identity transfer, and inpainting to produce natural, temporally consistent results. Designed for VFX use, it allows artists to quickly swap or rejuvenate faces while preserving performance, lighting, and integration with the original plate.
简介
The Face Swap Workflow in ComfyUI is a sophisticated tool designed for visual effects artists who need to perform de-aging or face replacements in video footage. This workflow leverages a combination of AI-driven masking, identity transfer, and inpainting techniques to deliver natural and consistent results across frames. Key nodes such as WanVideoVAELoader and WanVideoModelLoader are used to load and manage video data, while nodes like DrawMaskOnImage and GrowMask help in creating precise masks for face swapping.
The workflow also incorporates advanced detection models through OnnxDetectionModelLoader and CLIPVisionLoader to accurately identify and manipulate facial features. By integrating these components, the workflow ensures that the new face blends seamlessly with the original footage, maintaining lighting and performance nuances.
你需要提供
- 正在读取工作流
在 ComfyUI 中运行。 此工作流无法在本页面运行。你可以在 Comfy Cloud 中打开(已预先加载所需内容),或下载后在自己的 ComfyUI 中使用。
在 Cloud 中试用试试一个示例
查看工作流内部
节点图的只读预览。在 Comfy Cloud 中打开或下载后即可编辑节点。
Loading the graph
Open full-size workflow preview- 作者
- ComfyUI
需要自定义节点
- 正在读取工作流
通过代码使用此工作流
尽可能根据工作流图和输入自动生成代码,否则显示已保存的示例。运行代码可能产生费用,请先核对凭据和端点。
What you need
- Runs on
- Comfy Cloud
- 密钥
- COMFY_API_KEY
需要付费的 Cloud 方案和额度。
# Python 3.10+: pip install comfy-sdk==0.4.0
import os
from comfy_sdk import Comfy
api_key = os.environ.get("COMFY_API_KEY", "").strip()
if not api_key: raise RuntimeError("Set COMFY_API_KEY")
os.environ.setdefault("COMFY_BASE_URL", "https://cloud.comfy.org")
client = Comfy(api_key=api_key)
workflow = client.workflows.from_json({
"38": {
"class_type": "WanVideoVAELoader",
"inputs": {
"model_name": "Wan2_1_VAE_fp32.safetensors",
"precision": "fp32",
"use_cpu_cache": False,
"verbose": False
},
"_meta": {
"title": "WanVideo VAE Loader"
}
},
"195": {
"class_type": "WanVideoTorchCompileSettings",
"inputs": {
"allow_unmerged_lora_compile": False,
"backend": "inductor",
"compile_transformer_blocks_only": True,
"dynamic": False,
"dynamo_cache_size_limit": 64,
"dynamo_recompile_limit": 128,
"force_parameter_static_shapes": False,
"fullgraph": False,
"mode": "default"
},
"_meta": {
"title": "WanVideo Torch Compile Settings"
}
},
"891": {
"class_type": "WanVideoLoraSelectMulti",
"inputs": {
"lora_0": "WanAnimate_relight_lora_fp16.safetensors",
"lora_1": "lightx2v_I2V_14B_480p_cfg_step_distill_rank64_bf16.safetensors",
"lora_2": "none",
"lora_3": "none",
"lora_4": "none",
"low_mem_load": False,
"merge_loras": False,
"strength_0": 1,
"strength_1": 1,
"strength_2": 1,
"strength_3": 1,
"strength_4": 1
},
"_meta": {
"title": "WanVideo Lora Select Multi"
}
},
"899": {
"class_type": "LoadImage",
"inputs": {
"image": "27e8b5aa2f2631d728cbaca117fe57ae2297a085a2d356d1a3f1438a7e8dadea.png"
},
"_meta": {
"title": "Load Image"
}
},
"900": {
"class_type": "ImageResizeKJv2",
"inputs": {
"crop_position": "top",
"device": "cpu",
"divisible_by": 16,
"height": [
"1099",
4
],
"image": [
"899",
0
],
"keep_proportion": "crop",
"pad_color": "0, 0, 0",
"upscale_method": "lanczos",
"width": [
"1099",
3
]
},
"_meta": {
"title": "Resize Image v2"
}
},
"911": {
"class_type": "VHS_VideoCombine",
"inputs": {
"crf": 19,
"filename_prefix": "WanVideo2_1_T2V",
"format": "video/h264-mp4",
"frame_rate": [
"1099",
0
],
"images": [
"912",
0
],
"loop_count": 0,
"pingpong": False,
"pix_fmt": "yuv420p",
"save_metadata": True,
"save_output": False,
"trim_to_audio": False
},
"_meta": {
"title": "Video Combine 🎥🅥🅗🅢"
}
},
"912": {
"class_type": "DrawMaskOnImage",
"inputs": {
"color": "0,0,0",
"device": "cpu",
"image": [
"1098",
0
],
"mask": [
"914",
0
]
},
"_meta": {
"title": "Draw Mask On Image"
}
},
"913": {
"class_type": "GrowMask",
"inputs": {
"expand": 300,
"mask": [
"1293",
0
],
"tapered_corners": False
},
"_meta": {
"title": "Grow Mask"
}
},
"914": {
"class_type": "BlockifyMask",
"inputs": {
"block_size": 8,
"device": "cpu",
"masks": [
"913",
0
]
},
"_meta": {
"title": "Blockify Mask"
}
},
"926": {
"class_type": "VHS_VideoCombine",
"inputs": {
"crf": 19,
"filename_prefix": "WanVideo2_1_T2V",
"format": "video/h264-mp4",
"frame_rate": [
"1099",
0
],
"images": [
"1036",
1
],
"loop_count": 0,
"pingpong": False,
"pix_fmt": "yuv420p",
"save_metadata": True,
"save_output": False,
"trim_to_audio": False
},
"_meta": {
"title": "Video Combine 🎥🅥🅗🅢"
}
},
"935": {
"class_type": "CLIPVisionLoader",
"inputs": {
"clip_name": "clip_vision_h.safetensors"
},
"_meta": {
"title": "Load CLIP Vision"
}
},
"936": {
"class_type": "WanVideoClipVisionEncode",
"inputs": {
"clip_vision": [
"935",
0
],
"combine_embeds": "average",
"crop": "center",
"force_offload": True,
"image_1": [
"900",
0
],
"ratio": 0.5,
"strength_1": 1,
"strength_2": 1,
"tiles": 0
},
"_meta": {
"title": "WanVideo ClipVision Encode"
}
},
"951": {
"class_type": "WanVideoAnimateEmbeds",
"inputs": {
"bg_images": [
"912",
0
],
"clip_embeds": [
"936",
0
],
"colormatch": "hm-mvgd-hm",
"face_images": [
"1036",
1
],
"face_strength": 1,
"force_offload": False,
"frame_window_size": [
"1099",
1
],
"height": [
"1099",
4
],
"mask": [
"914",
0
],
"num_frames": [
"1099",
1
],
"pose_images": [
"1035",
0
],
"pose_strength": 1,
"ref_images": [
"900",
0
],
"tiled_vae": False,
"vae": [
"38",
0
],
"width": [
"1099",
3
]
},
"_meta": {
"title": "WanVideo Animate Embeds"
}
},
"953": {
"class_type": "WanVideoSampler",
"inputs": {
"add_noise_to_samples": False,
"batched_cfg": False,
"cfg": 1,
"denoise_strength": 1,
"end_step": -1,
"force_offload": True,
"image_embeds": [
"951",
0
],
"model": [
"979",
0
],
"riflex_freq_index": 0,
"rope_function": "comfy",
"scheduler": "dpm++_sde/beta",
"seed": 65346457346,
"shift": 6,
"start_step": 0,
"steps": 8,
"text_embeds": [
"982",
0
]
},
"_meta": {
"title": "WanVideo Sampler"
}
},
"954": {
"class_type": "WanVideoDecode",
"inputs": {
"enable_vae_tiling": False,
"normalization": "default",
"samples": [
"953",
0
],
"tile_stride_x": 144,
"tile_stride_y": 128,
"tile_x": 272,
"tile_y": 272,
"vae": [
"38",
0
]
},
"_meta": {
"title": "WanVideo Decode"
}
},
"979": {
"class_type": "WanVideoModelLoader",
"inputs": {
"attention_mode": "sdpa",
"base_precision": "fp16_fast",
"compile_args": [
"195",
0
],
"load_device": "offload_device",
"lora": [
"891",
0
],
"model": "Wan2_2-Animate-14B_fp8_scaled_e4m3fn_KJ_v2.safetensors",
"quantization": "disabled",
"rms_norm_function": "default"
},
"_meta": {
"title": "WanVideo Model Loader"
}
},
"982": {
"class_type": "WanVideoTextEncodeCached",
"inputs": {
"device": "gpu",
"model_name": "umt5-xxl-enc-bf16.safetensors",
"negative_prompt": "色调艳丽,过曝,静态,细节模糊不清,字幕,风格,作品,画作,画面,静止,整体发灰,最差质量,低质量,JPEG压缩残留,丑陋的,残缺的,多余的手指,画得不好的手部,画得不好的脸部,畸形的,毁容的,形态畸形的肢体,手指融合,静止不动的画面,杂乱的背景,三条腿,背景人很多,倒着走",
"positive_prompt": "A young man with short, dark brown hair sits in a dimly lit room, his gaze steady on the camera. He is wearing an olive green button-up shirt that appears slightly worn but neat. His skin has a natural tan tone, and he looks calm yet thoughtful. He has no wrinkles and looks very youthful. Not old.",
"precision": "bf16",
"quantization": "disabled",
"use_disk_cache": False
},
"_meta": {
"title": "WanVideo TextEncode Cached"
}
},
"1034": {
"class_type": "OnnxDetectionModelLoader",
"inputs": {
"onnx_device": "CUDAExecutionProvider",
"vitpose_model": "vitpose-l-wholebody.onnx",
"yolo_model": "yolov10m.onnx"
},
"_meta": {
"title": "ONNX Detection Model Loader"
}
},
"1035": {
"class_type": "DrawViTPose",
"inputs": {
"body_stick_width": -1,
"draw_head": True,
"hand_stick_width": -1,
"height": [
"1099",
4
],
"pose_data": [
"1036",
0
],
"retarget_padding": 16,
"width": [
"1099",
3
]
},
"_meta": {
"title": "Draw ViT Pose"
}
},
"1036": {
"class_type": "PoseAndFaceDetection",
"inputs": {
"face_padding": 0,
"height": [
"1099",
4
],
"images": [
"1098",
0
],
"model": [
"1034",
0
],
"width": [
"1099",
3
]
},
"_meta": {
"title": "Pose and Face Detection"
}
},
"1037": {
"class_type": "VHS_VideoCombine",
"inputs": {
"crf": 19,
"filename_prefix": "WanVideo2_1_T2V",
"format": "video/h264-mp4",
"frame_rate": [
"1099",
0
],
"images": [
"1035",
0
],
"loop_count": 0,
"pingpong": False,
"pix_fmt": "yuv420p",
"save_metadata": True,
"save_output": False,
"trim_to_audio": False
},
"_meta": {
"title": "Video Combine 🎥🅥🅗🅢"
}
},
"1098": {
"class_type": "VHS_LoadVideoFFmpeg",
"inputs": {
"custom_height": [
"1288",
0
],
"custom_width": [
"1287",
0
],
"force_rate": 0,
"format": "Wan",
"frame_load_cap": 0,
"start_time": 0,
"video": "dacaa9337be45cb55ba8e804af52574ff18a96c7e20e40757558d116b54fe1e6.mp4"
},
"_meta": {
"title": "Load Video FFmpeg (Upload) 🎥🅥🅗🅢"
}
},
"1099": {
"class_type": "VHS_VideoInfoLoaded",
"inputs": {
"video_info": [
"1098",
3
]
},
"_meta": {
"title": "Video Info (Loaded) 🎥🅥🅗🅢"
}
},
"1201": {
"class_type": "AILab_QwenVL",
"inputs": {
"attention_mode": "sdpa",
"custom_prompt": "describe this image as if its a 5 second long video. Include a detail description of the person and their features and hair and age. ",
"image": [
"900",
0
],
"keep_model_loaded": False,
"max_tokens": 512,
"model_name": "Qwen3-VL-2B-Instruct",
"preset_prompt": "🖼️ Detailed Description",
"quantization": "None (FP16)",
"seed": 2074003516
},
"_meta": {
"title": "QwenVL"
}
},
"1287": {
"class_type": "INTConstant",
"inputs": {
"value": 1920
},
"_meta": {
"title": "Width"
}
},
"1288": {
"class_type": "INTConstant",
"inputs": {
"value": 1088
},
"_meta": {
"title": "Height"
}
},
"1289": {
"class_type": "PreviewAny",
"inputs": {
"source": [
"1201",
0
]
},
"_meta": {
"title": "Preview as Text"
}
},
"1290": {
"class_type": "DownloadAndLoadFlorence2Model",
"inputs": {
"attention": "sdpa",
"convert_to_safetensors": False,
"model": "microsoft/Florence-2-large",
"precision": "fp16"
},
"_meta": {
"title": "DownloadAndLoadFlorence2Model"
}
},
"1291": {
"class_type": "Florence2Run",
"inputs": {
"do_sample": True,
"fill_mask": True,
"florence2_model": [
"1290",
0
],
"image": [
"1098",
0
],
"keep_model_loaded": False,
"max_new_tokens": 1024,
"num_beams": 3,
"output_mask_select": "",
"seed": 3228786869,
"task": "caption_to_phrase_grounding",
"text_input": "Face, Head"
},
"_meta": {
"title": "Florence2Run"
}
},
"1292": {
"class_type": "Florence2toCoordinates",
"inputs": {
"batch": True,
"data": [
"1291",
3
],
"index": "0"
},
"_meta": {
"title": "Florence2 Coordinates"
}
},
"1293": {
"class_type": "Sam2Segmentation",
"inputs": {
"bboxes": [
"1292",
1
],
"image": [
"1295",
0
],
"individual_objects": True,
"keep_model_loaded": True,
"sam2_model": [
"1294",
0
]
},
"_meta": {
"title": "Sam2Segmentation"
}
},
"1294": {
"class_type": "DownloadAndLoadSAM2Model",
"inputs": {
"device": "cuda",
"model": "sam2_hiera_base_plus.safetensors",
"precision": "bf16",
"segmentor": "single_image"
},
"_meta": {
"title": "(Down)Load SAM2Model"
}
},
"1295": {
"class_type": "GetImageSizeAndCount",
"inputs": {
"image": [
"1098",
0
]
},
"_meta": {
"title": "Get Image Size & Count"
}
},
"1298": {
"class_type": "VHS_VideoCombine",
"inputs": {
"audio": [
"1098",
2
],
"crf": 22,
"filename_prefix": "FaceSwap",
"format": "video/h265-mp4",
"frame_rate": [
"1099",
0
],
"images": [
"954",
0
],
"loop_count": 0,
"pingpong": False,
"pix_fmt": "yuv420p10le",
"save_metadata": True,
"save_output": True
},
"_meta": {
"title": "Video Combine 🎥🅥🅗🅢"
}
}
})
# run() also passes api_key to the partner nodes in this workflow.
job = client.run(workflow, api_key=api_key)常见问答
The workflow supports common video formats such as MP4 and AVI. Ensure your footage is compatible with the WanVideoVAELoader node.
Use high-quality reference images and adjust the mask settings carefully. The GrowMask and BlockifyMask nodes can help refine the mask edges for a seamless blend.
Yes, the workflow can process multiple faces. Use the OnnxDetectionModelLoader to detect and manage each face individually.
The Face Swap Workflow is optimized for post-production rather than real-time applications due to its computational requirements.