Delete pipeline/pipeline_utils.py
Browse files- pipeline/pipeline_utils.py +0 -191
pipeline/pipeline_utils.py
DELETED
|
@@ -1,191 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import cv2
|
| 3 |
-
import numpy as np
|
| 4 |
-
import tempfile
|
| 5 |
-
from pathlib import Path
|
| 6 |
-
from typing import Optional, Union, Callable
|
| 7 |
-
import logging
|
| 8 |
-
from PIL import Image
|
| 9 |
-
|
| 10 |
-
# Configure logging
|
| 11 |
-
logging.basicConfig(level=logging.INFO)
|
| 12 |
-
logger = logging.getLogger(__name__)
|
| 13 |
-
|
| 14 |
-
class VideoProcessor:
|
| 15 |
-
def __init__(self, temp_dir: Optional[str] = None):
|
| 16 |
-
"""
|
| 17 |
-
Initialize the video processor.
|
| 18 |
-
|
| 19 |
-
Args:
|
| 20 |
-
temp_dir: Directory for temporary files. If None, creates a temp directory.
|
| 21 |
-
"""
|
| 22 |
-
self.temp_dir = Path(temp_dir) if temp_dir else Path(tempfile.mkdtemp(prefix="bg_replace_"))
|
| 23 |
-
self.temp_dir.mkdir(parents=True, exist_ok=True)
|
| 24 |
-
self.device = self._get_device()
|
| 25 |
-
logger.info(f"Initialized VideoProcessor with device: {self.device}")
|
| 26 |
-
|
| 27 |
-
def _get_device(self) -> str:
|
| 28 |
-
"""Check if CUDA is available."""
|
| 29 |
-
try:
|
| 30 |
-
import torch
|
| 31 |
-
return "cuda" if torch.cuda.is_available() else "cpu"
|
| 32 |
-
except ImportError:
|
| 33 |
-
return "cpu"
|
| 34 |
-
|
| 35 |
-
def _create_static_bg_video(
|
| 36 |
-
self,
|
| 37 |
-
bg_image: np.ndarray,
|
| 38 |
-
reference_video: str,
|
| 39 |
-
output_path: str
|
| 40 |
-
) -> str:
|
| 41 |
-
"""
|
| 42 |
-
Create a static background video matching the input video's duration.
|
| 43 |
-
"""
|
| 44 |
-
cap = cv2.VideoCapture(reference_video)
|
| 45 |
-
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 46 |
-
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 47 |
-
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 48 |
-
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 49 |
-
cap.release()
|
| 50 |
-
|
| 51 |
-
# Resize background image
|
| 52 |
-
bg_image = cv2.resize(bg_image, (width, height))
|
| 53 |
-
|
| 54 |
-
# Write video
|
| 55 |
-
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 56 |
-
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
| 57 |
-
|
| 58 |
-
for _ in range(total_frames):
|
| 59 |
-
out.write(bg_image)
|
| 60 |
-
|
| 61 |
-
out.release()
|
| 62 |
-
return output_path
|
| 63 |
-
|
| 64 |
-
def _process_with_pipeline(
|
| 65 |
-
self,
|
| 66 |
-
input_video: str,
|
| 67 |
-
background: Optional[Union[str, np.ndarray]] = None,
|
| 68 |
-
bg_type: str = "blur",
|
| 69 |
-
progress_callback: Optional[Callable] = None
|
| 70 |
-
) -> str:
|
| 71 |
-
"""
|
| 72 |
-
Process video using the two-stage pipeline.
|
| 73 |
-
"""
|
| 74 |
-
try:
|
| 75 |
-
# Import the pipeline
|
| 76 |
-
from integrated_pipeline import TwoStageProcessor
|
| 77 |
-
|
| 78 |
-
# Update progress
|
| 79 |
-
if progress_callback:
|
| 80 |
-
progress_callback(0.1, "Initializing pipeline...")
|
| 81 |
-
|
| 82 |
-
# Handle background
|
| 83 |
-
bg_video_path = ""
|
| 84 |
-
if bg_type == "image" and background is not None:
|
| 85 |
-
if isinstance(background, str):
|
| 86 |
-
bg_image = cv2.imread(background)
|
| 87 |
-
else:
|
| 88 |
-
bg_image = background
|
| 89 |
-
|
| 90 |
-
bg_video_path = str(self.temp_dir / "background.mp4")
|
| 91 |
-
self._create_static_bg_video(bg_image, input_video, bg_video_path)
|
| 92 |
-
|
| 93 |
-
# Initialize processor
|
| 94 |
-
processor = TwoStageProcessor(temp_dir=str(self.temp_dir))
|
| 95 |
-
|
| 96 |
-
# Process video
|
| 97 |
-
output_path = str(self.temp_dir / "output.mp4")
|
| 98 |
-
|
| 99 |
-
# Mock click points (center of frame)
|
| 100 |
-
click_points = [[0.5, 0.5]]
|
| 101 |
-
|
| 102 |
-
# Process
|
| 103 |
-
success = processor.process_video(
|
| 104 |
-
input_video=input_video,
|
| 105 |
-
background_video=bg_video_path if bg_type == "image" else "",
|
| 106 |
-
click_points=click_points,
|
| 107 |
-
output_path=output_path,
|
| 108 |
-
use_matanyone=True,
|
| 109 |
-
progress_callback=progress_callback
|
| 110 |
-
)
|
| 111 |
-
|
| 112 |
-
if not success:
|
| 113 |
-
raise RuntimeError("Video processing failed")
|
| 114 |
-
|
| 115 |
-
return output_path
|
| 116 |
-
|
| 117 |
-
except Exception as e:
|
| 118 |
-
logger.error(f"Error in pipeline: {str(e)}")
|
| 119 |
-
raise
|
| 120 |
-
|
| 121 |
-
def process_video(
|
| 122 |
-
self,
|
| 123 |
-
input_path: Union[str, bytes],
|
| 124 |
-
background: Optional[Union[str, np.ndarray]] = None,
|
| 125 |
-
bg_type: str = "blur",
|
| 126 |
-
progress_callback: Optional[Callable] = None
|
| 127 |
-
) -> bytes:
|
| 128 |
-
"""
|
| 129 |
-
Process a video with the given background.
|
| 130 |
-
|
| 131 |
-
Args:
|
| 132 |
-
input_path: Path to input video or bytes
|
| 133 |
-
background: Background image path or numpy array
|
| 134 |
-
bg_type: Type of background ("image", "color", or "blur")
|
| 135 |
-
progress_callback: Optional callback for progress updates
|
| 136 |
-
|
| 137 |
-
Returns:
|
| 138 |
-
Processed video as bytes
|
| 139 |
-
"""
|
| 140 |
-
try:
|
| 141 |
-
# Save input to temp file if it's bytes
|
| 142 |
-
if isinstance(input_path, bytes):
|
| 143 |
-
input_video = str(self.temp_dir / "input.mp4")
|
| 144 |
-
with open(input_video, "wb") as f:
|
| 145 |
-
f.write(input_path)
|
| 146 |
-
else:
|
| 147 |
-
input_video = input_path
|
| 148 |
-
|
| 149 |
-
# Process the video
|
| 150 |
-
output_path = self._process_with_pipeline(
|
| 151 |
-
input_video,
|
| 152 |
-
background,
|
| 153 |
-
bg_type,
|
| 154 |
-
progress_callback
|
| 155 |
-
)
|
| 156 |
-
|
| 157 |
-
# Read the output file
|
| 158 |
-
with open(output_path, "rb") as f:
|
| 159 |
-
return f.read()
|
| 160 |
-
|
| 161 |
-
except Exception as e:
|
| 162 |
-
logger.error(f"Error processing video: {str(e)}")
|
| 163 |
-
raise
|
| 164 |
-
|
| 165 |
-
# Global instance
|
| 166 |
-
video_processor = VideoProcessor()
|
| 167 |
-
|
| 168 |
-
def process_video_pipeline(
|
| 169 |
-
input_data: Union[str, bytes],
|
| 170 |
-
background: Optional[Union[str, np.ndarray]] = None,
|
| 171 |
-
bg_type: str = "blur",
|
| 172 |
-
progress_callback: Optional[Callable] = None
|
| 173 |
-
) -> bytes:
|
| 174 |
-
"""
|
| 175 |
-
High-level function to process a video.
|
| 176 |
-
|
| 177 |
-
Args:
|
| 178 |
-
input_data: Input video path or bytes
|
| 179 |
-
background: Background image path or numpy array
|
| 180 |
-
bg_type: Type of background ("image", "color", or "blur")
|
| 181 |
-
progress_callback: Optional progress callback
|
| 182 |
-
|
| 183 |
-
Returns:
|
| 184 |
-
Processed video as bytes
|
| 185 |
-
"""
|
| 186 |
-
return video_processor.process_video(
|
| 187 |
-
input_data,
|
| 188 |
-
background,
|
| 189 |
-
bg_type,
|
| 190 |
-
progress_callback
|
| 191 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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