From 921b3c87f9a6079477261addd05294bd3cbd68b9 Mon Sep 17 00:00:00 2001 From: Morgan Williams Date: Wed, 24 Dec 2025 18:38:13 +0100 Subject: [PATCH] Fix batch processing: cache model, configure threads once, suppress ffmpeg output --- scripts/depth_to_stereo.py | 21 ++++++++++++++++++--- scripts/downscale_video.py | 4 ++-- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/scripts/depth_to_stereo.py b/scripts/depth_to_stereo.py index d9ec78e..688538c 100644 --- a/scripts/depth_to_stereo.py +++ b/scripts/depth_to_stereo.py @@ -16,6 +16,9 @@ from tqdm import tqdm from depth_anything_v2.dpt import DepthAnythingV2 +_threads_configured = False +_processor_cache = {} + def create_stereo_pair( rgb_frame: np.ndarray, @@ -83,11 +86,14 @@ class DepthToStereoProcessor: def load_model(self): """Load the Depth Anything V2 model.""" + global _threads_configured if self.model is not None: return - torch.set_num_threads(self.num_threads) - torch.set_num_interop_threads(self.num_threads) + if not _threads_configured: + torch.set_num_threads(self.num_threads) + torch.set_num_interop_threads(self.num_threads) + _threads_configured = True model_configs = { "vits": {"encoder": "vits", "features": 64, "out_channels": [48, 96, 192, 384]}, @@ -243,6 +249,9 @@ def process_video_to_sbs( """ Convenience function to process a video to SBS stereo. + Uses a cached processor instance to reuse the model across multiple calls + (important for batch processing). + Args: video_path: Path to input RGB video output_path: Path for output SBS video @@ -254,7 +263,13 @@ def process_video_to_sbs( Returns: Dict with processing statistics """ - processor = DepthToStereoProcessor(encoder=encoder) + global _processor_cache + + # Reuse processor for same encoder to avoid reloading model + if encoder not in _processor_cache: + _processor_cache[encoder] = DepthToStereoProcessor(encoder=encoder) + + processor = _processor_cache[encoder] return processor.process_video( video_path, output_path, diff --git a/scripts/downscale_video.py b/scripts/downscale_video.py index 0056ba9..00d3cc2 100644 --- a/scripts/downscale_video.py +++ b/scripts/downscale_video.py @@ -22,7 +22,7 @@ def downscale_to_1080p24(input_path: str, output_path: str = None, duration: flo "-b:a", "192k", str(cached) ] - subprocess.run(cmd, check=True) + subprocess.run(cmd, check=True, capture_output=True) if duration is not None: if output_path is None: @@ -33,7 +33,7 @@ def downscale_to_1080p24(input_path: str, output_path: str = None, duration: flo "-c", "copy", output_path ] - subprocess.run(cmd, check=True) + subprocess.run(cmd, check=True, capture_output=True) return output_path if output_path is None: -- knotmirror 0.0.1