ComfyUI Node Runs on cloud

Crop Face

Crop Face, explained

By ArtBot2023·Created 3 years ago·Updated 2 years ago· 102
Crop Face
  • model
  • image
  • face_image
  • preview
  • bbox
confidence0.80
margin32

Every face swap starts the same way: you need to know exactly where the face is, so you can cut it out, replace it, and paste the new one back in the same spot. That's the job of Crop Face - the node that finds a face in your base image, crops it into a square around the face, and hands you the bounding box so the swap can be put back precisely later.

It's the entry point of the Character Face Swap pack's compositing half, and the companion to Uncrop Face. Crop pulls the face out; Uncrop blends the new one back in at the same coordinates. Think of the pair as the SD 1.5 answer to the "swap a character's face" problem - the pipeline that predates the IP-Adapter and InstantID era covered in the KB's identity-preservation essay. It's older-school, but it's also completely transparent: you control every crop, every mask, every paste.

How it works

Under the hood it runs RetinaFace (the ResNet50 detector from xinntao's facexlib library) on your image. RetinaFace doesn't just draw a box - it returns five facial landmarks (eyes, nose, mouth corners) and a confidence score per detection. Crop Face keeps detections above your confidence threshold, then does two things to the box:

  1. Adds a margin around the raw face box, so the crop includes some hairline and jaw instead of hugging the skin.
  2. Squares it up - it expands the shorter side to make the box square while keeping the center fixed. That matters because the rest of the pack assumes a square face crop.

The inputs that matter

  • model - plug in the RETINAFACE output from Load RetinaFace. One model instance can feed as many Crop Face nodes as you want.
  • confidence (default 0.8) - the detection score cutoff. If you're getting false positives (a face-shaped sofa, a face in the background), bump it toward 0.9. If real faces are being missed, drop it to 0.6–0.7.
  • margin (default 32) - pixels of padding around the face before squaring. 32 is fine for a 512px image; go bigger if the crop feels claustrophobic, but remember the crop is what gets regenerated, so tighter usually means a cleaner swap.

Wiring the outputs

Crop Face gives you three things:

  • face_image - the cropped face. This is what you'll feed into img2img / ControlNet to regenerate as your target character.
  • preview - the full image with the detected box and the five landmarks drawn on it. Wire this to a PreviewImage and glance at it before queuing: it's your sanity check that RetinaFace found the right face.
  • bbox - the pack's custom BBOX type: (x, y, w, h) of the final squared crop. Save it and feed it to Uncrop Face at the end, or to Image Full BBox / Mask Contour in the shipped workflow.

One honest warning: the node returns only the first detected face. Multi-person photos pick one face and silently ignore the rest. If you need per-face crops, you'll want a batch-oriented detector instead.

Installing the pack

Character Face Swap installs like any custom node. Easiest is ComfyUI Manager: search Character Face Swap, install, restart. Or manually:

cd ComfyUI/custom_nodes/
git clone https://github.com/ArtBot2023/CharacterFaceSwap.git
cd CharacterFaceSwap
python install.py          # on the bundled Windows build: ../../../python_embeded/python install.py

That install.py pip-installs the bundled facexlib library, which this node imports directly - skip it and you'll hit No module named 'facexlib' on first run. The RetinaFace weights (detection_Resnet50_Final.pth) auto-download from xinntao's facexlib GitHub release into ComfyUI/models/facexlib/ the first time you load the model, so no manual model step.

Common problems

  • "no face detected" in the console, or a weird crop: the node's fallback when RetinaFace finds nothing is to pass the whole image through as the "crop" with a zeroed bbox. Check the preview before doing anything downstream - if it shows no box, lower confidence.
  • Face detected but crop is off-center: RetinaFace's box can drift on heavily tilted heads. The squared box keeps the detection center, not necessarily the face's true center - if it's noticeably off, re-crop with a larger margin.
  • It's slow on first use: the ResNet50 detector is a chunkier model and the weights download happens lazily. Subsequent runs are fast.

The one-liner: Crop Face answers "where is the face, and can I have a clean square of it?" - everything else in this pack is what you do after that answer.

CategoryCFaceSwap

Inputs (4)

NameTypeDefaultDescription
modelRETINAFACE
imageIMAGE
confidenceFLOAT0.800–1
marginINT32

Outputs (3)

NameTypeDescription
face_imageIMAGE
previewIMAGE
bboxBBOX