GroundingDinoSAMSegment (segment anything)
GroundingDinoSAMSegment (segment anything)
- sam_model
- grounding_dino_model
- image
- IMAGE
- MASK
Type a word, get a mask. That's the whole pitch, and it's a good one. You hand this node an image and the text "the red dress," and it hands back a pixel-precise mask around the red dress - no painting, no clicking, no brush. It's the node that made text-prompted masking a thing in ComfyUI, and if you've ever inherited an old workflow that "just segments the cat," this is almost certainly what was doing it.
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 time doesn't scale. This node automates the selection with a text query. That makes it the front door to a category of work: targeted inpainting, regional prompting, compositing, batch background-stripping for a dataset. SAM plus GroundingDINO is the right call when you need a specific object, not just foreground versus background - and it's overkill for a plain product cutout, where BiRefNet or rembg would be faster.
The killer feature is that it runs unattended. Point it at a folder of 500 images with the prompt "face" and it'll mask every face, no human in the loop. That's why it caught on for dataset prep in the first place.
How it works
It's two models wearing one node. GroundingDINO is a text-grounded object detector: give it a phrase and it draws bounding boxes around whatever matches. SAM (Meta's Segment Anything Model) then takes those boxes and turns each one into a clean, pixel-accurate mask. GroundingDINO answers "where is it," SAM answers "what's its exact outline." You supply both models (loaded by their own nodes) plus the image and the prompt, and the node chains them for you.
The inputs and outputs that matter
Five 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. Miss the object? Lower it. Grabbing junk you didn't ask for? Raise it. 0.3 is a sane start; most tuning happens here.
The other three - sam_model, grounding_dino_model, and image - are just wiring: the two model loaders and the picture to segment.
Outputs are IMAGE and MASK. The MASK is the black-and-white selection you feed into an inpaint sampler, a SetLatentNoiseMask, an IC-Light node, or a composite. The IMAGE is the cutout - your input with everything outside the match removed - handy when you just want the isolated subject on transparency.
How to install it
ComfyUI Manager is the easy path: search segment anything (the storyicon pack), install, restart. Manual:
cd ComfyUI/custom_nodes
git clone https://github.com/storyicon/comfyui_segment_anything
then pip install -r requirements.txt inside the folder and restart. The models auto-download on first use, but you can pre-place them: GroundingDINO weights go in models/grounding-dino, SAM weights in models/sams. There's also a quiet third dependency - GroundingDINO uses bert-base-uncased as its text encoder, which the transformers library pulls automatically to ~/.cache/huggingface. First run pulls a few gigabytes, so it won't be instant.
Common issues & troubleshooting
GroundingDINO won't install. This is the failure mode, and it's been true since 2023 - people report SAM working fine while "GroundingDINO install failed." It ships CUDA ops that sometimes need to compile, and if the build doesn't go through, the detector half is dead. If Manager's install throws errors, that's usually where.
The pack is old and abandoned. Be honest with yourself here: storyicon's repo hasn't moved in years, and on modern setups it breaks against newer transformers (v5+). If you're on a fresh, up-to-date ComfyUI and it refuses to import, that's why - it's not you. On a managed host like comfy.icu it's pinned to a working environment so it runs, but for a local install this is aging code.
It grabbed the wrong thing or nothing at all. Tune threshold first, then get more specific in the prompt - "blue car" beats "car" in a busy scene. GroundingDINO leans on generic nouns; the more distinctive your phrase, the better the boxes.
Masks a touch loose. SAM hugs objects well but isn't magic on flyaway hair. If you're inpainting off the result, feather the mask downstream; if you're compositing, a hard SAM edge often wants a pixel or two of blur.
If you're building from scratch today, know the whole SAM/GroundingDINO toolchain has since been folded into bigger, current packs - so this is more "what old workflows use" than "what you'd pick now." But it still does exactly what it says.
Inputs (5)
| Name | Type | Default | Description |
|---|---|---|---|
| sam_model | SAM_MODEL | — | |
| grounding_dino_model | GROUNDING_DINO_MODEL | — | |
| image | IMAGE | — | |
| prompt | STRING | — | |
| threshold | FLOAT | 0.300–1 | — |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| IMAGE | IMAGE | — |
| MASK | MASK | — |