Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/mrgnw/spatial-maker-python. Convert 2D videos to stereoscopic 3D spatial videos for Apple Vision Pr
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417"""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 jsonimport subprocessimport osimport sysfrom pathlib import Path
import cv2import numpy as npimport torchfrom tqdm import tqdm
from depth_anything_v2.dpt import DepthAnythingV2
_threads_configured = False_processor_cache = {}
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, json_progress: bool = False, ): 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 self.json_progress = json_progress
def load_model(self): """Load the Depth Anything V2 model.""" global _threads_configured if self.model is not None: return
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], }, 'vitb': { 'encoder': 'vitb', 'features': 128, 'out_channels': [96, 192, 384, 768], }, 'vitl': { 'encoder': 'vitl', 'features': 256, 'out_channels': [256, 512, 1024, 1024], }, }
checkpoint_name = f'depth_anything_v2_{self.encoder}.pth' checkpoint_path = self._find_checkpoint(checkpoint_name)
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 = DepthAnythingV2(**model_configs[self.encoder]) # Load weights directly to target device to avoid MPS/CPU tensor mismatch self.model.load_state_dict( torch.load(str(checkpoint_path), map_location=self.device) ) self.model = self.model.to(self.device).eval()
def _find_checkpoint(self, checkpoint_name: str) -> Path: """ Find checkpoint file in multiple possible locations.
Searches in order: 1. Next to the script's source directory (for development) 2. User's home directory ~/.spatial-maker/checkpoints/ 3. XDG data directory 4. Current working directory ./checkpoints/ """ search_paths = [ # Development: relative to source file Path(__file__).parent.parent / 'checkpoints' / checkpoint_name, # User home directory Path.home() / '.spatial-maker' / 'checkpoints' / checkpoint_name, # XDG data home Path(os.environ.get('XDG_DATA_HOME', Path.home() / '.local' / 'share')) / 'spatial-maker' / 'checkpoints' / checkpoint_name, # Current working directory Path.cwd() / 'checkpoints' / checkpoint_name, ]
# Also check if SPATIAL_MAKER_CHECKPOINTS env var is set env_checkpoint_dir = os.environ.get('SPATIAL_MAKER_CHECKPOINTS') if env_checkpoint_dir: search_paths.insert(0, Path(env_checkpoint_dir) / checkpoint_name)
for path in search_paths: if path.exists(): return path
# Not found - provide helpful error message raise FileNotFoundError( f"Checkpoint '{checkpoint_name}' not found.\n" f'Searched locations:\n' + '\n'.join(f' - {p}' for p in search_paths) + '\n\nPlease download the checkpoint and place it in one of these locations:\n' f' mkdir -p ~/.spatial-maker/checkpoints\n' f' # Then copy {checkpoint_name} there' )
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 """ 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', '-q:v', '65', # Quality-based encoding (0-100, higher = better) '-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)
frames_processed = 0
progress_iter = range(total_frames) if not self.json_progress: progress_iter = tqdm(progress_iter, desc=' ', unit='frame', ncols=60)
try: for _ in progress_iter: ret, rgb_frame = cap.read() if not ret: break
with torch.no_grad(): depth = self.model.infer_image(rgb_frame, 518)
depth_normalized = (depth - depth.min()) / (depth.max() - depth.min())
left_eye, right_eye = create_stereo_pair( rgb_frame, depth_normalized, max_disparity )
sbs_frame = np.hstack([left_eye, right_eye])
ffmpeg_proc.stdin.write(sbs_frame.tobytes()) frames_processed += 1
if self.json_progress: pct = ( (frames_processed / total_frames * 100) if total_frames > 0 else 0 ) print( json.dumps( { 'event': 'progress', 'stage': 'depth_stereo', 'frame': frames_processed, 'total': total_frames, 'pct': round(pct, 1), } ), flush=True, )
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, json_progress: bool = False,) -> dict: """ 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 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 """ global _processor_cache
cache_key = (encoder, json_progress) if cache_key not in _processor_cache: _processor_cache[cache_key] = DepthToStereoProcessor( encoder=encoder, json_progress=json_progress )
processor = _processor_cache[cache_key] 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"]}')