Extensions/ComfyUI_SAM2UltraV2
ComfyUI Extension

ComfyUI_SAM2UltraV2

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…

By wzyfromhust·Created 9 months ago·Updated 9 months ago· 1
wzyfromhust/ComfyUI_SAM2UltraV2
Nodes
On cloudLocal install
Stars1
Updated9 months ago
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.