Nodes/Kongshan Nodes/GroundingDINO + SAM 分割
ComfyUI Node

GroundingDINO + SAM 分割

Text Prompt Straight to Object Masks — One Node, No Wiring

By kongshan4219·Created 3 months ago·Updated 3 months ago· 0
GroundingDINO + SAM 分割
  • sam_model
  • grounding_dino_model
  • image
  • segmented_images
  • masks
prompt
threshold0.30

If KSGroundingDinoDetectKSSAMSegmentByBoxes is the canonical two-step, KSGroundedSAMSegment is the one-node version: it runs GroundingDINO to find your target by name, then SAM to trace its exact outline, and hands you segmented images plus masks - all from a single text prompt. No wires between detector and segmenter, no type mismatches to debug. For "cut out every shoe in this photo," it's the fastest route in the pack.

What it saves you

The combined node exists because the two-step version, while conceptually clean, has a real tax: the KS_DINO_BOXES type only flows between this pack's own nodes, and if you've ever wired a custom node pack whose internal types don't line up, you know the debugging that can eat an afternoon. Folding detection and segmentation into one node removes the seam entirely. You feed it two loaded models, an image, a prompt, and a threshold - it does the whole detect-then-segment loop internally and returns segmented_images and masks as a matched batch.

The tradeoff is control. In the two-step version you can inspect box_count, decide "that's wrong," and adjust the prompt before SAM ever runs. Here you get one shot: prompt → masks, no intermediate inspection. For a well-known product on a clean background that's a fine trade. For gnarly scenes where you expect to iterate, the two-step is the better debug loop - and the split nodes are right there in the same pack.

The inputs that matter

  • sam_model and grounding_dino_model - from the two loaders. The node can't work without both.
  • image - your photo batch.
  • prompt - the detection text. product finds the whole product; bottle, shoe, watch narrow it. This is the input you'll actually tune.
  • threshold (default 0.3) - GroundingDINO confidence cutoff. Same tradeoff as the split detector: lower = more boxes + more false positives.

Outputs: segmented_images (each detected instance as its own image, background blacked out) and masks (one per instance). Those feed KSProductsToBackground for the white-background crop, or KSApplyMaskBackground if you want to keep the original composition.

Installing

With the pack:

cd ComfyUI/custom_nodes
git clone https://github.com/kongshan4219/ComfyUI-Kongshan-Nodes

restart ComfyUI. First run downloads the SAM checkpoint into models/sams/ and the GroundingDINO weights into models/grounding-dino/, plus BERT for the text encoder - budget a couple of gigabytes and a slow first load.

Gotchas

  • If the prompt finds nothing, the loop breaks early. The source skips out when a frame yields zero boxes rather than emitting empty placeholders - so unlike the split SAM node, you don't get a clean "empty mask" you can gate on with KSIsMaskEmpty. You get a truncated batch. If your workflow depends on handling "no product found" gracefully, prefer the two-step path.
  • Two models on one card. SAM + GroundingDINO resident simultaneously. sam_vit_b + SwinT is a comfortable combo; vit_h + SwinB will test your VRAM.
  • It's the same classic pipeline, packaged. Don't expect SAM 3-style concept segmentation or anything newer - this is the 2023-2024 GroundingDINO+SAM combo, which is still perfectly good for "find the product and cut it out."

The whole pack is young and obscure (zero impressions on every node as of now), and this combined node is its "I just want it to work" option. It's the kind of node you add to a workflow, forget exists, and only remember fondly when you're not debugging type mismatches.

CategoryKongshan/Local

Inputs (5)

NameTypeDefaultDescription
sam_modelSAM_MODEL已加载的 SAM 模型,用于根据检测框生成精细遮罩。
grounding_dino_modelGROUNDING_DINO_MODEL已加载的 GroundingDINO 模型,用于根据 prompt 找目标框。
imageIMAGE待检测和分割的图片批次。
promptSTRING检测目标文本。比如 product 会倾向于找商品整体;更具体的词会改变检测范围。
thresholdFLOAT0.300–1GroundingDINO 检测阈值。降低更容易检出多个目标但误检增加;提高更严格但可能没有框。

Outputs (2)

NameTypeDescription
segmented_imagesIMAGE
masksMASK