2025-01-26 13:41:11 +01:00
WIP
2025-01-26 13:06:12 +01:00
2025-01-26 13:41:11 +01:00
2025-01-26 13:41:11 +01:00
2025-01-26 13:41:11 +01:00

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

  1. Clone this repository into your ComfyUI's custom_nodes directory:
cd ComfyUI/custom_nodes
git clone [repository-url] face_processor
  1. Install required dependencies:
pip install mediapipe opencv-python numpy pandas pillow torch

For CUDA acceleration:

  1. Install CUDA Toolkit
  2. 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 mode
    • Debug: Visualization of detected landmarks
    • Un-Wrap: Transform face to normalized position
    • Wrap: Transform normalized face back to original position
  • device: Processing device (CPU or CUDA)
  • show_detection: Toggle detected landmarks visualization
  • show_target: Toggle target landmarks visualization
  • landmark_size: Size of landmark points in visualization
  • show_labels: Toggle landmark index labels
  • x_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 image
  • processor_settings: Updated settings dictionary

FaceFitAndRestore

Node for face cropping and restoration operations.

Inputs:

  • mode: Operating mode
    • Fit: Crop and align face
    • Restore: Place processed face back in original image
  • image: Input image
  • padding_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 image
  • processor_settings: Updated settings dictionary
  • mask: 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:

  1. 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
)
  1. Process normalized face with your preferred method

  2. 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
S
Description
No description provided
Readme
153 KiB
Languages
Python 100%