GroundingDinoSAM2Segment (segment anything2)
GroundingDinoSAM2Segment (segment anything2)
- sam_model
- grounding_dino_model
- image
- IMAGE
- MASK
Type a word, get a mask. That's the whole pitch, and it's still a good one. You feed this node an image and the text "the red dress", and it hands back a pixel-tight mask around the red dress - no brush, no clicking, no manual selection. It's the SAM 2 version of the old text-prompted masking node, so if you liked the "just describe the thing" workflow but want Meta's newer segmenter under the hood, this is that.
Why you'd reach for it
Masking is the annoying part of half the workflows in this ecosystem. You want to inpaint just the jacket, relight just the product, or cut out just the person - but first you have to select that region, and doing it by hand every run doesn't scale. This node automates the selection with a text query, which makes it the front door to a whole category of work: targeted inpainting, regional prompting, compositing, batch dataset prep. It's the right call when you need a specific object rather than plain foreground-versus-background - for a simple product cutout it's overkill, and BiRefNet or rembg are faster and lighter.
The reason people put up with the dependency weight is that it runs unattended. Point it at a folder with the prompt "face" and it masks every face, no human in the loop.
How it works
Two models wearing one node. GroundingDINO is a text-grounded object detector: give it a phrase, it draws bounding boxes around whatever matches. SAM 2 (Segment Anything 2) then turns each box into a clean, pixel-accurate mask. GroundingDINO answers "where is it," SAM 2 answers "what's its exact outline." Both models are loaded by their own nodes and passed in here, and this node chains them.
Worth knowing: SAM 2's headline feature is video - tracking an object across frames - but this node only takes a single image, so you're using SAM 2 purely as a sharper, faster image segmenter. Fine reason to use it; just don't expect the video tricks here.
The inputs and outputs that matter
Six inputs, but you really only touch two:
- prompt (
STRING) - the object you want, in plain words."dog","the person on the left","hair". This is your selection. - threshold (
FLOAT, default0.3) - GroundingDINO's detection confidence, 0 to 1. Missing the object? Lower it. Grabbing junk you didn't ask for? Raise it.0.3is a sane start and most of your tuning happens right here.
The rest is wiring or a convenience toggle. sam_model (SAM2_MODEL) and grounding_dino_model (GROUNDING_DINO_MODEL) come from the two loader nodes; image is the picture to segment. keep_model_loaded (BOOLEAN, default false) decides whether the models stay resident in VRAM between runs - leave it false to free memory (it reloads next run), flip it true when you're firing many segments in a row and want to skip the reload.
Outputs are IMAGE and MASK. The MASK is the black-and-white selection you wire into an inpaint sampler, a SetLatentNoiseMask, an IC-Light node, or a composite - this is the output you'll use 90% of the time. The IMAGE is the cutout, your input with everything outside the match dropped, handy when you just want the isolated subject.
How to install it
ComfyUI Manager is the easy path: search ComfyUI SAM2 (or "Segment Anything 2"), install, restart. Manual:
cd ComfyUI/custom_nodes
git clone https://github.com/neverbiasu/ComfyUI-SAM2
then pip install -r requirements.txt inside the folder and restart. Models auto-download on first use, or you can pre-place them: SAM 2 checkpoints in models/sam2, GroundingDINO weights (config and .pth, don't rename them) in models/grounding-dino. There's also a quiet third dependency - GroundingDINO uses bert-base-uncased as its text encoder, which transformers pulls to models/bert-base-uncased. First run downloads a couple gigabytes across all three, so it won't be instant. If a download crawls, set HTTP_PROXY / HTTPS_PROXY.
Common issues & troubleshooting
GroundingDINO won't install. This is the failure mode for this whole family, and it's been true since the SAM 1 days - people routinely report SAM working fine while the GroundingDINO half fails to build. It ships CUDA ops that sometimes need to compile, and if the build doesn't go through, the detector is dead and this node can't run. On a managed host like comfy.icu it's pinned to a working environment, so it just runs; a local install is where you'll hit this.
It grabbed the wrong thing, or nothing. Tune threshold first, then get more specific in the prompt - "blue car" beats "car" in a busy scene. GroundingDINO leans on generic nouns, so a distinctive phrase gives better boxes. If the detection is wrong, no SAM 2 checkpoint will save it; the outline is only ever as good as the box it was handed.
Mask is a touch loose. SAM 2 hugs objects well but isn't magic on flyaway hair or fine mesh. If you're inpainting off it, feather or grow the mask downstream; if compositing, a hard edge usually wants a pixel or two of blur.
OOM. You're holding GroundingDINO plus a SAM 2 checkpoint plus the rest of your graph in VRAM. Drop to a smaller SAM 2 checkpoint at the loader, and leave keep_model_loaded off so the weights release between runs.
Inputs (6)
| Name | Type | Default | Description |
|---|---|---|---|
| sam_model | SAM2_MODEL | — | |
| grounding_dino_model | GROUNDING_DINO_MODEL | — | |
| image | IMAGE | — | |
| prompt | STRING | — | |
| threshold | FLOAT | 0.300–1 | — |
| keep_model_loaded | BOOLEAN | false | — |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| IMAGE | IMAGE | — |
| MASK | MASK | — |