Nodes/ComfyUI-Sapiens2/Sapiens2 Seg Extract Class
ComfyUI Node

Sapiens2 Seg Extract Class

Pull one body part out of Sapiens2's segmentation

By kijai·Created 4 months ago·Updated 4 months ago· 65
Sapiens2 Seg Extract Class
  • class_id_mask
  • mask
class_nameFace_Neck
invertfalse
num_classes29

This is the node you reach for five minutes after you run Sapiens2 Seg for the first time and think "okay, now I want just the shirt." It turns the normalized class mask into a clean binary mask for exactly one class - Apparel, Hair, Face_Neck, Left_Hand, whichever you pick - and hands you something you can wire straight into an inpaint, a recoloring pass, or a compositing node.

How it works

You feed it the class_id_mask output from Sapiens2 Body-Part Segmentation. The class ID is baked into each pixel as class_id / (num_classes-1), and this node reverses that math: multiply back up, round, and compare against the class you chose. The inputs:

  • class_name (default Face_Neck) - dropdown of all 29 Sapiens2 classes. This is the one you'll actually touch.
  • num_classes (default 29) - how many classes the source segmentation used. The Sapiens2 standard is 29 (Goliath-28 plus Eyeglass), and the default is correct for this pack's own Seg node - leave it alone unless you're feeding a mask from somewhere else.
  • invert (default off) - flips the resulting mask. One click to get "everything except the shirt" instead of "just the shirt."

Output is a single mask (MASK), binary, ready to drop into a mask input anywhere in ComfyUI.

The workflow it completes

Sapiens2 Seg → Sapiens2SegExtract (class = "Apparel") → mask
                                                        └→ Set Latent Noise Mask → KSampler   (recolor the outfit)
                                                        └→ INPAINT (redraw only the shirt)

Real talk on where it shines: recoloring or restyling clothing without touching the skin is the crowd-pleaser - Apparel or Upper_Clothing gives you an outfit mask that hugs the silhouette, and because it's a segmentation polygon rather than a rectangle, seams stay invisible. Hair masking is the other big one: Hair + an inpaint lets you change hairstyle while the face stays untouched.

The gotcha is subtle and worth internalizing: the mask values only mean something relative to num_classes. If you ever feed this node a mask that didn't come from Sapiens2's seg output, or you change num_classes, the rounding quietly lands on the wrong class. Also - the classic misread - a low-confidence segmentation isn't something this node can fix; if Apparel comes out jagged, go back and feed Sapiens2 Seg a cleaner image or use the colored output to see what the model actually believed. This node extracts; it doesn't repair.

Installing

Same as the rest of the pack: Manager search "ComfyUI-Sapiens2" or git clone https://github.com/kijai/ComfyUI-Sapiens2 into custom_nodes, restart. It's pure math over a mask - no model, no checkpoint, no extra dependencies. The only "install" is loading a seg checkpoint for the upstream Seg node. Needs a recent ComfyUI because the pack targets the newer io.ComfyNode API, which is worth remembering whenever any node in this family refuses to appear.

CategorySapiens2

Inputs (4)

NameTypeDefaultDescription
class_id_maskMASKThe class_id_mask output from Sapiens2 Body-Part Segmentation — values are class_id / (num_classes-1).
class_nameCOMBOFace_Neck29 options: Background, Apparel, Eyeglass, Face_Neck, Hair, Left_Foot, +23
invertBOOLEANfalseInvert the resulting binary mask.
num_classesINT292–256Total number of classes in the source segmentation. Sapiens2 standard is 29 (Goliath-28 + Eyeglass).

Outputs (1)

NameTypeDescription
maskMASK