db92f67158187220c7378501bd1e1b239f41532f
Face Processor for ComfyUI
A custom node collection for ComfyUI that provides advanced face detection, alignment, and transformation capabilities using MediaPipe Face Mesh.
Features
- Face Detection & Landmark Extraction: Uses MediaPipe Face Mesh to detect and extract 468 facial landmarks
- Face Alignment: Automatic face alignment based on eye positions
- Face Transformation:
- Warping between source and target face landmarks
- Scale and translation controls for face shape adjustment
- CPU and CUDA-accelerated processing options
- Debug Visualization:
- Visual landmark overlay with customizable parameters
- Support for both detected and target landmark visualization
- Optional landmark labels
Installation
- Clone this repository into your ComfyUI's
custom_nodesdirectory:
cd ComfyUI/custom_nodes
git clone [repository-url] face_processor
- Install required dependencies:
pip install mediapipe opencv-python numpy pandas pillow torch
For CUDA acceleration:
- Install CUDA Toolkit
- Install CuPy:
pip install cupy-cuda11x # Replace with your CUDA version
Nodes
FaceWrapper
Main node for face detection and transformation operations.
Inputs:
image: Input image (ComfyUI IMAGE type)mode: Operating modeDebug: Visualization of detected landmarksUn-Wrap: Transform face to normalized positionWrap: Transform normalized face back to original position
device: Processing device (CPUorCUDA)show_detection: Toggle detected landmarks visualizationshow_target: Toggle target landmarks visualizationlandmark_size: Size of landmark points in visualizationshow_labels: Toggle landmark index labelsx_scale: Horizontal scaling factor (0.5 to 1.0)y_transform: Vertical translation (-0.5 to 0.5)processor_settings: Optional settings dictionary
Outputs:
image: Processed imageprocessor_settings: Updated settings dictionary
FaceFitAndRestore
Node for face cropping and restoration operations.
Inputs:
mode: Operating modeFit: Crop and align faceRestore: Place processed face back in original image
image: Input imagepadding_percent: Additional padding around face (0.0 to 1.0)bbox_size: Output size for cropped face (512, 1024, or 2048)processor_settings: Required for Restore mode
Outputs:
image: Processed imageprocessor_settings: Updated settings dictionarymask: Mask indicating face region
Technical Details
Core Components
Face Detection
- Uses MediaPipe Face Mesh for robust face detection and landmark extraction
- Provides 468 facial landmarks with 3D coordinates
- Supports various input formats (PIL Image, numpy array, torch tensor)
Image Processing
- Automatic face rotation based on eye positions
- Aspect ratio-preserving resizing
- Support for square cropping with configurable padding
- Boundary triangulation for complete face warping
Face Warping
- Triangle-based warping using predefined mesh topology from MediaPipe Face Mesh
- CPU implementation using pure Python/NumPy
- CUDA-accelerated GPU implementation using CuPy
- Handles both forward and inverse warping
Performance Considerations
- GPU acceleration requires CUDA toolkit and CuPy
- CPU fallback available for all operations
- Progressive feedback during long operations
- Memory-efficient processing for large images
Example Usage
Basic face detection and visualization:
face_wrapper = FaceWrapper()
result_image, settings = face_wrapper.detect_face(
image=input_image,
mode="Debug",
device="CPU",
show_detection=True,
show_target=False,
landmark_size=4,
show_labels=True,
x_scale=1.0,
y_transform=0.0
)
Face normalization workflow:
- Detect and normalize face:
# Unwrap face to normalized position
normalized_face, settings = face_wrapper.detect_face(
image=input_image,
mode="Un-Wrap",
device="CUDA",
x_scale=1.0,
y_transform=0.0
)
-
Process normalized face with your preferred method
-
Restore face to original position:
# Wrap processed face back
final_image, _ = face_wrapper.detect_face(
image=processed_face,
mode="Wrap",
device="CUDA",
processor_settings=settings
)
License
[Your License Here]
Acknowledgments
- MediaPipe Face Mesh for facial landmark detection
- ComfyUI project for the node system framework
Languages
Python
100%