人臉置換
最佳即時 AI 人臉置換技術,適用於照片。
工作流程內部
來源範本的唯讀預覽。請在 Cloud 中開啟,以探索節點並進行變更。
Loading the graph
Open full-size workflow preview從您的程式碼呼叫此模型
以下請求使用與 Playground 相同的已準備節點圖和設定。每次提交都可能啟動付費執行;若提交結果不明,請勿自動重試提交。
What you need
- Runs on
- Comfy Cloud
- 金鑰
- COMFY_API_KEY
- 您的檔案
- 由程式碼透過其 URL 上傳
需要付費 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({
"76": {
"class_type": "LoadImage",
"inputs": {
"image": "UPLOADED_base_image_FILENAME"
},
"_meta": {
"title": "Load Target Image"
}
},
"81": {
"class_type": "LoadImage",
"inputs": {
"image": "UPLOADED_head_image_FILENAME"
},
"_meta": {
"title": "Load Source Image"
}
},
"190": {
"class_type": "ImageCompare",
"inputs": {
"compare_view": {
"__value__": [
"",
""
]
},
"image_a": [
"204",
0
],
"image_b": [
"199:120",
0
]
},
"_meta": {
"title": "Compare Images"
}
},
"204": {
"class_type": "SaveImageAdvanced",
"inputs": {
"filename_prefix": "face_swap",
"format": "png",
"format.bit_depth": "8-bit",
"format.input_color_space": "sRGB",
"images": [
"199:131",
0
]
},
"_meta": {
"title": "Save Image (Advanced)"
}
},
"205": {
"class_type": "SaveImageAdvanced",
"inputs": {
"filename_prefix": "face_swap_stitched",
"format": "png",
"format.bit_depth": "8-bit",
"format.input_color_space": "sRGB",
"images": [
"199:177",
0
]
},
"_meta": {
"title": "Save Image (Advanced)"
}
},
"199:109": {
"class_type": "SamplerCustomAdvanced",
"inputs": {
"guider": [
"199:123",
0
],
"latent_image": [
"199:113",
0
],
"noise": [
"199:110",
0
],
"sampler": [
"199:125",
0
],
"sigmas": [
"199:122",
0
]
},
"_meta": {
"title": "SamplerCustomAdvanced"
}
},
"199:110": {
"class_type": "RandomNoise",
"inputs": {
"noise_seed": 788122026421577
},
"_meta": {
"title": "RandomNoise"
}
},
"199:111": {
"class_type": "VAELoader",
"inputs": {
"vae_name": "flux2-vae.safetensors"
},
"_meta": {
"title": "Load VAE"
}
},
"199:112": {
"class_type": "GetImageSize",
"inputs": {
"image": [
"199:120",
0
]
},
"_meta": {
"title": "Get Image Size"
}
},
"199:113": {
"class_type": "EmptyFlux2LatentImage",
"inputs": {
"batch_size": 1,
"height": [
"199:112",
1
],
"width": [
"199:112",
0
]
},
"_meta": {
"title": "Empty Flux 2 Latent"
}
},
"199:114:179": {
"class_type": "ReferenceLatent",
"inputs": {
"conditioning": [
"199:116:180",
0
],
"latent": [
"199:114:78",
0
]
},
"_meta": {
"title": "Set Reference Latent"
}
},
"199:114:77": {
"class_type": "ReferenceLatent",
"inputs": {
"conditioning": [
"199:116:182",
0
],
"latent": [
"199:114:78",
0
]
},
"_meta": {
"title": "Set Reference Latent"
}
},
"199:114:78": {
"class_type": "VAEEncode",
"inputs": {
"pixels": [
"199:121",
0
],
"vae": [
"199:111",
0
]
},
"_meta": {
"title": "VAE Encode"
}
},
"199:115": {
"class_type": "CLIPLoader",
"inputs": {
"clip_name": "qwen_3_8b_fp8mixed.safetensors",
"device": "default",
"type": "flux2"
},
"_meta": {
"title": "Load CLIP"
}
},
"199:116:180": {
"class_type": "ReferenceLatent",
"inputs": {
"conditioning": [
"199:119",
0
],
"latent": [
"199:116:181",
0
]
},
"_meta": {
"title": "Set Reference Latent"
}
},
"199:116:181": {
"class_type": "VAEEncode",
"inputs": {
"pixels": [
"199:120",
0
],
"vae": [
"199:111",
0
]
},
"_meta": {
"title": "VAE Encode"
}
},
"199:116:182": {
"class_type": "ReferenceLatent",
"inputs": {
"conditioning": [
"199:117",
0
],
"latent": [
"199:116:181",
0
]
},
"_meta": {
"title": "Set Reference Latent"
}
},
"199:117": {
"class_type": "CLIPTextEncode",
"inputs": {
"clip": [
"199:115",
0
],
"text": [
"199:198",
0
]
},
"_meta": {
"title": "CLIP Text Encode (Positive Prompt)"
}
},
"199:118": {
"class_type": "UNETLoader",
"inputs": {
"unet_name": "flux-2-klein-9b.safetensors",
"weight_dtype": "default"
},
"_meta": {
"title": "Load Diffusion Model"
}
},
"199:119": {
"class_type": "CLIPTextEncode",
"inputs": {
"clip": [
"199:115",
0
],
"text": "bad quality, noise, blurry, worst quality, low resolution, blur, distortion, unnatural blending, cartoon, illustration, painting"
},
"_meta": {
"title": "CLIP Text Encode ( Negative Prompt)"
}
},
"199:120": {
"class_type": "ResizeImageMaskNode",
"inputs": {
"input": [
"76",
0
],
"resize_type": "scale total pixels",
"resize_type.megapixels": 1,
"scale_method": "lanczos"
},
"_meta": {
"title": "Resize Image/Mask"
}
},
"199:121": {
"class_type": "ResizeImageMaskNode",
"inputs": {
"input": [
"81",
0
],
"resize_type": "scale total pixels",
"resize_type.megapixels": 1,
"scale_method": "lanczos"
},
"_meta": {
"title": "Resize Image/Mask"
}
},
"199:122": {
"class_type": "Flux2Scheduler",
"inputs": {
"height": [
"199:112",
1
],
"steps": 4,
"width": [
"199:112",
0
]
},
"_meta": {
"title": "Flux2Scheduler"
}
},
"199:123": {
"class_type": "CFGGuider",
"inputs": {
"cfg": 1,
"model": [
"199:133",
0
],
"negative": [
"199:114:179",
0
],
"positive": [
"199:114:77",
0
]
},
"_meta": {
"title": "CFG Guider"
}
},
"199:125": {
"class_type": "KSamplerSelect",
"inputs": {
"sampler_name": "lcm"
},
"_meta": {
"title": "KSamplerSelect"
}
},
"199:131": {
"class_type": "VAEDecode",
"inputs": {
"samples": [
"199:109",
0
],
"vae": [
"199:111",
0
]
},
"_meta": {
"title": "VAE Decode"
}
},
"199:133": {
"class_type": "LoraLoaderModelOnly",
"inputs": {
"lora_name": "Alissonerdx__BFS-Best-Face-Swap__bfs_head_v1_flux-klein_9b_step3750_rank64.safetensors",
"model": [
"199:118",
0
],
"strength_model": 1
},
"_meta": {
"title": "Load LoRA"
}
},
"199:171": {
"class_type": "StringConcatenate",
"inputs": {
"delimiter": "",
"string_a": [
"199:197",
0
],
"string_b": [
"199:184",
0
]
},
"_meta": {
"title": "Concatenate Prompt"
}
},
"199:176": {
"class_type": "ImageConcanate",
"inputs": {
"direction": "down",
"image1": [
"199:120",
0
],
"image2": [
"199:121",
0
],
"match_image_size": True
},
"_meta": {
"title": "ImageConcanate"
}
},
"199:177": {
"class_type": "ImageConcanate",
"inputs": {
"direction": "right",
"image1": [
"199:176",
0
],
"image2": [
"199:131",
0
],
"match_image_size": True
},
"_meta": {
"title": "ImageConcanate"
}
},
"199:184": {
"class_type": "TextGenerate",
"inputs": {
"clip": [
"199:185",
0
],
"image": [
"199:120",
0
],
"max_length": 256,
"mtp": "auto",
"prompt": "Describe the facial expression. Do not mention facial details such as freckles or unique marks. Do not output <think> tags.\n\nAn example output is as follows:\n<example>\nKey features include:\n- A wide, open-mouthed grin with their tongue sticking out playfully.\n- One eye is winking (the right eye), adding to the mischievous or fun vibe.\n- Their lips are parted in an exaggerated smile that complements the tongue-out gesture.\n- The overall look conveys confidence, joy, and a sense of youthful rebellion — perfectly matching the vibrant, colorful aesthetic they’re presenting.\n</<example>",
"sampling_mode": "on",
"sampling_mode.min_p": 0.05,
"sampling_mode.presence_penalty": 0,
"sampling_mode.repetition_penalty": 1.05,
"sampling_mode.seed": 1,
"sampling_mode.temperature": 0.7,
"sampling_mode.top_k": 64,
"sampling_mode.top_p": 0.95,
"thinking": False,
"use_default_template": True
},
"_meta": {
"title": "Generate Text"
}
},
"199:185": {
"class_type": "CLIPLoader",
"inputs": {
"clip_name": "qwen3.5_9b_bf16.safetensors",
"device": "default",
"type": "qwen_image"
},
"_meta": {
"title": "Load CLIP"
}
},
"199:195": {
"class_type": "ComfySwitchNode",
"inputs": {
"on_false": [
"199:197",
0
],
"on_true": [
"199:171",
0
],
"switch": True
},
"_meta": {
"title": "If/Else Switch"
}
},
"199:197": {
"class_type": "PrimitiveStringMultiline",
"inputs": {
"value": "head_swap: start with Picture 1 as the base image, keeping its lighting, environment, and background. remove the head from Picture 1 completely and replace it with the head from Picture 2, strictly preserving the hair, eye color, nose structure of Picture 2. copy the direction of the eye, head rotation, micro expressions from Picture 1, high quality, sharp details, 4k\n"
},
"_meta": {
"title": "Prompt Base"
}
},
"199:198": {
"class_type": "PreviewAny",
"inputs": {
"source": [
"199:195",
0
]
},
"_meta": {
"title": "Preview as Text"
}
}
})
workflow.set_input("76", "image", client.assets.from_url("https://comfy.org/catalog-media/8fe73d3d-d1b4-441f-868f-de7c39283e4b/e7eb0cc0-e6b4-435e-be2f-9b834c6d4fb9"))
workflow.set_input("81", "image", client.assets.from_url("https://comfy.org/catalog-media/24509314-ef4a-4dee-af38-c8602e8f38dc/6b898242-c7e9-4f34-9ac3-b0e9abd51ff2"))
# run() also passes api_key to the partner nodes in this workflow.
job = client.run(workflow, api_key=api_key)
for output in job.get_outputs("204"):
output.to_file(output.name)
for output in job.get_outputs("205"):
output.to_file(output.name)試試範例
關於此工作流程
此 ComfyUI 工作流程利用簡潔、易調整的圖形流程,為照片進行快速高品質人臉置換。兩個 LoadImage 節點分別載入來源臉(欲轉移的身份)與目標照片。自訂人臉置換處理器(節點 ID:217f3726-59bc-4170-9ab1-c622bb578338)會偵測並對齊目標臉部,將來源身份轉移至目標,同時保留表情、姿勢與光線,並輸出合成圖像。ImageCompare 節點可視覺比對原圖與結果,SaveImageAdvanced 負責乾淨、可重複的匯出。
技術上,人臉置換節點整合了臉部偵測、對齊/變形、身份轉移與融合,無需額外偵測或遮罩節點。流程簡潔穩定:載入圖像、可選擇調整置換節點設定(如多臉照片選擇要置換哪一臉、融合強度等),用 ImageCompare 預覽,最後用 SaveImageAdvanced 以一致命名與格式匯出。特別適合需從高品質輸入照片進行可靠置換、又不想建立複雜流程時使用。
問答
使用清晰、光線充足、臉部明確且無遮擋的照片。正面或輕微 3/4 側臉效果最好。嚴重遮擋(手、頭髮、口罩)、極端側面或動態模糊都可能讓結果變差。
開啟換臉節點(217f3726-59bc-4170-9ab1-c622bb578338)。若提供臉部選擇控制,選擇你要替換的 index/臉。若沒有,請先將目標影像裁切到預期臉部周圍後重跑。
來源與目標之間的光線或色彩差異太大時,融合會顯得明顯。請改用光線與色調更接近的來源照片,或先做基本的色彩/對比調整。若有提供,也可微調換臉節點的混合/羽化以改善。
不會。此工作流程會產生一張新的換臉影像。SaveImageAdvanced 可用你指定的格式、品質與檔名樣式進行非破壞式匯出。