aditya-rAj19 commited on
Commit
ca509be
Β·
1 Parent(s): 8eed89d

Default to the clean face swap; make head-swap & hair-swap opt-in

Browse files

The head-swap-first reorder + HairFastGAN hair step degraded the clean face
swap that was already producing good results. Both are now OFF by default
(opt-in via head_swap=1 / swap_hair=1). Default pipeline: face swap (main face)
-> GFPGAN -> skin match -> glasses -> blend -> 4x upscale.

Files changed (1) hide show
  1. web_app.py +11 -15
web_app.py CHANGED
@@ -708,14 +708,14 @@ def api_swap():
708
  (src_tone["b"] - tgt_tone["b"]) ** 2
709
  ) ** 0.5
710
 
711
- # Pipeline order (as requested): HEAD swap -> FACE swap -> HAIR swap.
 
 
 
712
  #
713
- # 1. HEAD SWAP β€” transplant the SOURCE head shape + hair onto the target
714
- # when the pose allows. If it can't, we keep the plain target as the
715
- # base. (Previously, a weak head-swap result short-circuited the face
716
- # swap, leaving a poor result.)
717
  base = target
718
- if request.form.get("head_swap", "1") in ("1", "true", "on"):
719
  try:
720
  hs = full_head_swap(source, target)
721
  if hs is not None:
@@ -723,9 +723,8 @@ def api_swap():
723
  except Exception as e:
724
  print(f"[swap] head swap error: {e}")
725
 
726
- # 2. FACE SWAP β€” ALWAYS run. Lays the crisp SOURCE identity over the main
727
- # face (refines the soft head-swap warp, or swaps onto the target).
728
- # Only the main subject is swapped, never background/poster faces.
729
  swapped = swap_face_insightface(source, base)
730
 
731
  # 3. GFPGAN face restoration β€” recovers detail lost in the 128px swap.
@@ -737,12 +736,9 @@ def api_swap():
737
  swapped, source, faces_src[0], faces_tgt[0], strength=0.75
738
  )
739
 
740
- # 5. HAIR SWAP β€” transfer the SOURCE's real hairstyle. On GPU this uses
741
- # HairFastGAN (StyleGAN) for a photoreal result: it returns an FFHQ-
742
- # aligned portrait wearing the source hair, which we composite back
743
- # onto the swap via the hair-region mask. Falls back to the BiSeNet
744
- # warp when HairFastGAN isn't available (CPU / no weights / no token).
745
- if request.form.get("swap_hair", "1") in ("1", "true", "on"):
746
  hf_portrait = None
747
  try:
748
  hf_portrait = transfer_hair(face_bgr=swapped, shape_bgr=source,
 
708
  (src_tone["b"] - tgt_tone["b"]) ** 2
709
  ) ** 0.5
710
 
711
+ # Default pipeline = the clean, high-quality FACE swap (this is what
712
+ # produced the good results). HEAD swap and HAIR swap are OPT-IN only
713
+ # (?head_swap=1 / ?swap_hair=1) because, with the current models, they
714
+ # degrade the clean face swap more often than they help.
715
  #
716
+ # 1. (optional) HEAD SWAP β€” transplant the source head shape+hair.
 
 
 
717
  base = target
718
+ if request.form.get("head_swap", "0") in ("1", "true", "on"):
719
  try:
720
  hs = full_head_swap(source, target)
721
  if hs is not None:
 
723
  except Exception as e:
724
  print(f"[swap] head swap error: {e}")
725
 
726
+ # 2. FACE SWAP β€” the main event. Source identity onto the main face only
727
+ # (background/poster faces are never swapped).
 
728
  swapped = swap_face_insightface(source, base)
729
 
730
  # 3. GFPGAN face restoration β€” recovers detail lost in the 128px swap.
 
736
  swapped, source, faces_src[0], faces_tgt[0], strength=0.75
737
  )
738
 
739
+ # 5. (optional) HAIR SWAP β€” only when explicitly requested. HairFastGAN
740
+ # on GPU, else the BiSeNet warp. Off by default (can mangle the hair).
741
+ if request.form.get("swap_hair", "0") in ("1", "true", "on"):
 
 
 
742
  hf_portrait = None
743
  try:
744
  hf_portrait = transfer_hair(face_bgr=swapped, shape_bgr=source,