How gourieff/comfyui-reactor works
Project Type and Purpose
• This is a Python-based plugin (extension) for ComfyUI—a node-based workflow engine for AI image processing—providing nodes that perform face swapping, face model creation, masking and restoration.
• It’s not a web application or microservice. It’s a modular plugin that hooks into ComfyUI’s node registry and execution engine, invoking deep-learning models (ONNX, PyTorch) for detection, swapping, restoration and related utilities.
High-Level Component Analysis
• ComfyUI Core
• Web UI (browser front-end)
• Node Execution Engine (Python backend)
• ReActor Extension (loaded as “custom_nodes/ComfyUI-ReActor”)
• Node Registration Layer (nodes.py)
• Pipeline Modules (modules/*.py)
• Face‐swap Logic & Utilities (reactor_utils.py, reactor_patcher.py)
• Third-Party Integration Libraries
• BasicSR integration (r_basicsr package: archs, data loaders, losses, metrics, models, ops)
• InsightFace / ONNX models for face detection & embeddings (r_facelib)
• Chainner architectures for face model blending (r_chainner)
• CLI / Installer
• install.py / install.bat
• scripts/ directory with sub-folders for architecture scripts, masking, boosting, etc.
• External Model Repositories
• HuggingFace downloads (inswapper_128.onnx, buffalo_l, face-restorers models, SAM, YOLO face detector)
Data Flow & Interactions
• User drags “ReActorFaceSwap” node into a ComfyUI workflow in the browser → ComfyUI registers it via nodes.py → on execution:
1. The node calls reactor_utils, which invokes:
a. Face detection (r_facelib → ONNX detectors or ultralytics)
b. Face embedding extraction (r_facelib → inswapper_128.onnx)
c. Face swap algorithm (r_chainner + shader scripts)
2. If restoration is enabled, it calls BasicSR models (r_basicsr) to enhance the swapped faces:
• Model architectures (r_basicsr/archs/*)
• Data transforms, degradations (r_basicsr/data)
• Inference ops via PyTorch/CUDA or ONNXRuntime
3. Final compositing and masking (modules/scripts_postprocessing.py)
4. The resulting image is returned to ComfyUI for further chaining or display.
Architectural Patterns & Design Principles
• Plugin Architecture: ComfyUI discovers and loads custom_nodes dynamically.
• Modular Node Graph: Each face‐swap/restore/mask operation is encapsulated in its own node module under modules/.
• Separation of Concerns:
• Detection & Embedding (r_facelib)
• Swapping & Blending (r_chainner)
• Super-resolution & Restoration (r_basicsr)
• Orchestration & Compositing (modules/ & reactor_utils)
• Dependency Injection: Nodes declare inputs/outputs so ComfyUI composes pipelines declaratively.