From 22ac38bf7bdd08966e7be63b7aaf904205213b65 Mon Sep 17 00:00:00 2001 From: Morgan Williams Date: Wed, 24 Dec 2025 18:08:09 +0100 Subject: [PATCH] Combine depth estimation and stereo conversion into single streaming pipeline - New scripts/depth_to_stereo.py pipes frames directly to ffmpeg - No intermediate depth frame files written to disk - Reduces pipeline from 4 stages to 3 stages - More efficient memory and disk usage --- pipeline.py | 30 ++-- scripts/depth_to_stereo.py | 312 +++++++++++++++++++++++++++++++++++++ 2 files changed, 321 insertions(+), 21 deletions(-) create mode 100644 scripts/depth_to_stereo.py diff --git a/pipeline.py b/pipeline.py index ab7b540..8b2ba17 100644 --- a/pipeline.py +++ b/pipeline.py @@ -20,8 +20,7 @@ from pathlib import Path import shutil from scripts.downscale_video import downscale_to_1080p24 -from scripts.stereo_converter import convert_video_to_sbs -from depth_estimators import DepthAnythingV2Estimator +from scripts.depth_to_stereo import process_video_to_sbs def check_spatial_cli(): @@ -94,36 +93,25 @@ def run_pipeline( ) print(f" Prepared video: {prepared_video}") - # Stage 2: Generate depth maps + # Stage 2: Generate depth and create SBS stereoscopic video print("\n" + "=" * 60) - print(f"Stage 2: Generating depth maps (encoder: {encoder})") - print("=" * 60) - - depth_dir = work_dir / "depth_frames" - estimator = DepthAnythingV2Estimator(encoder=encoder) - depth_result = estimator.process_video(prepared_video, str(depth_dir)) - print(f" Processed {depth_result['frames_processed']} frames at {depth_result['fps']} fps") - print(f" Depth video: {depth_result['video_path']}") - - # Stage 3: Create side-by-side stereoscopic video - print("\n" + "=" * 60) - print(f"Stage 3: Creating SBS stereoscopic video (disparity: {max_disparity})") + print(f"Stage 2: Depth estimation + SBS stereo (encoder: {encoder}, disparity: {max_disparity})") print("=" * 60) sbs_video = work_dir / f"{input_path.stem}_sbs.mp4" - sbs_result = convert_video_to_sbs( + sbs_result = process_video_to_sbs( prepared_video, - str(depth_dir), str(sbs_video), + encoder=encoder, max_disparity=max_disparity, fps=24, - half_width=False, # Full-width SBS so each eye maintains original resolution ) - print(f" SBS video: {sbs_result}") + print(f" Processed {sbs_result['frames_processed']} frames") + print(f" SBS video: {sbs_result['output_path']}") - # Stage 4: Convert to spatial video using spatial CLI + # Stage 3: Convert to spatial video using spatial CLI print("\n" + "=" * 60) - print("Stage 4: Converting to MV-HEVC spatial video") + print("Stage 3: Converting to MV-HEVC spatial video") print("=" * 60) if not check_spatial_cli(): diff --git a/scripts/depth_to_stereo.py b/scripts/depth_to_stereo.py new file mode 100644 index 0000000..a858fab --- /dev/null +++ b/scripts/depth_to_stereo.py @@ -0,0 +1,312 @@ +""" +Combined depth estimation and stereo conversion - pipes directly to ffmpeg without intermediate frames. + +This module combines depth estimation (Depth Anything V2) with stereo conversion (DIBR) +into a single streaming pipeline that avoids writing intermediate depth frames to disk. +""" + +import subprocess +import sys +import os +from pathlib import Path + +import cv2 +import numpy as np +import torch +from tqdm import tqdm + +sys.path.insert(0, str(Path(__file__).parent.parent / "Depth-Anything-V2")) +from depth_anything_v2.dpt import DepthAnythingV2 + + +def create_stereo_pair( + rgb_frame: np.ndarray, + depth_frame: np.ndarray, + max_disparity: int = 30, +) -> tuple: + """ + Create left and right eye views from RGB frame and depth map. + + Args: + rgb_frame: Original RGB image (H, W, 3) + depth_frame: Normalized depth map (H, W), values 0-1 + max_disparity: Maximum pixel shift for closest objects + + Returns: + (left_eye, right_eye) tuple of images + """ + h, w = rgb_frame.shape[:2] + + # Calculate disparity map (closer = higher value = more shift) + disparity = (depth_frame * max_disparity).astype(np.float32) + + # Create coordinate grids + x_coords = np.arange(w, dtype=np.float32) + y_coords = np.arange(h, dtype=np.float32) + x_grid, y_grid = np.meshgrid(x_coords, y_coords) + + # Shift for left eye (shift right for closer objects) + x_left = x_grid + disparity / 2 + # Shift for right eye (shift left for closer objects) + x_right = x_grid - disparity / 2 + + # Remap using cv2 (handles interpolation and boundary) + left_eye = cv2.remap( + rgb_frame, + x_left, + y_grid, + interpolation=cv2.INTER_LINEAR, + borderMode=cv2.BORDER_REPLICATE, + ) + right_eye = cv2.remap( + rgb_frame, + x_right, + y_grid, + interpolation=cv2.INTER_LINEAR, + borderMode=cv2.BORDER_REPLICATE, + ) + + return left_eye, right_eye + + +class DepthToStereoProcessor: + """ + Combined depth estimation and stereo conversion processor. + + Streams RGB frames through depth estimation and stereo conversion, + piping output directly to ffmpeg without intermediate files. + """ + + def __init__(self, encoder: str = "vits", num_threads: int = None): + self.encoder = encoder + self.num_threads = num_threads if num_threads is not None else max(1, os.cpu_count() - 4) + self.model = None + self.device = None + + def load_model(self): + """Load the Depth Anything V2 model.""" + if self.model is not None: + return + + torch.set_num_threads(self.num_threads) + torch.set_num_interop_threads(self.num_threads) + + model_configs = { + "vits": {"encoder": "vits", "features": 64, "out_channels": [48, 96, 192, 384]}, + "vitb": {"encoder": "vitb", "features": 128, "out_channels": [96, 192, 384, 768]}, + "vitl": {"encoder": "vitl", "features": 256, "out_channels": [256, 512, 1024, 1024]}, + } + + checkpoint_path = ( + Path(__file__).parent.parent + / "Depth-Anything-V2" + / "checkpoints" + / f"depth_anything_v2_{self.encoder}.pth" + ) + + self.model = DepthAnythingV2(**model_configs[self.encoder]) + self.model.load_state_dict(torch.load(str(checkpoint_path), map_location="cpu")) + + if torch.backends.mps.is_available(): + self.device = torch.device("mps") + elif torch.cuda.is_available(): + self.device = torch.device("cuda") + else: + self.device = torch.device("cpu") + + self.model = self.model.to(self.device).eval() + + def process_video( + self, + video_path: str, + output_path: str, + max_disparity: int = 30, + fps: int = 24, + max_frames: int = None, + ) -> dict: + """ + Process video: generate depth and create SBS stereo, piping directly to ffmpeg. + + Args: + video_path: Path to input RGB video + output_path: Path for output SBS video + max_disparity: Maximum pixel disparity for 3D effect + fps: Output frame rate + max_frames: Optional limit on frames to process + + Returns: + Dict with processing statistics + """ + print(" Loading depth model...") + self.load_model() + + # Open input video + cap = cv2.VideoCapture(video_path) + if not cap.isOpened(): + raise ValueError(f"Could not open video: {video_path}") + + total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) + if max_frames is not None: + total_frames = min(total_frames, max_frames) + + # Get dimensions + width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) + height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) + + # Output is full-width SBS (double width) + out_width = width * 2 + out_height = height + + output_path = Path(output_path) + output_path.parent.mkdir(parents=True, exist_ok=True) + + # Start ffmpeg process to receive piped frames + ffmpeg_cmd = [ + "ffmpeg", + "-y", + "-f", "rawvideo", + "-vcodec", "rawvideo", + "-s", f"{out_width}x{out_height}", + "-pix_fmt", "bgr24", + "-r", str(fps), + "-i", "-", # Read from stdin + "-c:v", "hevc_videotoolbox", + "-b:v", "12M", + "-tag:v", "hvc1", + str(output_path), + ] + + ffmpeg_proc = subprocess.Popen( + ffmpeg_cmd, + stdin=subprocess.PIPE, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + ) + + # Warmup inference + ret, first_frame = cap.read() + if not ret: + raise ValueError("Could not read first frame") + cap.set(cv2.CAP_PROP_POS_FRAMES, 0) + + with torch.no_grad(): + _ = self.model.infer_image(first_frame, 518) + print(" Model warmed up") + + frames_processed = 0 + + try: + for _ in tqdm(range(total_frames), desc=" Processing", unit="frame"): + ret, rgb_frame = cap.read() + if not ret: + break + + # Generate depth + with torch.no_grad(): + depth = self.model.infer_image(rgb_frame, 518) + + # Normalize depth to 0-1 + depth_normalized = (depth - depth.min()) / (depth.max() - depth.min()) + + # Create stereo pair + left_eye, right_eye = create_stereo_pair( + rgb_frame, depth_normalized, max_disparity + ) + + # Create full-width SBS frame + sbs_frame = np.hstack([left_eye, right_eye]) + + # Pipe to ffmpeg + ffmpeg_proc.stdin.write(sbs_frame.tobytes()) + frames_processed += 1 + + if max_frames is not None and frames_processed >= max_frames: + break + + finally: + cap.release() + ffmpeg_proc.stdin.close() + ffmpeg_proc.wait() + + return { + "frames_processed": frames_processed, + "output_path": str(output_path), + "dimensions": f"{out_width}x{out_height}", + } + + +def process_video_to_sbs( + video_path: str, + output_path: str, + encoder: str = "vits", + max_disparity: int = 30, + fps: int = 24, + max_frames: int = None, +) -> dict: + """ + Convenience function to process a video to SBS stereo. + + Args: + video_path: Path to input RGB video + output_path: Path for output SBS video + encoder: Depth Anything V2 encoder (vits, vitb, vitl) + max_disparity: Maximum pixel disparity for 3D effect + fps: Output frame rate + max_frames: Optional limit on frames to process + + Returns: + Dict with processing statistics + """ + processor = DepthToStereoProcessor(encoder=encoder) + return processor.process_video( + video_path, + output_path, + max_disparity=max_disparity, + fps=fps, + max_frames=max_frames, + ) + + +if __name__ == "__main__": + import argparse + + parser = argparse.ArgumentParser( + description="Convert 2D video to SBS stereo using depth estimation" + ) + parser.add_argument("input", help="Input video file") + parser.add_argument("output", help="Output SBS video file") + parser.add_argument( + "--encoder", + choices=["vits", "vitb", "vitl"], + default="vits", + help="Depth model encoder (default: vits)", + ) + parser.add_argument( + "--max-disparity", + type=int, + default=30, + help="Maximum pixel disparity (default: 30)", + ) + parser.add_argument( + "--fps", + type=int, + default=24, + help="Output FPS (default: 24)", + ) + parser.add_argument( + "--max-frames", + type=int, + help="Maximum frames to process", + ) + + args = parser.parse_args() + + result = process_video_to_sbs( + args.input, + args.output, + encoder=args.encoder, + max_disparity=args.max_disparity, + fps=args.fps, + max_frames=args.max_frames, + ) + print(f"Done! Output: {result['output_path']}") -- knotmirror 0.0.1