تبديل الوجه
أفضل تقنية تبديل وجه فوري بالذكاء الاصطناعي للصور.
داخل سير العمل
معاينة للقراءة فقط للقالب المصدر. افتحه في Cloud لاستكشاف العُقد وإجراء التغييرات.
Loading the graph
Open full-size workflow previewالملفات في سير العمل هذا
- المخرج
- صورة
- المؤلف
- Rob
استدعِ هذا النموذج من التعليمة البرمجية الخاصة بك
يستخدم الطلب أدناه المخطط والإعدادات المُعدّة نفسها كما في 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 بإدخال وجه المصدر (الهوية التي تريد نقلها) وصورة الهدف. يقوم معالج تبديل الوجه المخصص (معرّف العقدة: 217f3726-59bc-4170-9ab1-c622bb578338) باكتشاف الوجوه ومحاذاتها في الهدف، ثم ينقل هوية المصدر إلى الهدف مع الحفاظ على التعبير والوضعية والإضاءة، ويخرج صورة مركّبة. تتيح عقدة ImageCompare التحقق بصريًا من النتائج مقارنة بالأصل لإجراء فحص A/B سريع، بينما تتولى SaveImageAdvanced التصدير بشكل نظيف وقابل للتكرار.
تقنيًا، تُغلّف عقدة تبديل الوجه خط الأنابيب الأساسي—اكتشاف الوجه، والمحاذاة/التحوير، ونقل الهوية، والدمج—لذا لا تحتاج لإدارة عُقَد كشف أو أقنعة منفصلة. هذا يجعل سير العمل صغيرًا ومتينًا: حمّل الصور، عدّل إعدادات عقدة التبديل اختياريًا (مثل تحديد أي وجه يتم استبداله أو قوة الدمج حسب التطبيق)، عاين عبر ImageCompare، ثم احفظ الناتج بتسمية وصيغة متسقتين عبر SaveImageAdvanced. وهو مفيد خصوصًا عندما تحتاج تبديلات موثوقة من صور إدخال جيدة دون بناء مخطط كبير ومعقد.
الأسئلة والأجوبة
استخدم صورًا حادة ومضاءة جيدًا مع وجه واضح وغير محجوب. تعمل الزاوية الأمامية أو زاوية ثلاثة أرباع الخفيفة بأفضل شكل. قد تؤدي الحجب الشديد (أيدٍ، شعر، أقنعة) أو البروفايل الحاد أو ضبابية الحركة إلى تدهور النتائج.
افتح عقدة تبديل الوجه (217f3726-59bc-4170-9ab1-c622bb578338). إذا كانت توفر خيار تحديد الوجه، اختر الفهرس/الوجه الذي تريد استبداله. إن لم يكن ذلك متاحًا، قصّ صورة الهدف حول الوجه المقصود وأعد التشغيل.
قد تظهر فروق كبيرة في الإضاءة أو اللون بين المصدر والهدف داخل الدمج. جرّب صورة مصدر متقاربة في الإضاءة والدرجة اللونية، أو قم بمعالجة مسبقة بتعديلات أساسية للون/التباين. قد تساعد أيضًا تعديلات خفيفة على الدمج/الترييش في عقدة التبديل (إن كانت متاحة).
لا. يُولّد سير العمل صورة جديدة بعد الاستبدال. تتيح SaveImageAdvanced التصدير بدون تدمير وفق الصيغة والجودة ونمط اسم الملف الذي تختاره.