devkunalnaik commited on
Commit
b3255f0
Β·
1 Parent(s): e11e479

Feature: Laplacian pyramid edge blending + progress bars for image swap

Browse files
Files changed (2) hide show
  1. app.py +5 -2
  2. processors/face_swap.py +90 -7
app.py CHANGED
@@ -38,7 +38,7 @@ def _get_body_swapper():
38
 
39
  # ── Processing functions ─────────────────────────────────────────────────────
40
 
41
- def face_swap_image(source_img, target_img, enhance):
42
  if source_img is None or target_img is None:
43
  return None, "Please upload both a source and a target image."
44
  try:
@@ -46,21 +46,24 @@ def face_swap_image(source_img, target_img, enhance):
46
  pil_to_bgr(source_img),
47
  pil_to_bgr(target_img),
48
  enhance=enhance,
 
49
  )
50
  return (bgr_to_pil(result) if result is not None else None), msg
51
  except Exception as e:
52
  return None, f"Error: {e}"
53
 
54
 
55
- def body_swap_image(source_img, target_img, blend_strength):
56
  if source_img is None or target_img is None:
57
  return None, "Please upload both a source and a target image."
58
  try:
 
59
  result, msg = _get_body_swapper().swap(
60
  pil_to_bgr(source_img),
61
  pil_to_bgr(target_img),
62
  blend_strength=blend_strength,
63
  )
 
64
  return (bgr_to_pil(result) if result is not None else None), msg
65
  except Exception as e:
66
  return None, f"Error: {e}"
 
38
 
39
  # ── Processing functions ─────────────────────────────────────────────────────
40
 
41
+ def face_swap_image(source_img, target_img, enhance, progress=gr.Progress()):
42
  if source_img is None or target_img is None:
43
  return None, "Please upload both a source and a target image."
44
  try:
 
46
  pil_to_bgr(source_img),
47
  pil_to_bgr(target_img),
48
  enhance=enhance,
49
+ progress_cb=lambda v, m: progress(v, desc=m),
50
  )
51
  return (bgr_to_pil(result) if result is not None else None), msg
52
  except Exception as e:
53
  return None, f"Error: {e}"
54
 
55
 
56
+ def body_swap_image(source_img, target_img, blend_strength, progress=gr.Progress()):
57
  if source_img is None or target_img is None:
58
  return None, "Please upload both a source and a target image."
59
  try:
60
+ progress(0.05, desc="Loading segmentation model…")
61
  result, msg = _get_body_swapper().swap(
62
  pil_to_bgr(source_img),
63
  pil_to_bgr(target_img),
64
  blend_strength=blend_strength,
65
  )
66
+ progress(1.0, desc=msg)
67
  return (bgr_to_pil(result) if result is not None else None), msg
68
  except Exception as e:
69
  return None, f"Error: {e}"
processors/face_swap.py CHANGED
@@ -346,6 +346,72 @@ class FaceSwapper:
346
 
347
  return result
348
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
349
  # ── Public API ────────────────────────────────────────────────────────────
350
 
351
  def swap(
@@ -353,19 +419,25 @@ class FaceSwapper:
353
  source_bgr: np.ndarray,
354
  target_bgr: np.ndarray,
355
  enhance: bool = True,
 
356
  ):
357
  """
358
  Swap the first detected face in *source_bgr* onto every face in
359
- *target_bgr*.
 
 
360
 
361
  Returns:
362
  (result_bgr, status_message)
363
  """
 
 
 
 
364
  self._init()
 
365
 
366
  try:
367
- # Preserve full resolution up to 2048px on the longest side.
368
- # Going beyond that adds little visible quality on CPU but multiplies time.
369
  MAX_DIM = 2048
370
  orig_h, orig_w = target_bgr.shape[:2]
371
  scale_down = 1.0
@@ -378,6 +450,7 @@ class FaceSwapper:
378
  )
379
 
380
  source_faces = self._app.get(source_bgr)
 
381
  target_faces = self._app.get(target_bgr)
382
 
383
  if not source_faces:
@@ -385,26 +458,36 @@ class FaceSwapper:
385
  if not target_faces:
386
  return None, "No face detected in target image."
387
 
388
- source_face = source_faces[0]
389
- result = target_bgr.copy()
 
 
390
 
391
  for tgt_face in target_faces:
392
  result = self._swapper.get(
393
  result, tgt_face, source_face, paste_back=True
394
  )
395
 
396
- # CodeFormer ONNX + ESPCN super-res + CLAHE (falls back to OpenCV if unavailable)
 
 
 
 
 
397
  if enhance:
 
398
  result = self._enhance_codeformer(result, target_faces)
399
 
400
- # If we downscaled, upscale back to original resolution with Lanczos
401
  if scale_down < 1.0:
 
402
  result = cv2.resize(
403
  result,
404
  (orig_w, orig_h),
405
  interpolation=cv2.INTER_LANCZOS4,
406
  )
407
 
 
408
  return result, f"Swapped {len(target_faces)} face(s) successfully."
409
 
410
  except Exception as exc:
 
346
 
347
  return result
348
 
349
+ # ── Laplacian pyramid blending ────────────────────────────────────────────
350
+
351
+ @staticmethod
352
+ def _face_ellipse_mask(shape: tuple, faces, expand: float = 0.35) -> np.ndarray:
353
+ """
354
+ Soft elliptical mask covering all detected face regions.
355
+ 255 = use swapped face, 0 = use original background.
356
+ """
357
+ mask = np.zeros(shape[:2], dtype=np.uint8)
358
+ for face in faces:
359
+ box = face.bbox.astype(int)
360
+ x1 = max(box[0], 0); y1 = max(box[1], 0)
361
+ x2 = min(box[2], shape[1]); y2 = min(box[3], shape[0])
362
+ w, h = x2 - x1, y2 - y1
363
+ if w <= 0 or h <= 0:
364
+ continue
365
+ cx, cy = (x1 + x2) // 2, (y1 + y2) // 2
366
+ ax = int(w // 2 * (1 + expand))
367
+ ay = int(h // 2 * (1 + expand))
368
+ cv2.ellipse(mask, (cx, cy), (ax, ay), 0, 0, 360, 255, -1)
369
+ # Heavy Gaussian feather β€” wide transition = no visible seam
370
+ blur = max(31, min(mask.shape[:2]) // 10)
371
+ if blur % 2 == 0:
372
+ blur += 1
373
+ return cv2.GaussianBlur(mask, (blur, blur), 0)
374
+
375
+ @staticmethod
376
+ def _laplacian_blend(swapped: np.ndarray, original: np.ndarray,
377
+ mask: np.ndarray, levels: int = 6) -> np.ndarray:
378
+ """
379
+ Laplacian pyramid blending.
380
+ Blends swapped face region onto original at multiple spatial scales
381
+ so no hard edge is visible regardless of skin tone or lighting.
382
+
383
+ mask: uint8 single-channel, 255 = take from swapped, 0 = take from original.
384
+ """
385
+ A = swapped.astype(np.float32)
386
+ B = original.astype(np.float32)
387
+ M = (mask.astype(np.float32) / 255.0)
388
+ if M.ndim == 2:
389
+ M = M[:, :, np.newaxis]
390
+
391
+ # Build Gaussian pyramids
392
+ gA, gB, gM = [A], [B], [M]
393
+ for _ in range(levels):
394
+ gA.append(cv2.pyrDown(gA[-1]))
395
+ gB.append(cv2.pyrDown(gB[-1]))
396
+ gM.append(cv2.pyrDown(gM[-1]))
397
+
398
+ # Build Laplacian pyramids
399
+ lA, lB = [], []
400
+ for i in range(levels):
401
+ sz = (gA[i].shape[1], gA[i].shape[0])
402
+ lA.append(gA[i] - cv2.pyrUp(gA[i + 1], dstsize=sz))
403
+ lB.append(gB[i] - cv2.pyrUp(gB[i + 1], dstsize=sz))
404
+ lA.append(gA[levels])
405
+ lB.append(gB[levels])
406
+
407
+ # Blend each level, reconstruct coarse→fine
408
+ result = lA[levels] * gM[levels] + lB[levels] * (1.0 - gM[levels])
409
+ for i in range(levels - 1, -1, -1):
410
+ sz = (lA[i].shape[1], lA[i].shape[0])
411
+ result = cv2.pyrUp(result, dstsize=sz) + lA[i] * gM[i] + lB[i] * (1.0 - gM[i])
412
+
413
+ return np.clip(result, 0, 255).astype(np.uint8)
414
+
415
  # ── Public API ────────────────────────────────────────────────────────────
416
 
417
  def swap(
 
419
  source_bgr: np.ndarray,
420
  target_bgr: np.ndarray,
421
  enhance: bool = True,
422
+ progress_cb=None,
423
  ):
424
  """
425
  Swap the first detected face in *source_bgr* onto every face in
426
+ *target_bgr*. Applies Laplacian pyramid blending for seamless edges.
427
+
428
+ progress_cb: optional callable(fraction: float, label: str)
429
 
430
  Returns:
431
  (result_bgr, status_message)
432
  """
433
+ def _p(v, msg):
434
+ if progress_cb:
435
+ progress_cb(v, msg)
436
+
437
  self._init()
438
+ _p(0.1, "Models ready β€” detecting faces…")
439
 
440
  try:
 
 
441
  MAX_DIM = 2048
442
  orig_h, orig_w = target_bgr.shape[:2]
443
  scale_down = 1.0
 
450
  )
451
 
452
  source_faces = self._app.get(source_bgr)
453
+ _p(0.3, "Source face detected β€” scanning target…")
454
  target_faces = self._app.get(target_bgr)
455
 
456
  if not source_faces:
 
458
  if not target_faces:
459
  return None, "No face detected in target image."
460
 
461
+ _p(0.45, f"Swapping {len(target_faces)} face(s)…")
462
+ source_face = source_faces[0]
463
+ result = target_bgr.copy()
464
+ original_bgr = target_bgr.copy() # kept for Laplacian blend
465
 
466
  for tgt_face in target_faces:
467
  result = self._swapper.get(
468
  result, tgt_face, source_face, paste_back=True
469
  )
470
 
471
+ # ── Laplacian pyramid blending β€” removes hard boundary ─────────
472
+ _p(0.65, "Blending edges (Laplacian pyramid)…")
473
+ blend_mask = self._face_ellipse_mask(original_bgr.shape, target_faces)
474
+ result = self._laplacian_blend(result, original_bgr, blend_mask)
475
+
476
+ # ── CodeFormer enhancement (images only) ──────────────────────
477
  if enhance:
478
+ _p(0.80, "Enhancing quality (CodeFormer)…")
479
  result = self._enhance_codeformer(result, target_faces)
480
 
481
+ # ── Upscale back to original resolution ───────────────────────
482
  if scale_down < 1.0:
483
+ _p(0.95, "Upscaling to original resolution…")
484
  result = cv2.resize(
485
  result,
486
  (orig_w, orig_h),
487
  interpolation=cv2.INTER_LANCZOS4,
488
  )
489
 
490
+ _p(1.0, f"Done β€” {len(target_faces)} face(s) swapped.")
491
  return result, f"Swapped {len(target_faces)} face(s) successfully."
492
 
493
  except Exception as exc: