Bugs: - Remove unused edge_sensitivity param from remove_background_pil() - Add try/except fallback for structlog import (was ImportError outside ComfyUI) Warnings: - Subsample pixels in _kmeans_colors() to prevent OOM on 4K images - Set OUTPUT_NODE = True (node produces terminal IMAGE+MASK output) - Narrow except Exception to except ImportError in __init__.py Suggestions: - Extract ScalingMixin to shared scaling.py (used by both nodes) - Remove unused comfy.utils import and _COMFY_AVAILABLE flag
60 lines
2.5 KiB
Python
60 lines
2.5 KiB
Python
# Check for optional dependencies and provide helpful warnings
|
||
try:
|
||
import sklearn
|
||
SKLEARN_AVAILABLE = True
|
||
except ImportError:
|
||
SKLEARN_AVAILABLE = False
|
||
print("\n" + "="*70)
|
||
print("⚠️ TransparencyBackgroundRemover: scikit-learn not installed!")
|
||
print("="*70)
|
||
print("The node will work with reduced accuracy (color clustering disabled).")
|
||
print("\nTo install dependencies for best results, run ONE of:")
|
||
print(" • Windows (ComfyUI App): install.bat")
|
||
print(" • Linux/Mac: ./install.sh")
|
||
print(" • Manual: pip install scikit-learn")
|
||
print("\nInstallation files are in: custom_nodes/ComfyUI-TransparencyBackgroundRemover/")
|
||
print("="*70 + "\n")
|
||
|
||
# Import main transparency background remover nodes
|
||
from .nodes import NODE_CLASS_MAPPINGS as NODES_MAPPINGS, NODE_DISPLAY_NAME_MAPPINGS as NODES_DISPLAY_MAPPINGS
|
||
|
||
# Import standalone RemoveBackgroundAndResizeNode
|
||
try:
|
||
from .RemoveBackgroundAndResizeNode import (
|
||
NODE_CLASS_MAPPINGS as REMBG_MAPPINGS,
|
||
NODE_DISPLAY_NAME_MAPPINGS as REMBG_DISPLAY_MAPPINGS,
|
||
)
|
||
REMBG_AVAILABLE = True
|
||
except ImportError as e:
|
||
REMBG_MAPPINGS = {}
|
||
REMBG_DISPLAY_MAPPINGS = {}
|
||
REMBG_AVAILABLE = False
|
||
print("\n" + "="*70)
|
||
print("ℹ️ RemoveBackgroundAndResizeNode unavailable:", str(e))
|
||
print("="*70 + "\n")
|
||
|
||
# Try to import GrabCut nodes (optional - requires ultralytics)
|
||
try:
|
||
from .grabcut_nodes import NODE_CLASS_MAPPINGS as GRABCUT_MAPPINGS, NODE_DISPLAY_NAME_MAPPINGS as GRABCUT_DISPLAY_MAPPINGS
|
||
GRABCUT_AVAILABLE = True
|
||
except ImportError as e:
|
||
# GrabCut nodes require additional dependencies (ultralytics, etc.)
|
||
GRABCUT_MAPPINGS = {}
|
||
GRABCUT_DISPLAY_MAPPINGS = {}
|
||
GRABCUT_AVAILABLE = False
|
||
print("\n" + "="*70)
|
||
print("ℹ️ TransparencyBackgroundRemover: GrabCut nodes disabled")
|
||
print("="*70)
|
||
print(f"Reason: {str(e)}")
|
||
print("\nGrabCut nodes require additional dependencies. To enable them:")
|
||
print(" • Install: pip install ultralytics")
|
||
print(" • Or run: pip install -r requirements.txt")
|
||
print("\nMain background remover nodes are still available!")
|
||
print("="*70 + "\n")
|
||
|
||
# Combine node mappings from all modules
|
||
NODE_CLASS_MAPPINGS = {**NODES_MAPPINGS, **GRABCUT_MAPPINGS, **REMBG_MAPPINGS}
|
||
NODE_DISPLAY_NAME_MAPPINGS = {**NODES_DISPLAY_MAPPINGS, **GRABCUT_DISPLAY_MAPPINGS, **REMBG_DISPLAY_MAPPINGS}
|
||
|
||
__all__ = ['NODE_CLASS_MAPPINGS', 'NODE_DISPLAY_NAME_MAPPINGS']
|