andevs commited on
Commit
cb097e5
·
verified ·
1 Parent(s): 4a9906c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -2
app.py CHANGED
@@ -609,6 +609,41 @@ def _refine_video_alpha(result_rgba: Image.Image, guide_rgb: np.ndarray) -> Imag
609
  return result_rgba
610
 
611
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
612
  def _reset_session(session_id: str) -> bool:
613
  if not session_id:
614
  return False
@@ -1459,6 +1494,7 @@ def _process_frames_sync(raw_frames, resolved_model, transparent,
1459
  fallback_chain = ['silueta'] # Keep video RAM predictable; no heavyweight fallback.
1460
  active_session = None # Disable frame-to-frame alpha ghosting on moving subjects.
1461
  results = []
 
1462
  for idx, content in enumerate(raw_frames):
1463
  try:
1464
  if not content:
@@ -1489,12 +1525,22 @@ def _process_frames_sync(raw_frames, resolved_model, transparent,
1489
  results.append({"data":"", "format":"png", "error":"all_models_failed"})
1490
  continue
1491
  if result.mode != "RGBA": result = result.convert("RGBA")
 
 
1492
  # v32.9.8: guided filter + soft contrast
1493
  try:
1494
  guide_rgb = np.array(img.convert("RGB"))
1495
  result = _refine_video_alpha(result, guide_rgb)
1496
  except Exception as e:
1497
  logger.warning(f"[frames-batch] alpha refine failed: {e}")
 
 
 
 
 
 
 
 
1498
  blended = False
1499
  if active_session is not None:
1500
  arr = np.array(result)
@@ -1517,8 +1563,7 @@ def _process_frames_sync(raw_frames, resolved_model, transparent,
1517
  results.append({"data":base64.b64encode(buf.getvalue()).decode("ascii"),
1518
  "format":fmt,"size":buf.tell(),
1519
  "model":used_model,"smoothed":blended,
1520
- "matting":used_matting})
1521
- del arr
1522
  except Exception as e:
1523
  logger.error(f"[frames-batch] frame {idx} failed: {e}")
1524
  results.append({"data":"","format":"png","error":str(e)[:120]})
 
609
  return result_rgba
610
 
611
 
612
+ def _recover_video_mask_dropout(current: np.ndarray, raw: np.ndarray,
613
+ previous: Optional[np.ndarray]) -> tuple:
614
+ """Conservative, within-batch dropout recovery; never copies opaque old pixels.
615
+
616
+ A previous mask is used only as a spatial hint for pixels which the *current*
617
+ Silueta inference still assigns some foreground probability. This prevents
618
+ replacing a moving subject with an old silhouette. No state survives batches.
619
+ """
620
+ if previous is None or previous.shape != current.shape:
621
+ return current, False
622
+ h, w = current.shape
623
+ prev_area = int(np.count_nonzero(previous >= 128))
624
+ cur_area = int(np.count_nonzero(current >= 128))
625
+ if prev_area < h*w*0.025 or cur_area < h*w*0.008:
626
+ return current, False
627
+ ratio = cur_area / max(1, prev_area)
628
+ if not (0.25 <= ratio < 0.63):
629
+ return current, False
630
+ prev_core = (previous >= 175).astype(np.uint8)
631
+ cur_core = (current >= 175).astype(np.uint8)
632
+ # Reject large subject motion; old outlines would cause ghosting.
633
+ intersection = np.count_nonzero((prev_core > 0) & (cur_core > 0))
634
+ if intersection / max(1, np.count_nonzero(cur_core)) < 0.65:
635
+ return current, False
636
+ # Recover only pixels already supported by current raw model confidence.
637
+ near_previous = cv2.dilate(prev_core, np.ones((7, 7), np.uint8)) > 0
638
+ candidate = near_previous & (raw >= 55) & (current < 128)
639
+ if not np.any(candidate):
640
+ return current, False
641
+ out = current.copy()
642
+ # A modest boost, never turn faint background evidence fully opaque.
643
+ out[candidate] = np.maximum(out[candidate], np.minimum(raw[candidate], 175))
644
+ return out, True
645
+
646
+
647
  def _reset_session(session_id: str) -> bool:
648
  if not session_id:
649
  return False
 
1494
  fallback_chain = ['silueta'] # Keep video RAM predictable; no heavyweight fallback.
1495
  active_session = None # Disable frame-to-frame alpha ghosting on moving subjects.
1496
  results = []
1497
+ previous_alpha = None
1498
  for idx, content in enumerate(raw_frames):
1499
  try:
1500
  if not content:
 
1525
  results.append({"data":"", "format":"png", "error":"all_models_failed"})
1526
  continue
1527
  if result.mode != "RGBA": result = result.convert("RGBA")
1528
+ # Keep raw Silueta confidence for conservative dropout recovery.
1529
+ raw_alpha = np.array(result.convert("RGBA"))[:, :, 3].copy()
1530
  # v32.9.8: guided filter + soft contrast
1531
  try:
1532
  guide_rgb = np.array(img.convert("RGB"))
1533
  result = _refine_video_alpha(result, guide_rgb)
1534
  except Exception as e:
1535
  logger.warning(f"[frames-batch] alpha refine failed: {e}")
1536
+ arr = np.array(result.convert("RGBA"))
1537
+ recovered, did_recover = _recover_video_mask_dropout(
1538
+ arr[:, :, 3], raw_alpha, previous_alpha)
1539
+ arr[:, :, 3] = recovered
1540
+ result = Image.fromarray(arr, "RGBA")
1541
+ # Store only reliable frames as references, never a recovered frame.
1542
+ if not did_recover:
1543
+ previous_alpha = recovered.copy()
1544
  blended = False
1545
  if active_session is not None:
1546
  arr = np.array(result)
 
1563
  results.append({"data":base64.b64encode(buf.getvalue()).decode("ascii"),
1564
  "format":fmt,"size":buf.tell(),
1565
  "model":used_model,"smoothed":blended,
1566
+ "matting":used_matting,"dropout_recovered":did_recover})
 
1567
  except Exception as e:
1568
  logger.error(f"[frames-batch] frame {idx} failed: {e}")
1569
  results.append({"data":"","format":"png","error":str(e)[:120]})