A custom
Modular Diffusers block for monocular depth estimation using Apple's
Depth Pro model. Supports both images and videos.
1# Using uv
2uv sync
3
4# Using pip
5pip install -r requirements.txt
1from diffusers import ModularPipelineBlocks
2import torch
3
4blocks = ModularPipelineBlocks.from_pretrained(
5 "your-username/depth-pro-estimator", # or local path "."
6 trust_remote_code=True,
7)
8pipeline = blocks.init_pipeline()
9pipeline.load_components(torch_dtype=torch.float16)
10pipeline.to("cuda")
1from PIL import Image
2
3image = Image.open("photo.jpg")
4output = pipeline(image=image)
5
6# Save depth map
7output.depth_image.save("photo_depth.png")
8
9# Access raw metric depth tensor (in meters)
10print(output.predicted_depth.shape) # (H, W)
11print(output.field_of_view) # estimated FOV
12print(output.focal_length) # estimated focal length
1output = pipeline(image=image, colormap="turbo")
2output.depth_image.save("photo_depth_turbo.png")
1from block import save_video
2
3output = pipeline(video_path="input.mp4", colormap="grayscale")
4save_video(output.depth_frames, output.fps, "output_depth.mp4")
1output = pipeline(video_path="input.mp4", colormap="turbo")
2save_video(output.depth_frames, output.fps, "output_depth_turbo.mp4")
Depth visualization uses inverse depth clipped to [0.1m, 250m], following
Apple's reference implementation. This prevents sky/infinity values (clamped at 10,000m by the model) from crushing near-field detail into a binary mask.