Update app.py
Browse files
app.py
CHANGED
|
@@ -611,37 +611,41 @@ def _refine_video_alpha(result_rgba: Image.Image, guide_rgb: np.ndarray) -> Imag
|
|
| 611 |
|
| 612 |
def _recover_video_mask_dropout(current: np.ndarray, raw: np.ndarray,
|
| 613 |
previous: Optional[np.ndarray]) -> tuple:
|
| 614 |
-
"""
|
| 615 |
|
| 616 |
-
|
| 617 |
-
Silueta
|
| 618 |
-
|
| 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 |
-
|
| 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 |
-
|
| 637 |
-
|
| 638 |
-
candidate = near_previous & (raw >= 55) & (current < 128)
|
| 639 |
-
if not np.any(candidate):
|
| 640 |
return current, False
|
| 641 |
-
|
| 642 |
-
#
|
| 643 |
-
|
| 644 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 645 |
|
| 646 |
|
| 647 |
def _reset_session(session_id: str) -> bool:
|
|
@@ -1538,8 +1542,11 @@ def _process_frames_sync(raw_frames, resolved_model, transparent,
|
|
| 1538 |
arr[:, :, 3], raw_alpha, previous_alpha)
|
| 1539 |
arr[:, :, 3] = recovered
|
| 1540 |
result = Image.fromarray(arr, "RGBA")
|
| 1541 |
-
#
|
| 1542 |
-
|
|
|
|
|
|
|
|
|
|
| 1543 |
previous_alpha = recovered.copy()
|
| 1544 |
blended = False
|
| 1545 |
if active_session is not None:
|
|
@@ -1563,7 +1570,9 @@ def _process_frames_sync(raw_frames, resolved_model, transparent,
|
|
| 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]})
|
|
|
|
| 611 |
|
| 612 |
def _recover_video_mask_dropout(current: np.ndarray, raw: np.ndarray,
|
| 613 |
previous: Optional[np.ndarray]) -> tuple:
|
| 614 |
+
"""Protect legitimate foreground against destructive refinement.
|
| 615 |
|
| 616 |
+
Never paste a previous frame into a moving subject: use only CURRENT-frame
|
| 617 |
+
Silueta confidence. Previous alpha is a diagnostic reference, not pixels
|
| 618 |
+
to composite. A truly missing raw mask cannot be reconstructed here.
|
| 619 |
"""
|
| 620 |
+
h, w = current.shape
|
| 621 |
+
raw_area = int(np.count_nonzero(raw >= 128))
|
| 622 |
+
refined_area = int(np.count_nonzero(current >= 128))
|
| 623 |
+
# The current model detected foreground that refinement threw away.
|
| 624 |
+
# Recover from this frame's original alpha, including disconnected limbs.
|
| 625 |
+
if raw_area > h * w * 0.015 and refined_area < raw_area * 0.78:
|
| 626 |
+
logger.warning('[video-mask] postprocess dropout: raw=%d refined=%d; restoring raw',
|
| 627 |
+
raw_area, refined_area)
|
| 628 |
+
return raw.copy(), True
|
| 629 |
if previous is None or previous.shape != current.shape:
|
| 630 |
return current, False
|
|
|
|
| 631 |
prev_area = int(np.count_nonzero(previous >= 128))
|
| 632 |
+
if prev_area < h * w * 0.02 or raw_area < h * w * 0.008:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 633 |
return current, False
|
| 634 |
+
ratio = raw_area / max(prev_area, 1)
|
| 635 |
+
if ratio >= 0.62:
|
|
|
|
|
|
|
| 636 |
return current, False
|
| 637 |
+
# Raw inference itself shrank. Previous mask must not be blindly copied;
|
| 638 |
+
# retaining current raw values avoids edge clipping, not model dropout.
|
| 639 |
+
prev_core = previous >= 160
|
| 640 |
+
cur_core = raw >= 160
|
| 641 |
+
overlap = np.count_nonzero(prev_core & cur_core) / max(1, np.count_nonzero(cur_core))
|
| 642 |
+
logger.warning('[video-mask] raw model dropout: previous=%d raw=%d ratio=%.3f overlap=%.3f',
|
| 643 |
+
prev_area, raw_area, ratio, overlap)
|
| 644 |
+
# Only relax cleanup if there is evidence of a shared, relatively static subject.
|
| 645 |
+
if overlap >= 0.70:
|
| 646 |
+
restored = np.maximum(current, np.where(raw >= 35, raw, 0).astype(np.uint8))
|
| 647 |
+
return restored, True
|
| 648 |
+
return current, False
|
| 649 |
|
| 650 |
|
| 651 |
def _reset_session(session_id: str) -> bool:
|
|
|
|
| 1542 |
arr[:, :, 3], raw_alpha, previous_alpha)
|
| 1543 |
arr[:, :, 3] = recovered
|
| 1544 |
result = Image.fromarray(arr, "RGBA")
|
| 1545 |
+
# Diagnostic metrics make the failure stage observable in server logs.
|
| 1546 |
+
raw_fraction = round(float(np.count_nonzero(raw_alpha >= 128)) / raw_alpha.size, 4)
|
| 1547 |
+
final_fraction = round(float(np.count_nonzero(recovered >= 128)) / recovered.size, 4)
|
| 1548 |
+
# Never use a suspect or recovered mask as a temporal reference.
|
| 1549 |
+
if not did_recover and np.count_nonzero(recovered >= 128) >= recovered.size * 0.015:
|
| 1550 |
previous_alpha = recovered.copy()
|
| 1551 |
blended = False
|
| 1552 |
if active_session is not None:
|
|
|
|
| 1570 |
results.append({"data":base64.b64encode(buf.getvalue()).decode("ascii"),
|
| 1571 |
"format":fmt,"size":buf.tell(),
|
| 1572 |
"model":used_model,"smoothed":blended,
|
| 1573 |
+
"matting":used_matting,"dropout_recovered":did_recover,
|
| 1574 |
+
"raw_foreground_fraction":raw_fraction,
|
| 1575 |
+
"final_foreground_fraction":final_fraction})
|
| 1576 |
except Exception as e:
|
| 1577 |
logger.error(f"[frames-batch] frame {idx} failed: {e}")
|
| 1578 |
results.append({"data":"","format":"png","error":str(e)[:120]})
|