Spaces:
Running
Stop the seam blend from erasing the head swap
Browse filesWith the Head Swap toggle on, the output was indistinguishable from an ordinary
face swap. The head transplant ran, but step 3b then blended the swapped face
against `target` — the ORIGINAL scene — rather than against `base`, the image
the head swap had just produced. Everywhere outside the face mask, which is
exactly where the transplanted head shape and hair live, the result was pulled
back to the untouched target.
Measured on the Library target: full_head_swap changes the frame by mean 0.90,
but only 0.29 survived the blend — roughly two thirds of the effect discarded.
Blending against `base` retains 0.90, and end to end the toggle now changes
1.69% of pixels by more than 15 levels instead of producing the same picture.
`base` is `target` whenever the head swap is off, so nothing changes there. The
file's own comment already warned about this trap for the hair step; the same
one was left open for the head swap.
Also drops the composite feather from 0.025 to 0.012, which measured slightly
crisper at the hairline (1531 -> 1578 on a Laplacian-variance check).
The remaining softness is a property of the source photo, not the blend: the
head is warped by whatever scale maps the source's landmarks onto the target's,
so a smaller source head is upscaled — 1.43x here — and lands softer than the
photograph around it. Documented on full_head_swap rather than papered over.
14 tests pass.
Files changed (2)
- core/head_swap.py+9-1
- web_app.py+9-3
642 unmodified lines6436446456466476486492 unmodified lines652653654655656657642 unmodified linesdef full_head_swap(source: np.ndarray, target: np.ndarray,feather: float = 0.025):"""Transplant the SOURCE's whole head — face SHAPE + skin + hair (+ glasses) —onto the target's body/scene. Unlike InsightFace (which keeps the target's2 unmodified linesproportions; only the head region is touched, so the BACKGROUND is untouched.Best when source and target face roughly the same way. Returns the compositedimage, or None if it can't (caller falls back to the face swap)."""app = _get_insightface()if app is None:642 unmodified lines6436446456466476486492 unmodified lines652653654655656657658659660661662663664665642 unmodified linesdef full_head_swap(source: np.ndarray, target: np.ndarray,feather: float = 0.012):"""Transplant the SOURCE's whole head — face SHAPE + skin + hair (+ glasses) —onto the target's body/scene. Unlike InsightFace (which keeps the target's2 unmodified linesproportions; only the head region is touched, so the BACKGROUND is untouched.Best when source and target face roughly the same way. Returns the compositedimage, or None if it can't (caller falls back to the face swap).Sharpness is bounded by the SOURCE photo. The head is warped by whateverscale maps the source's eyes/nose/mouth onto the target's, so a source whosehead is smaller than the target's gets upscaled and comes out visibly softerthan the surrounding photograph — measured at roughly 1.43x upscale for a66x77 source face against a 102x116 target one. A close, sharp source photois what makes this look right; no amount of blending recovers detail thatwas never captured."""app = _get_insightface()if app is None:
887 unmodified lines888889890891892893894895896897898899900901902887 unmodified lines# 3b. Laplacian pyramid blend over the FACE-swap boundary — smooths the# seam left by InsightFace's paste_back. MUST run BEFORE the hair step:# it composites the swapped FACE over the target everywhere else, so if# it ran after the hair it would overwrite the newly-transferred hair# (which lies outside the face mask) with the target's original hair.note(50, "Blending the seam…")try:from core.segmentor import segment_hair_neck_skin_fmask = segment_hair_neck_skin(swapped).get("face_mask")if _fmask is not None and _fmask.max() > 0:swapped = laplacian_blend(swapped, target, _fmask, levels=4)except Exception as e:print(f"[swap] Laplacian blend skipped: {e}")887 unmodified lines888889890891892893894895896897898899900901902903904905906907908887 unmodified lines# 3b. Laplacian pyramid blend over the FACE-swap boundary — smooths the# seam left by InsightFace's paste_back. MUST run BEFORE the hair step:# it composites the swapped FACE over the base everywhere else, so if# it ran after the hair it would overwrite the newly-transferred hair# (which lies outside the face mask) with the base's original hair.## Blend against `base`, NOT `target`. They are the same image unless# the head swap ran, and when it did, blending against `target` threw# the transplanted head and hair away everywhere outside the face# mask — measured as erasing about two thirds of the head swap, which# is why head_swap=1 and head_swap=0 came out looking identical.note(50, "Blending the seam…")try:from core.segmentor import segment_hair_neck_skin_fmask = segment_hair_neck_skin(swapped).get("face_mask")if _fmask is not None and _fmask.max() > 0:swapped = laplacian_blend(swapped, base, _fmask, levels=4)except Exception as e:print(f"[swap] Laplacian blend skipped: {e}")