Update app.py
Browse files
app.py
CHANGED
|
@@ -648,6 +648,52 @@ def _recover_video_mask_dropout(current: np.ndarray, raw: np.ndarray,
|
|
| 648 |
return current, False
|
| 649 |
|
| 650 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 651 |
def _reset_session(session_id: str) -> bool:
|
| 652 |
if not session_id:
|
| 653 |
return False
|
|
@@ -1531,6 +1577,18 @@ def _process_frames_sync(raw_frames, resolved_model, transparent,
|
|
| 1531 |
if result.mode != "RGBA": result = result.convert("RGBA")
|
| 1532 |
# Keep raw Silueta confidence for conservative dropout recovery.
|
| 1533 |
raw_alpha = np.array(result.convert("RGBA"))[:, :, 3].copy()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1534 |
# v32.9.8: guided filter + soft contrast
|
| 1535 |
try:
|
| 1536 |
guide_rgb = np.array(img.convert("RGB"))
|
|
@@ -1572,7 +1630,8 @@ def _process_frames_sync(raw_frames, resolved_model, transparent,
|
|
| 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]})
|
|
|
|
| 648 |
return current, False
|
| 649 |
|
| 650 |
|
| 651 |
+
def _retry_video_subject_crop(img: Image.Image, raw: np.ndarray, session) -> tuple:
|
| 652 |
+
"""Second Silueta view for frames where full-scene inference sees only a fragment.
|
| 653 |
+
|
| 654 |
+
The cat's brown torso blends with the chair in the supplied test video.
|
| 655 |
+
A lower-scene crop changes Silueta's scale/context, without loading another
|
| 656 |
+
model. Reject implausible proposals rather than adding chair to the mask.
|
| 657 |
+
"""
|
| 658 |
+
h, w = raw.shape
|
| 659 |
+
area = int(np.count_nonzero(raw >= 128))
|
| 660 |
+
fraction = area / float(h * w)
|
| 661 |
+
if fraction >= 0.115 or fraction < 0.008 or session is None:
|
| 662 |
+
return raw, 'not_needed'
|
| 663 |
+
# Two views, but stop after first plausible improvement; only for suspect frames.
|
| 664 |
+
views = [(0, int(h * .18), w, h),
|
| 665 |
+
(int(w * .07), int(h * .12), int(w * .93), h)]
|
| 666 |
+
for x0, y0, x1, y1 in views:
|
| 667 |
+
if x1-x0 < 48 or y1-y0 < 48: continue
|
| 668 |
+
try:
|
| 669 |
+
crop = img.crop((x0,y0,x1,y1))
|
| 670 |
+
candidate, _ = process_with_neural(crop, session, 'silueta',
|
| 671 |
+
force_no_matting=True,
|
| 672 |
+
use_alpha_matting=False)
|
| 673 |
+
if candidate is None: continue
|
| 674 |
+
a = np.asarray(candidate.convert('RGBA'))[:, :, 3]
|
| 675 |
+
if a.shape != (y1-y0, x1-x0):
|
| 676 |
+
a = cv2.resize(a, (x1-x0,y1-y0), interpolation=cv2.INTER_LINEAR)
|
| 677 |
+
proposed = np.zeros_like(raw)
|
| 678 |
+
proposed[y0:y1,x0:x1] = a
|
| 679 |
+
proposed_area = int(np.count_nonzero(proposed >= 128))
|
| 680 |
+
if not (area * 1.45 <= proposed_area <= min(h*w*.60, area*5.0)):
|
| 681 |
+
continue
|
| 682 |
+
# Must include the existing detected subject, not an unrelated chair.
|
| 683 |
+
old_core = raw >= 160
|
| 684 |
+
new_core = proposed >= 128
|
| 685 |
+
overlap = np.count_nonzero(old_core & new_core) / max(1, np.count_nonzero(old_core))
|
| 686 |
+
if overlap < .58: continue
|
| 687 |
+
# No hard seams: only merge inside crop, and preserve original fur.
|
| 688 |
+
merged = np.maximum(raw, proposed)
|
| 689 |
+
logger.info('[video-mask] crop retry accepted: raw=%.3f crop=%.3f overlap=%.2f',
|
| 690 |
+
fraction, proposed_area/(h*w), overlap)
|
| 691 |
+
return merged, 'crop_accepted'
|
| 692 |
+
except Exception as e:
|
| 693 |
+
logger.warning('[video-mask] crop retry failed: %s', str(e)[:120])
|
| 694 |
+
return raw, 'crop_rejected'
|
| 695 |
+
|
| 696 |
+
|
| 697 |
def _reset_session(session_id: str) -> bool:
|
| 698 |
if not session_id:
|
| 699 |
return False
|
|
|
|
| 1577 |
if result.mode != "RGBA": result = result.convert("RGBA")
|
| 1578 |
# Keep raw Silueta confidence for conservative dropout recovery.
|
| 1579 |
raw_alpha = np.array(result.convert("RGBA"))[:, :, 3].copy()
|
| 1580 |
+
retry_status = 'not_needed'
|
| 1581 |
+
try:
|
| 1582 |
+
# Rerun only suspicious frames; keep Silueta as the only video model.
|
| 1583 |
+
retry_session = get_session_for_model('silueta')
|
| 1584 |
+
improved_alpha, retry_status = _retry_video_subject_crop(img, raw_alpha, retry_session)
|
| 1585 |
+
if retry_status == 'crop_accepted':
|
| 1586 |
+
raw_alpha = improved_alpha
|
| 1587 |
+
_rgba = np.array(result.convert('RGBA'))
|
| 1588 |
+
_rgba[:, :, 3] = improved_alpha
|
| 1589 |
+
result = Image.fromarray(_rgba, 'RGBA')
|
| 1590 |
+
except Exception as e:
|
| 1591 |
+
logger.warning('[video-mask] retry skipped: %s', str(e)[:120])
|
| 1592 |
# v32.9.8: guided filter + soft contrast
|
| 1593 |
try:
|
| 1594 |
guide_rgb = np.array(img.convert("RGB"))
|
|
|
|
| 1630 |
"model":used_model,"smoothed":blended,
|
| 1631 |
"matting":used_matting,"dropout_recovered":did_recover,
|
| 1632 |
"raw_foreground_fraction":raw_fraction,
|
| 1633 |
+
"final_foreground_fraction":final_fraction,
|
| 1634 |
+
"crop_retry":retry_status})
|
| 1635 |
except Exception as e:
|
| 1636 |
logger.error(f"[frames-batch] frame {idx} failed: {e}")
|
| 1637 |
results.append({"data":"","format":"png","error":str(e)[:120]})
|