SAM3 — native detection and masking

Native SAM3 masking for Comfiest — text-prompted image and video mattes with core ComfyUI nodes, prompt grammar, coverage checks, and alpha compositing.

This is a skill for AI agents. Install for Cursor, Claude, Codex, and other agents with npx oli-skills install sam3.

  • Cursor reads .cursor/skills/ and the rule created by npx oli-skills init.
  • Claude Code reads .claude/skills/.
  • Codex reads .codex/skills/ and .agents/skills/.

Source: comfiest

/sam3@sam3

Use this when you need a matte, not a full character-replacement graph. SAM3 takes an image or video plus a short text target and returns a clean luma matte. All three graphs are API-first and use core comfy_extras.nodes_sam3; no custom SAM3 node pack is needed.

image/video -> SAM3_Detect (or SAM3_VideoTrack -> SAM3_TrackToMask)
            -> GrowMaskWithBlur -> MaskToImage -> matte

The video graphs use ComfyUI-VideoHelperSuite; the feather node is GrowMaskWithBlur from ComfyUI-KJNodes. The SAM3 chain itself is core-only.

Files

Files live in ../../workflows/sam3/api/ and are API format for run_workflow.

FileTaskInputOutput
sam3-mask-video.api.jsonper-frame detect + segmentone videoluma matte video
sam3-mask-image.api.jsonper-image detect + segmentone imagematte PNG + inverted-background QA preview
sam3-track-video.api.jsonSAM3 memory trackerone videoluma matte video

All three are small (7–10 nodes) and meant to be run as-is with one or two inputs changed. Do not add them to the UI; open the JSON if you need to read it.

Run it

const run = await tools["comfy-<host>-<port>"].run_workflow({
  workflow_path: "<repo>/workflows/sam3/api/sam3-mask-video.api.json",
  wait: false
});

Then poll /history/<prompt_id> and collect the output. See ../workflow-development/SKILL.md for the full submit/poll/ collect loop.

Change the source and target in a copy under work/scratch/; never write back into workflows/.

AddressMeaningPublic default
10.video (sam3-mask-video, sam3-track-video)input video in the target's input/example.mp4
10.image (sam3-mask-image)input image in the target's input/example.png
12.textwhat to detectperson

Stage assets in work/uploads/<project>/ with the exact filename, then upload_file them to every target that may run the job. upload_file keeps the name unless the target already has that file, in which case it writes name (1).ext; use the returned cloud_name. Check VHS_LoadVideo's video combo (or /object_info/VHS_LoadVideo) before assuming a target has the file.

Choosing what SAM3 detects

12.text is a CLIPTextEncode feeding SAM3's text encoder. It is an open-vocabulary prompt with a small grammar:

PromptResult
personthe single most confident person
man / woman / childnarrows by apparent gender/age
person:3up to 3 people, unioned into one matte
dog, bicycleboth categories, unioned into one matte
red carattribute-steered; works when the attribute is visible
woman in white sweateruse when several similar subjects are present

Rules from the tokenizer (comfy/text_encoders/sam3_clip.py):

  • Commas split the prompt into separate categories. Each is detected independently, then unioned when individual_masks=false. Never put a comma inside one description.
  • :N sets max detections for that category (person:3, dog:2). Default is 1 per category, so person returns only the top-scoring instance.
  • The tokenizer caps at 32 tokens. Keep prompts to a short noun phrase; long sentences are truncated and behave unpredictably.
  • There is no text negative prompt. Use a more specific target, a higher 13.threshold, or the tracker's object_indices to drop unwanted objects.

Absent targets do not return empty

Measured on the same portrait, same graph, refine_iterations=2:

PromptCoverageWhat came back
person86.67%the person — correct
elephant (absent)86.67%still the person
narrow prop (absent)98.04%the person plus a band of wall/hair
man, narrow prop98.04%union poisoned by the absent category

SAM3 is a segmenter: an unmatched prompt falls back to the salient foreground object or a broader region rather than returning nothing. Two consequences:

  1. A nearly-full matte means the prompt is wrong, not that the subject is large.
  2. Never stack a speculative category into the prompt — one bad category floods the union. Test categories one at a time.

refine_iterations is not the cause: the absent prompt gave the same 98.04% at refine_iterations=0.

Knobs

AddressDefaultEffect
13.threshold0.5detection score gate. Raise to 0.6–0.7 to drop weak/background hits; lower to 0.3 if a real subject is missed
13.refine_iterations2SAM decoder passes on the detection crop; 0 is fastest and uses the raw detector mask
13.individual_masksfalseimages only: true emits one mask per object (N PNGs). Keep false for video — on video it concatenates per-object masks into the frame batch, multiplying the frame count and breaking timing
14.expand2dilate the matte by N px; 0 for a hard matte
14.blur_radius4.0feather in px; 0 for a hard matte
20.frame_rate24.0must match the source fps or the matte drifts against the plate. The graph does not wire fps from the loader to the saver, so set it by hand
13.detection_threshold (track)0.5tracker re-detection gate
13.max_objects (track)4tracked object budget; initial masks count toward it
13.detect_interval (track)1run detection every N frames; higher is cheaper but drifts
14.object_indices (track)"""" = all objects; "0", "0,2" select tracked IDs

Outputs

  • The matte is a luma matte: white subject on black, same size and frame count as the source.
  • Video → COMFY_WORKFLOWS/SAM3_MASK_*.mp4 (h264) or SAM3_TRACK_MASK_*.mp4; image → matte PNG plus a preview PNG where the subject stays normal and the background is inverted (a fast “did it grab the right thing” check).
  • Retrieve with fetch_outputs, or the target's /view URL. See ../workflow-development/SKILL.md for the direct URL pattern.

Sanity-check coverage before using a matte

Mean luma of a 1×1 downscale equals the matte's white fraction:

ffmpeg -i SAM3_MASK_00001.mp4 -vf "format=gray,scale=1:1" -f rawvideo -pix_fmt gray - \
  | python -c "import sys;b=sys.stdin.buffer.read();print(f'{sum(b)/len(b)/255:.2%} over {len(b)} frames')"

Expect roughly the subject's screen area: ~16% for a full-height figure in 16:9, and anywhere from ~40% to ~87% for a tight portrait (the verified 768×1376 head-and-shoulders matte measured 86.67%). Cross-check against the preview or a single extracted frame — the number alone cannot distinguish “subject fills the frame” from “prompt grabbed everything”. Above ~95% on a normal shot, assume the prompt is wrong.

Alpha WebM (compositing contract)

The matte is not alpha video, but it converts cleanly:

ffmpeg -i PLATE.mp4 -i SAM3_MASK_00001.mp4 \
  -filter_complex "[1:v]format=gray[m];[0:v][m]alphamerge" \
  -c:v libvpx-vp9 -pix_fmt yuva420p -b:v 0 -crf 28 -row-mt 1 -an fg.webm

VP9 stores alpha in a side channel: ffprobe reports pix_fmt=yuv420p with TAG:alpha_mode=1, and that tag — not the pix_fmt — is the proof alpha survived. Browsers decode it.

Text behind subject

  1. Run sam3-mask-video.api.json on the plate → matte.
  2. Convert to alpha WebM with the command above.
  3. In HTML, paint in this z-order: plate (z-index: 0, keeps audio), text (z-index: 10), alpha foreground (z-index: 20). Text is occluded by the subject with nothing baked into the plate.

Verified runs

Public sanitization changed only the input placeholders, not the graph shape or the default person target. Reference runs on a 24 GB RTX 4090, ComfyUI 0.43.0:

GraphInputPromptResultCoverage
sam3-mask-video1376×768, 48 fps, 383 fpersonSAM3_MASK_00001.mp4, ~7.98 s, ~150 s wall15.97% mean (10.2–24.3%)
sam3-mask-image768×1376personSAM3_MASK_IMG_00001_.png + preview86.67% (subject fills frame)
sam3-track-video48-frame cappersonSAM3_TRACK_MASK_00001.mp4, ~2.0 s8.96% mean

The video matte was visually checked (clean silhouette) and round-tripped to a VP9 alpha WebM, alpha_mode=1, against the source plate.

The public copy was re-rendered on 2026-11-10 (ComfyUI 0.45.0) after the tensor links were repaired: sam3-mask-image produced SAM3_MASK_IMG_00005_.png (57,547 B, YAVG 110) plus a preview showing the subject in colour over an inverted background, and sam3-mask-video produced SAM3_MASK_00003.mp4 (203,236 B, 416×736, 124 f) with a matte that tracked the performer.

Gotchas

  • frame_rate mismatch is the most common silent failure: the matte plays at the wrong speed and desyncs from the plate once converted to alpha. Set 20.frame_rate to the source fps.
  • Tracker vs per-frame: the tracker is temporally steadier and cheaper on long clips, but it can latch onto a second bright object (two small false positives on a lit window in the verified run) and it holds RAM across frames. Use object_indices to drop them, or stay on sam3-mask-video for short clips.
  • Per-frame detect can flicker on soft edges or motion blur. A light 14.blur_radius hides it; heavier cases need the tracker or a post-pass.
  • Hair, smoke, thin props: the matte is binary/feathered and stops at the silhouette. Thin smoke is not included, and hair edges need ImageMatting with a trimap for close-ups.
  • individual_masks=true on video breaks timing.
  • Empty detection returns an all-black matte, not an error (rare; unmatched prompts usually fall back to the salient object). The coverage check catches both failure directions.
  • GrowMaskWithBlur missing: it is KJNodes. If a target lacks it, delete nodes 14/15 and wire SAM3_Detect/SAM3_TrackToMask straight to MaskToImage; the SAM3 chain is core-only.
  • Wrong source file: example.mp4 and example.png are placeholders. Upload a real asset under the exact filename before running.

Requirements

  • ComfyUI 0.43.0 or newer with core comfy_extras.nodes_sam3 (SAM3_Detect, SAM3_VideoTrack, SAM3_TrackToMask). No custom SAM3 pack is needed.
  • models/checkpoints/sam3.1_multiplex_fp16.safetensors from Comfy-Org sam3.1.
  • ComfyUI-VideoHelperSuite for the video graphs; ComfyUI-KJNodes for GrowMaskWithBlur.
  • A 24 GB-class GPU is ample; a 383-frame 1376×768 video matte took ~150 s on a 4090.
Skills Mixer