Skip to content

Advanced Face Restoration for ComfyUI - Professional face enhancement with intelligent quality assessment and model fallback systems

License

Notifications You must be signed in to change notification settings

flickleafy/facerestore_advanced

Repository files navigation

Face Restore Advanced - Advanced Face Restoration for ComfyUI

License ComfyUI

Overview

Face Restore Advanced is a comprehensive and advanced face restoration node suite for ComfyUI, providing state-of-the-art face enhancement capabilities with intelligent quality assessment and model fallback systems. This is a complete refactoring and significant expansion of the original facerestore implementation, incorporating cutting-edge features for professional-quality face restoration.

Example

Key Features

🔥 Advanced Face Restoration Pipeline

  • Dual Model Support: Full CodeFormer and GFPGAN integration with automatic model detection
  • Intelligent Caching System: Shared cache for face processing results to optimize repeated operations
  • Model Fallback System: Automatic fallback to alternative models when primary restoration fails quality checks
  • Fidelity Control: Fine-grained CodeFormer fidelity parameter control (0.0-1.0)

🧠 Comprehensive Quality Assessment

  • Multi-Metric Analysis: SSIM, artifact detection, color bleeding analysis, geometric distortion detection
  • Face-Specific Quality Metrics: Skin texture analysis, facial landmark preservation, feature consistency
  • Model-Specific Artifact Detection: Specialized detection for CodeFormer and GFPGAN artifacts
  • Mouth Corner Artifact Detection: Advanced detection of corner-of-mouth artifacts common in face restoration
  • Progressive Thresholding: Quality thresholds with gradual strictness levels (0.0-1.0)

🎯 Specialized Face Analysis

  • 68-Point Landmark Detection: Detailed facial feature analysis and preservation scoring
  • Skin Texture Quality Assessment: Advanced texture analysis using local binary patterns
  • Eye and Mouth Region Analysis: Specialized quality assessment for critical facial features
  • Color Consistency Analysis: Jensen-Shannon divergence for color distribution analysis

Performance Optimizations

  • Intelligent Caching: Face detection and restoration results cached for efficiency
  • Memory Management: Automatic cache size limiting and cleanup
  • Robust Error Handling: Comprehensive error recovery with fallback mechanisms
  • Tensor Optimization: Advanced tensor-to-image conversion with overflow protection

Node Types

1. FaceRestoreCFWithModel

The main face restoration node with advanced quality control and fallback capabilities.

Parameters:

  • facerestore_model: The loaded face restoration model (CodeFormer or GFPGAN)
  • image: Input image(s) for face restoration
  • facedetection: Face detection method (retinaface_resnet50, retinaface_mobile0.25, YOLOv5l, YOLOv5n)
  • codeformer_fidelity: Fidelity parameter for CodeFormer (0.0-1.0, higher preserves more original details)
  • quality_threshold: Quality threshold for accepting restoration (0.0-1.0, set to 0.0 to disable)
  • enable_model_fallback: Enable automatic model fallback on quality failure
  • fallback_model: Optional explicit fallback model

2. CropFace

Extracts and crops detected faces from images.

Parameters:

  • image: Input image(s)
  • facedetection: Face detection method

3. CropFaceRestored

Extracts faces, restores them, and returns the cropped restored faces.

Parameters:

  • Same as FaceRestoreCFWithModel but returns cropped faces instead of full images

4. FaceRestoreModelLoader

Loads face restoration models with intelligent architecture detection.

Parameters:

  • model_name: Model file from models/facerestore_models directory

Installation

Quick Installation

  1. Clone this repository to your ComfyUI custom nodes directory:
cd ComfyUI/custom_nodes
git clone https://github.com/flickleafy/facerestore_advanced.git
  1. Install dependencies:

Windows (Portable): Run embedded_install.bat Windows (Standard): Run install.bat
Linux/Mac: Run install.sh

Manual Dependency Installation

If the automatic installation fails, manually install these dependencies:

pip install opencv-python scikit-image scipy dlib

Note: If you encounter a "cv2 not found" error, ensure opencv-python is properly installed.

Required Models

Face Restoration Models

Place these models in ComfyUI/models/facerestore_models/:

CodeFormer:

GFPGAN:

Face Detection Models

These models are automatically downloaded on first use to ComfyUI/models/facedetection/:

RetinaFace Models:

YOLOv5 Face Models:

Face Parsing Model:

Advanced Usage Guide

Quality Threshold Settings

The quality threshold parameter provides fine-grained control over restoration acceptance:

  • 0.0: Disable quality assessment (always accept restoration)
  • 0.1-0.3: Very lenient (accept most restorations)
  • 0.4-0.6: Balanced (recommended for most use cases)
  • 0.7-0.8: Strict (only accept high-quality restorations)
  • 0.9-1.0: Very strict (only accept exceptional restorations)

Model Fallback System

Enable automatic model fallback for optimal results:

  1. Set enable_model_fallback to True
  2. Optionally provide a fallback_model (or let the system auto-determine)
  3. If primary restoration fails quality checks, the system automatically tries the fallback model
  4. Best result is automatically selected based on quality metrics

CodeFormer Fidelity Parameter

Fine-tune the balance between restoration strength and detail preservation:

  • 0.0: Maximum restoration, minimal original detail preservation
  • 0.5: Balanced restoration (recommended starting point)
  • 1.0: Maximum original detail preservation, minimal restoration

Face Detection Methods

Choose the optimal detection method for your use case:

  • retinaface_resnet50: Highest accuracy, slower processing
  • retinaface_mobile0.25: Good accuracy, faster processing
  • YOLOv5l: Large model, excellent detection capabilities
  • YOLOv5n: Lightweight model, fastest processing

Quality Assessment Metrics

The system evaluates restoration quality using comprehensive metrics:

Core Quality Metrics

  • SSIM (Structural Similarity): Measures overall image similarity
  • Artifact Detection: Identifies restoration artifacts and noise
  • Color Bleeding Analysis: Detects unnatural color mixing
  • Geometric Distortion: Measures facial feature distortion

Face-Specific Metrics

  • Skin Texture Quality: Evaluates skin texture improvement
  • Facial Feature Preservation: Measures landmark consistency
  • Eye Region Analysis: Specialized eye area quality assessment
  • Mouth Corner Artifacts: Detects common mouth corner issues

Model-Specific Analysis

  • CodeFormer Artifacts: Over-smoothing, skin uniformity issues
  • GFPGAN Artifacts: Over-sharpening, checkerboard patterns

Performance Optimization

Caching System

  • Face processing results are automatically cached
  • Cache key includes image content, model, and parameters
  • Automatic cache size management (50 entry limit)
  • Significant performance improvement for batch processing

Memory Management

  • Automatic tensor cleanup after processing
  • CUDA memory clearing for GPU acceleration
  • Overflow protection in image conversions
  • Robust error handling with graceful degradation

Technical Implementation

Architecture

  • Modular Design: Separate classes for face restoration, cropping, and model loading
  • Smart Model Detection: Automatic CodeFormer/GFPGAN architecture identification
  • Robust Tensor Handling: Advanced image-tensor conversion with error protection
  • Comprehensive Logging: Detailed quality assessment reporting

Error Handling

  • Graceful fallback on model loading failures
  • Quality assessment error recovery
  • Invalid value handling in image processing
  • Memory overflow protection

Code Quality

  • Type hints throughout codebase
  • Comprehensive documentation
  • Modular function design
  • Extensive error checking

Troubleshooting

Common Issues

"cv2 not found" Error:

pip install opencv-python

"SSIM calculation failed" Warning:

pip install scikit-image

Memory Issues with Large Batches:

  • Reduce batch size
  • Enable automatic fallback
  • Lower quality thresholds

Poor Restoration Quality:

  • Increase quality threshold
  • Enable model fallback
  • Try different face detection methods
  • Adjust CodeFormer fidelity parameter

Debug Information

Enable detailed quality assessment logging by monitoring the console output. The system provides comprehensive metrics for each face restoration attempt.

Contributing

We welcome contributions! Areas of particular interest:

  • Additional quality assessment metrics
  • New face detection methods
  • Performance optimizations
  • Model architecture support
  • Documentation improvements

Changelog

Latest Version

  • ✨ Complete refactoring of face restoration pipeline
  • ✨ Advanced quality assessment with 15+ metrics
  • ✨ Intelligent model fallback system
  • ✨ Comprehensive caching for performance optimization
  • ✨ Mouth corner artifact detection
  • ✨ Progressive quality thresholding
  • ✨ Enhanced error handling and memory management
  • ✨ Detailed quality reporting and analysis
  • ✨ Model-specific artifact detection
  • ✨ Facial landmark preservation analysis

Credits and Acknowledgments

This project builds upon and integrates several excellent open-source projects:

Special thanks to the computer vision and machine learning research communities for their groundbreaking work in face restoration and quality assessment.

License

This project is licensed under the GNU General Public License v3.0 (GPLv3). See the LICENSE file for full details.

Copyright © 2025 flickleafy

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

About

Advanced Face Restoration for ComfyUI - Professional face enhancement with intelligent quality assessment and model fallback systems

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published