ComfyUI Extension: ComfyUI_SAM2UltraV2

Authored by wzyfromhust

Created

Updated

0 stars

A fully modular and high-performance implementation of SAM2 (Segment Anything Model 2) for ComfyUI with dual-prompt support, negative point sampling, and upper body segmentation.

Custom Nodes (0)

    README

    ComfyUI SAM2 Ultra V2

    A fully modular and high-performance implementation of SAM2 (Segment Anything Model 2) for ComfyUI.

    Features

    • šŸš€ High Performance: Model loading optimization with 50-80% faster repeated inference
    • šŸŽÆ Dual Prompt Support: Combine bbox and keypoint prompts for enhanced accuracy
    • ā­• Negative Point Sampling: Ring-region negative sampling for better foreground/background separation
    • šŸ‘¤ Upper Body Segmentation: Hip-based intelligent cutting for portrait segmentation
    • šŸ“¦ Fully Modular: Clean code architecture with separated concerns
    • šŸ”§ Independent Package: No dependencies on other custom nodes

    Nodes

    1. SAM2 Ultra V2: Load Model

    Load and cache SAM2 model for reuse across multiple segmentations.

    Inputs:

    • model: SAM2 model selection
    • precision: fp16/bf16/fp32
    • device: cuda/cpu
    • segmentor: single_image/video

    Outputs:

    • sam2_model: Reusable model object

    2. SAM2 Ultra V2: Upper Body

    Segment upper body using hip keypoints for intelligent cutting.

    Inputs:

    • sam2_model: From Load Model node
    • image: Input image
    • pose_keypoint: OpenPose keypoints
    • keypoint_types: upper_body/face/hands
    • Detail processing parameters

    Outputs:

    • image: RGBA image with transparency
    • mask: Binary mask
    • context_image: Visualization with keypoints and cut line

    3. SAM2 Ultra V2: Crop

    Bbox-based segmentation with dual prompts and negative sampling.

    Inputs:

    • sam2_model: From Load Model node
    • image: Input image
    • bboxes: Detection bounding boxes
    • pose_keypoint (optional): For dual-prompt mode
    • keypoint_types: none/face/hands
    • enable_negative_points: Enable ring sampling
    • negative_sample_count: Number of negative points (default: 4)
    • negative_margin: Buffer zone size (default: 0.1)
    • Detail processing parameters

    Outputs:

    • image: RGBA image with transparency
    • mask: Binary mask
    • context_image: Visualization with bboxes and keypoints

    Installation

    1. Clone this repository into ComfyUI/custom_nodes/:
    cd ComfyUI/custom_nodes/
    git clone https://github.com/yourusername/ComfyUI_SAM2UltraV2.git
    
    1. Install dependencies:
    cd ComfyUI_SAM2UltraV2
    pip install -r requirements.txt
    
    1. Restart ComfyUI

    Model Download

    SAM2 models will be automatically downloaded from Hugging Face on first use. Models are stored in ComfyUI/models/sam2/.

    Available models:

    • sam2_hiera_tiny.safetensors (Fastest)
    • sam2_hiera_small.safetensors
    • sam2_hiera_base_plus.safetensors (Recommended)
    • sam2_hiera_large.safetensors (Best quality)
    • SAM 2.1 versions with -fp16 suffix

    Usage Example

    Basic Upper Body Segmentation

    OpenPose Estimator → Pose Keypoint
                      ↓
    SAM2 Ultra V2: Load Model → SAM2 Ultra V2: Upper Body → Output
                             ↓
                        Image Input
    

    Crop Segmentation with Dual Prompts

    YOLO Detector → Bboxes
                 ↓
    SAM2 Ultra V2: Load Model → SAM2 Ultra V2: Crop → Output
                             ↓              ↓
                        Image Input    Pose Keypoint (optional)
    

    Technical Details

    Negative Point Sampling

    The crop node uses ring-region sampling to place negative points between the bbox and expanded crop region. This helps SAM2 distinguish between:

    • Inner region (bbox + margin): Foreground (positive points)
    • Ring region: Background (negative points)
    • Outer region (crop): Context for segmentation

    Module Structure

    ComfyUI_SAM2UltraV2/
    ā”œā”€ā”€ __init__.py              # Node registration
    ā”œā”€ā”€ config.py                # Constants and configuration
    ā”œā”€ā”€ model_loader.py          # SAM2 model loading
    ā”œā”€ā”€ keypoint_utils.py        # Keypoint processing
    ā”œā”€ā”€ sampling.py              # Negative point sampling
    ā”œā”€ā”€ imagefunc.py             # Image utilities
    ā”œā”€ā”€ blendmodes.py            # Blend mode utilities
    ā”œā”€ā”€ sam2/                    # SAM2 model architecture
    └── nodes/
        ā”œā”€ā”€ load_model.py        # Load Model node
        ā”œā”€ā”€ upper_body.py        # Upper Body node
        └── crop.py              # Crop node
    

    Requirements

    • Python 3.8+
    • PyTorch 1.13+
    • ComfyUI
    • See requirements.txt for full list

    License

    MIT License

    Credits

    • SAM2 Model: Meta AI
    • Original SAM2 Ultra implementation: LayerStyle
    • Modular refactoring and negative sampling: dz

    Changelog

    v2.0.0 (2025-11-13)

    • Initial modular release
    • Added negative point sampling
    • Separated into independent package
    • Improved code maintainability (38% reduction in complexity)
    • Added comprehensive documentation

    Support

    For issues and feature requests, please open an issue on GitHub.