Create pipelines/video_matting_pipeline.py
Browse files
pipelines/video_matting_pipeline.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Add import at the top
|
| 2 |
+
from models.wrappers.matanyone_wrapper import MatAnyOneWrapper
|
| 3 |
+
|
| 4 |
+
# In your __init__ method, initialize MatAnyone
|
| 5 |
+
def __init__(self, ...):
|
| 6 |
+
# ... existing SAM2 initialization ...
|
| 7 |
+
|
| 8 |
+
# Add MatAnyone initialization
|
| 9 |
+
if self.use_matanyone:
|
| 10 |
+
from matanyone.inference_core import InferenceCore # Or wherever it's located
|
| 11 |
+
matanyone_core = InferenceCore(...) # Initialize with your config
|
| 12 |
+
self.matanyone = MatAnyOneWrapper(matanyone_core, device=self.device)
|
| 13 |
+
|
| 14 |
+
# In your process_frame or segment_frame method
|
| 15 |
+
def process_frame(self, frame, ...):
|
| 16 |
+
# ... existing SAM2 processing ...
|
| 17 |
+
|
| 18 |
+
# Add MatAnyone refinement after SAM2
|
| 19 |
+
if self.use_matanyone and sam2_mask is not None:
|
| 20 |
+
# Convert SAM2 output to tensor if needed
|
| 21 |
+
mask_tensor = self._prepare_mask_tensor(sam2_mask)
|
| 22 |
+
image_tensor = self._prepare_image_tensor(frame)
|
| 23 |
+
|
| 24 |
+
# Load component masks if available
|
| 25 |
+
components = None
|
| 26 |
+
if self.component_paths:
|
| 27 |
+
components = {
|
| 28 |
+
'hair': self._load_component('hair', frame_idx),
|
| 29 |
+
'edge': self._load_component('edge', frame_idx),
|
| 30 |
+
# ... other components
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
# Refine with MatAnyone
|
| 34 |
+
refined_mask = self.matanyone.step(
|
| 35 |
+
image_tensor,
|
| 36 |
+
mask_tensor,
|
| 37 |
+
components=components
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
# Convert back to numpy if needed
|
| 41 |
+
final_mask = refined_mask.cpu().numpy().squeeze()
|