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 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 selectionprecision: fp16/bf16/fp32device: cuda/cpusegmentor: 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 nodeimage: Input imagepose_keypoint: OpenPose keypointskeypoint_types: upper_body/face/hands- Detail processing parameters
Outputs:
image: RGBA image with transparencymask: Binary maskcontext_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 nodeimage: Input imagebboxes: Detection bounding boxespose_keypoint(optional): For dual-prompt modekeypoint_types: none/face/handsenable_negative_points: Enable ring samplingnegative_sample_count: Number of negative points (default: 4)negative_margin: Buffer zone size (default: 0.1)- Detail processing parameters
Outputs:
image: RGBA image with transparencymask: Binary maskcontext_image: Visualization with bboxes and keypoints
Installation
- Clone this repository into
ComfyUI/custom_nodes/:
cd ComfyUI/custom_nodes/
git clone https://github.com/yourusername/ComfyUI_SAM2UltraV2.git
- Install dependencies:
cd ComfyUI_SAM2UltraV2
pip install -r requirements.txt
- 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.safetensorssam2_hiera_base_plus.safetensors(Recommended)sam2_hiera_large.safetensors(Best quality)- SAM 2.1 versions with
-fp16suffix
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.txtfor 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.