Nodes/comfyui_sunxAI_facetools/Detect Face By Index
ComfyUI Node

Detect Face By Index

'the second woman from the left', automated

By upseem·Created about a year ago·Updated 11 months ago· 15
Detect Face By Index
  • image
  • mask
  • faces
  • has_face
threshold0.50
min_size64
max_size512
face_index0
gender_filter0
priority_mode0

The pack's plain DetectFaces node just grabs the biggest face in the frame. Detect Face By Index is the smarter sibling: it finds all the faces, then lets you pick one by position - 0 = leftmost, 1 = second from the left, and so on - with an optional gender filter on top. If you've ever tried to run InstantID or a face-restore pass on a group photo and watched it latch onto the wrong person, this is the node that fixes it.

It's from the comfyui_sunxAI_facetools pack by Sunx.ai, an InstantID-flavored face toolkit, and it's basically the workflow's "who am I actually targeting" decision, promoted to a node.

How it works - two detectors, one answer. Face detection here is a two-stage affair. First a YOLO model (face_yolov8m.pt, loaded from ComfyUI/models/ultralytics/bbox/) finds faces and filters them by your size and confidence limits. Then, separately, the node runs InsightFace on the original image to assign a gender to each detected face. The faces get sorted left-to-right by x-coordinate, and then your two knobs decide the winner:

  • face_index (0–10, default 0) - which face by position
  • gender_filter (0 = any, 1 = men only, 2 = women only)
  • priority_mode (0 = index-first: pick the position, then check gender; 1 = gender-first: filter by gender, then pick the position). With index-first, a mismatch means you get nothing; with gender-first, you get the first matching-gender face at that index.

The other inputs do the boring but important work: threshold (0–1, default 0.5 - higher is stricter), min_size and max_size (defaults 64 and 512, in pixels, filters that throw out faces that are too small or too big), and an optional mask - detection only runs on the unmasked part of the image, which is useful when you know exactly where the subject is.

Outputs. Two: faces (type FACE), which wires into the pack's CropFaces / face-handling nodes, and has_face (BOOLEAN), which is the signal the rest of the pack lives on - it feeds the has_face inputs of VAE Encode New, VAE Decode New, and the color adjust nodes, letting them skip work when nobody's there.

The honest caveats. Gender detection is heuristic, not gospel. The code itself falls back to a guess - if YOLO and InsightFace disagree on how many faces exist, it labels the leftmost face "man" and works from there - which is exactly as reliable as it sounds. It's fine for "pick the woman in the group shot," less fine for anything where a mislabel ruins the output. Also, the InsightFace pass is the pack's heaviest dependency: it's the notoriously painful-to-install one, and it auto-downloads model files on first run (the default buffalo_l pack, 300MB-ish, pulled into ~/.insightface/models/).

Install & issues. Whole-pack install: ComfyUI Manager → search comfyui_sunxAI_facetools, or:

cd ComfyUI/custom_nodes
git clone https://github.com/upseem/comfyui_sunxAI_facetools

then restart. Beyond the heavy requirements (insightface, onnxruntime, ultralytics, mediapipe, transformers), the YOLO face model and the fan2_68_landmark.onnx landmark file are expected under ComfyUI/models/ but aren't auto-downloaded by this pack and barely mentioned in its README - if you've set up stock InstantID before, you already have them. People have hit genuine install pain with this pack in the wild, mostly InsightFace-related, so if the node errors on load, start there.

It's the node you reach for when "the biggest face" isn't the face you want. Pick your person, then let the rest of the pack do its thing.

CategorysunxAI_facetools

Inputs (8)

NameTypeDefaultDescription
imageIMAGE
thresholdFLOAT0.500–1人脸检测置信度阈值,越高越严格
min_sizeINT64最小人脸尺寸,过滤掉太小的检测结果
max_sizeINT512最大人脸尺寸,过滤掉太大的检测结果
face_indexINT00–10人脸索引:0=最左边第一个,1=第二个,以此类推
gender_filterINT00–2性别筛选:0=任意性别,1=只检测男性(man),2=只检测女性(woman)
priority_modeINT00–1优先级模式:0=下标优先(先选位置再检查性别),1=性别优先(先筛选性别再选位置)
maskoptMASK

Outputs (2)

NameTypeDescription
facesFACE
has_faceBOOLEAN