Spaces:
Running
Fix scene-harmonisation regressions: quality scores, skin-tone reversal, long-hair gap
Browse filesThree bugs in the harmonise pass (step 8) and its wiring:
1. Grain was injected BEFORE compute_quality_score ran on the same image, so
the deliberate noise fed straight into the naturalness noise-penalty and the
blend discontinuity term — the "make it look photographed" step was lowering
the app's own reported quality on every swap. Quality + alignment are now
scored first; harmonise still runs last before the output is encoded, so the
grain survives into both the preview and the 4K download.
2. The colour cast sampled scene_mean over the WHOLE target frame, which still
contains the original target face (target is never overwritten by the swap).
That dragged the head's colour back toward the tone of the face it replaced,
partially undoing match_skin_to_source one step earlier. It now samples only
outside the head mask, and works in LAB with the same darken-only L rule as
_match_lighting, so a bright background can't wash hair/skin to grey/silver.
3. The mask reached only 1.2x face-height below the chin while swap_hair places
hair as far as 5.0x, so the lower half of long transplanted hair never got
grained and kept the GAN-smooth look. Extended to 3.0x.
Both internal except blocks swallowed errors silently; they now log like every
other function in the file.
Files changed (2)
- core/head_swap.py+32-12
- web_app.py+13-9
531 unmodified lines5325335345355365375385395405365415425435445 unmodified lines550551552548549550551553554555556557558559560561562563564565554555556557558559560566567568569570571572573574575576577578579580581582583531 unmodified lines x1, y1, x2, y2 = [int(v) for v in face_bbox] bw, bh = x2 - x1, y2 - y1 mask = np.zeros((h, w), np.float32) # Down reach covers hair past the shoulders (~mid-torso). At the old 1.2 the # lower half of long transplanted hair fell outside the mask and kept the # GAN-smooth look this function exists to remove; swap_hair itself clamps # hair as far as tbh*5.0, but masking that far mostly grains background in # wide shots, so this stops short of it. rx1 = max(0, x1 - int(bw * 1.25)); ry1 = max(0, y1 - int(bh * 1.6)) rx2 = min(w, x2 + int(bw * 1.25)); ry2 = min(h, y2 + int(bh * 1.2)) rx2 = min(w, x2 + int(bw * 1.25)); ry2 = min(h, y2 + int(bh * 3.0)) mask[ry1:ry2, rx1:rx2] = 1.0 mask = cv2.GaussianBlur(mask, (0, 0), max(2.0, min(h, w) * 0.02)) m3 = mask[..., None]5 unmodified lines 2.5, 8.0)) * grain noise = np.random.randn(h, w).astype(np.float32) * grain_std res = res + noise[..., None] * m3 except Exception: pass
# (2) gentle colour-cast harmonisation: shift the head's mean toward the scene's except Exception as e: print(f"[head_swap] harmonize grain skipped: {e}")
# (2) gentle colour-cast harmonisation: shift the head's mean toward the # scene's — sampled OUTSIDE the head mask (the surrounding scene), never from # inside it. `target` still shows the ORIGINAL target face (never overwritten # by the swap), so sampling the whole frame would pull the head's colour back # toward the target's own skin tone, undoing match_skin_to_source which runs # immediately before this step. LAB's L channel is darken-only, mirroring # _match_lighting's rule, so this can't wash hair/skin out to grey/silver # against a bright background. try: sel = mask > 0.4 if int(sel.sum()) > 200: head_mean = res[sel].reshape(-1, 3).mean(0) scene_mean = target.astype(np.float32).reshape(-1, 3).mean(0) cast = (scene_mean - head_mean) * 0.12 # subtle res = res + cast[None, None, :] * m3 except Exception: pass bg_sel = mask < 0.05 if int(sel.sum()) > 200 and int(bg_sel.sum()) > 200: head_lab = cv2.cvtColor(np.clip(res, 0, 255).astype(np.uint8), cv2.COLOR_BGR2LAB).astype(np.float32) scene_lab = cv2.cvtColor(target, cv2.COLOR_BGR2LAB).astype(np.float32) head_mean = head_lab[sel].reshape(-1, 3).mean(0) scene_mean = scene_lab[bg_sel].reshape(-1, 3).mean(0) cast = np.zeros(3, np.float32) cast[0] = min(0.0, scene_mean[0] - head_mean[0]) * 0.35 # L: darken-only cast[1:] = (scene_mean[1:] - head_mean[1:]) * 0.12 # a/b: gentle nudge out_lab = head_lab + cast[None, None, :] * m3 res = cv2.cvtColor(np.clip(out_lab, 0, 255).astype(np.uint8), cv2.COLOR_LAB2BGR).astype(np.float32) except Exception as e: print(f"[head_swap] harmonize colour-cast skipped: {e}")
return np.clip(res, 0, 255).astype(np.uint8)
804 unmodified lines80580680780880981081181281381481581680880981081181281381430 unmodified lines845846847848849850851852853854855856857858859860804 unmodified lines except Exception as e: print(f"[swap] skin tone match skipped: {e}")
# 8. HARMONISE — make the swapped head look PHOTOGRAPHED WITH the scene # (not pasted on it): add the photo's grain over the GAN-smooth face/hair # and gently match its colour cast. This is the last visual step, so the # grain isn't smoothed away by anything after it. try: swapped = harmonize_to_scene(swapped, target, faces_tgt[0], grain=0.9) except Exception as e: print(f"[swap] harmonize skipped: {e}")
# -- quality metrics -------------------------------------------------- # Scored BEFORE step 8 below: that step adds deliberate grain/noise for # visual realism, which would otherwise inflate the noise/discontinuity # penalties in compute_quality_score and understate the swap's real quality. quality = compute_quality_score(swapped, target, None, None)
# Real alignment: how closely the swapped face's 5 landmarks sit on the30 unmodified lines except Exception as e: print(f"[swap] alignment metric skipped: {e}")
# 8. HARMONISE — make the swapped head look PHOTOGRAPHED WITH the scene # (not pasted on it): add the photo's grain over the GAN-smooth face/hair # and gently match its colour cast. Runs AFTER quality scoring (so the # added grain doesn't skew the naturalness/blend metrics) but still last # before the output is encoded, so nothing smooths the grain away. try: swapped = harmonize_to_scene(swapped, target, faces_tgt[0], grain=0.9) except Exception as e: print(f"[swap] harmonize skipped: {e}")
# -- 4K upscale for download (RealESRGAN x4, Lanczos fallback) -------- hi_res = upscale_image(swapped, scale=4)