Update utils/cv_processing.py
Browse files- utils/cv_processing.py +28 -1
utils/cv_processing.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
cv_processing.py 路 FIXED VERSION with proper SAM2 handling + MatAnyone stateful integration
|
|
|
|
| 4 |
|
| 5 |
All public functions in this module expect RGB images (H,W,3) unless stated otherwise.
|
| 6 |
CoreVideoProcessor already converts BGR鈫扲GB before calling into this module.
|
|
@@ -8,6 +9,7 @@
|
|
| 8 |
|
| 9 |
from __future__ import annotations
|
| 10 |
|
|
|
|
| 11 |
import logging
|
| 12 |
from pathlib import Path
|
| 13 |
from typing import Any, Dict, Optional, Tuple, Callable
|
|
@@ -17,6 +19,19 @@
|
|
| 17 |
|
| 18 |
logger = logging.getLogger(__name__)
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
# ----------------------------------------------------------------------------
|
| 21 |
# Background presets
|
| 22 |
# ----------------------------------------------------------------------------
|
|
@@ -187,10 +202,15 @@ def segment_person_hq(
|
|
| 187 |
High-quality person segmentation with proper SAM2 handling.
|
| 188 |
Expects RGB frame (H,W,3), uint8 or float in [0,1].
|
| 189 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
frame_rgb = _ensure_rgb(frame)
|
| 191 |
h, w = frame_rgb.shape[:2]
|
| 192 |
|
| 193 |
if use_sam2 is False:
|
|
|
|
| 194 |
return _simple_person_segmentation(frame_rgb)
|
| 195 |
|
| 196 |
if predictor is not None:
|
|
@@ -251,6 +271,8 @@ def segment_person_hq(
|
|
| 251 |
|
| 252 |
if mask is not None:
|
| 253 |
mask = _to_mask01(mask)
|
|
|
|
|
|
|
| 254 |
if float(mask.max()) > 0.1:
|
| 255 |
return np.ascontiguousarray(mask)
|
| 256 |
else:
|
|
@@ -294,9 +316,14 @@ def refine_mask_hq(
|
|
| 294 |
Returns:
|
| 295 |
2-D float32 alpha [H,W], contiguous, in [0,1] (OpenCV-safe).
|
| 296 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
mask01 = _to_mask01(mask)
|
| 298 |
|
| 299 |
if use_matanyone is False:
|
|
|
|
| 300 |
return mask01
|
| 301 |
|
| 302 |
if matanyone is not None and callable(matanyone):
|
|
@@ -470,4 +497,4 @@ def validate_video_file(video_path: str) -> Tuple[bool, str]:
|
|
| 470 |
"create_professional_background",
|
| 471 |
"validate_video_file",
|
| 472 |
"PROFESSIONAL_BACKGROUNDS",
|
| 473 |
-
]
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
cv_processing.py 路 FIXED VERSION with proper SAM2 handling + MatAnyone stateful integration
|
| 4 |
+
Now with environment variable support for USE_SAM2 and USE_MATANYONE
|
| 5 |
|
| 6 |
All public functions in this module expect RGB images (H,W,3) unless stated otherwise.
|
| 7 |
CoreVideoProcessor already converts BGR鈫扲GB before calling into this module.
|
|
|
|
| 9 |
|
| 10 |
from __future__ import annotations
|
| 11 |
|
| 12 |
+
import os
|
| 13 |
import logging
|
| 14 |
from pathlib import Path
|
| 15 |
from typing import Any, Dict, Optional, Tuple, Callable
|
|
|
|
| 19 |
|
| 20 |
logger = logging.getLogger(__name__)
|
| 21 |
|
| 22 |
+
# ----------------------------------------------------------------------------
|
| 23 |
+
# Environment variable helpers
|
| 24 |
+
# ----------------------------------------------------------------------------
|
| 25 |
+
def _use_sam2_enabled() -> bool:
|
| 26 |
+
"""Check if SAM2 should be used based on environment variable"""
|
| 27 |
+
val = os.getenv("USE_SAM2", "1")
|
| 28 |
+
return val.lower() in ("1", "true", "yes", "on")
|
| 29 |
+
|
| 30 |
+
def _use_matanyone_enabled() -> bool:
|
| 31 |
+
"""Check if MatAnyone should be used based on environment variable"""
|
| 32 |
+
val = os.getenv("USE_MATANYONE", "1")
|
| 33 |
+
return val.lower() in ("1", "true", "yes", "on")
|
| 34 |
+
|
| 35 |
# ----------------------------------------------------------------------------
|
| 36 |
# Background presets
|
| 37 |
# ----------------------------------------------------------------------------
|
|
|
|
| 202 |
High-quality person segmentation with proper SAM2 handling.
|
| 203 |
Expects RGB frame (H,W,3), uint8 or float in [0,1].
|
| 204 |
"""
|
| 205 |
+
# Override with environment variable if not explicitly set
|
| 206 |
+
if use_sam2 is None:
|
| 207 |
+
use_sam2 = _use_sam2_enabled()
|
| 208 |
+
|
| 209 |
frame_rgb = _ensure_rgb(frame)
|
| 210 |
h, w = frame_rgb.shape[:2]
|
| 211 |
|
| 212 |
if use_sam2 is False:
|
| 213 |
+
logger.info("SAM2 disabled by environment variable, using fallback segmentation")
|
| 214 |
return _simple_person_segmentation(frame_rgb)
|
| 215 |
|
| 216 |
if predictor is not None:
|
|
|
|
| 271 |
|
| 272 |
if mask is not None:
|
| 273 |
mask = _to_mask01(mask)
|
| 274 |
+
# Add debug logging
|
| 275 |
+
logger.info(f"SAM2 mask stats: shape={mask.shape}, min={mask.min():.3f}, max={mask.max():.3f}, mean={mask.mean():.3f}")
|
| 276 |
if float(mask.max()) > 0.1:
|
| 277 |
return np.ascontiguousarray(mask)
|
| 278 |
else:
|
|
|
|
| 316 |
Returns:
|
| 317 |
2-D float32 alpha [H,W], contiguous, in [0,1] (OpenCV-safe).
|
| 318 |
"""
|
| 319 |
+
# Override with environment variable if not explicitly set
|
| 320 |
+
if use_matanyone is None:
|
| 321 |
+
use_matanyone = _use_matanyone_enabled()
|
| 322 |
+
|
| 323 |
mask01 = _to_mask01(mask)
|
| 324 |
|
| 325 |
if use_matanyone is False:
|
| 326 |
+
logger.info("MatAnyone disabled by environment variable, returning unrefined mask")
|
| 327 |
return mask01
|
| 328 |
|
| 329 |
if matanyone is not None and callable(matanyone):
|
|
|
|
| 497 |
"create_professional_background",
|
| 498 |
"validate_video_file",
|
| 499 |
"PROFESSIONAL_BACKGROUNDS",
|
| 500 |
+
]
|