ComfyUI Node

seg

YOLOv8 masks you can actually use

By zcfrank1st·Created 3 years ago·Updated 2 years ago· 26
seg
  • image
  • IMAGE
  • MASK
model_name
class_id0

The detection node in this pack draws boxes around things. This one goes further and gives you the thing itself - a real pixel mask of the class you care about, ready to feed into an inpaint node, a mask editor, or a composite. That's the step that turns "there's a person in this image" into "here's exactly where the person is," and it's the reason segmentation beats plain detection for almost any real workflow.

It's the second of two nodes in zcfrank1st's tiny Comfyui-Yolov8 pack, and it shares the detection node's install, its models/yolov8/ folder, and its "deadly simple" philosophy. The one real difference: it runs a YOLO segmentation model and filters the output down to a single class, which is where the interesting part - the class_id input - comes in.

How it works

The node loads a .pt model from ComfyUI/models/yolov8/, runs it over your image, and pulls the instance masks. Then it filters them by class: it takes the class index from each bounding box, keeps only the instances whose class matches your class_id, and merges every remaining mask into one. The result is a single binary mask of "all the people in this image" (or cars, or whatever class you picked), plus the same plotted preview image the detection node produces.

That class filter is the whole idea, and it's also the main thing to understand: class_id is a COCO class index, not a friendly name. 0 is person, 2 is car, and so on - the values are baked into the model's training, so a stock YOLOv8 model will happily label a bus as class 5 and a cat as 15. If you want something specific that the 80 COCO classes don't cover, you need a fine-tuned model that has your target as one of its classes.

The inputs that matter

  • image - a single IMAGE tensor. Same caveat as the detection node: it squeezes the batch dimension, so feed it one image at a time.
  • model_name - dropdown of .pt files in models/yolov8/. Use a -seg model (like yolov8s-seg.pt). The node never checks - but a plain detection model returns no masks, and the mask output comes back empty.
  • class_id - the COCO class index to keep. Default 0 (person). This is the one you'll actually fiddle with.

Outputs: the annotated IMAGE and the MASK, which is the one you wire onward - straight into a masked inpaint node, a MaskToImage, or a composite.

Installing it

Same as the detection node - Manager (search "ComfyUI Yolov8") or:

cd ComfyUI/custom_nodes
git clone https://github.com/zcfrank1st/Comfyui-Yolov8

Drop a segmentation model into ComfyUI/models/yolov8/, restart, and then remember the bit the README forgets to mention: the pack ships no requirements.txt, so you must install ultralytics into ComfyUI's Python yourself before anything runs.

ComfyUI\python_embeded\python.exe -m pip install ultralytics

Where people get burned

  • Wrong model, empty mask. The most common "it doesn't work" - you fed it yolov8n.pt and got an empty MASK because detection models don't produce masks. Use a -seg weight.
  • Wrong class, also empty mask. If nothing in the image matches your class_id, the empty-tensor merge throws. And if you left it at 0 on a non-COCO fine-tuned model, "person" may not be class 0 at all.
  • The mask is 0/255, not 0–1. The node outputs the mask multiplied to 255 as integers instead of the float [0,1] ComfyUI usually expects. Most mask nodes tolerate it, but anything that does math on the mask directly (blur, feather, arithmetic) can behave oddly - expect to normalize it.
  • Hard edges. This is a YOLO polygon mask - great for subject extraction, mediocre for hair and anything semi-transparent. If you need a clean cutout of a person against a busy background, BiRefNet or SAM are the modern tools; YOLO is the fast, class-driven middle ground. It's the same tradeoff that keeps ADetailer's detection layer on YOLO while background-removal folks moved to matting models.

One light aside: everything in this pack reloads its model from disk on every single execution, so the first run after changing weights has a little hiccup. It's the price of "deadly simple." For one-off masking, that's a fair trade.

Categoryyolov8

Inputs (3)

NameTypeDefaultDescription
imageIMAGE
model_nameCOMBO0 options:
class_idINT0

Outputs (2)

NameTypeDescription
IMAGEIMAGE
MASKMASK