Media depth

From damionrashford/media-os · View interactive version

Media depth is an AI agent skill from the GitHub repository damionrashford/media-os. Monocular depth estimation with open-source + commercial-safe models: Depth-Anything v2 (Apache 2.0, TikTok/ByteDance 2024, current SOTA small/base/large) via HuggingFace transformers, MiDaS v3.1 (MIT, Intel ISL classic with DPT/BEiT/Swin backbones). Extract depth maps from a single image or video,

How to use this skill

Skill file

Media Depth

Context: $ARGUMENTS

Monocular depth estimation — estimate a per-pixel depth map from a single RGB image or video, no stereo rig or LiDAR required. Two actively maintained open-source model families with commercial-safe licenses: Depth-Anything v2 (Apache 2.0, current SOTA, 2024) and MiDaS v3.1 (MIT, Intel ISL, long-standing baseline).

Quick start

When to use

Step 1 — Install PyTorch + transformers

Depth-Anything v2 is distributed via HuggingFace transformers using the pipeline("depth-estimation", ...) API — no custom model code required. MiDaS also works through transformers (using the DPTForDepthEstimation class).

PEP 723 header in scripts/depth.py declares the full dependency set. Running via uv run handles an ephemeral venv. Manual install:

pip install torch transformers opencv-python numpy pillow

On Apple Silicon, PyTorch picks MPS automatically when you set device="mps". On NVIDIA, device="cuda". The helper auto-selects.

Verify the runtime:

uv run ${CLAUDE_SKILL_DIR}/scripts/depth.py install depthanything-v2 --size small

Downloads the depth-anything/Depth-Anything-V2-Small-hf weights (~100 MB) into ~/.cache/huggingface/hub/.

Step 2 — Pick a model

| Model | License | Variants | Relative/Metric | Notes |

|-----------------------------|---------------|-----------------------------------|-----------------|--------------------------------------------------|

| depthanything-v2 (default)| Apache 2.0 | small / base / large | Relative (inverse depth) | SOTA 2024, crisper edges than v1; metric fine-tunes exist for indoor/outdoor |

| midas | MIT | dpt-swinv2-tiny-256 / dpt-beit-large-512 | Relative | Intel ISL v3.1, older, still robust |

Pick by size vs accuracy:

Full per-model speed, VRAM, and license matrix in references/models.md. Read it when picking a model for batch video or embedded deployment.

Step 3 — Depth map from a single image

uv run ${CLAUDE_SKILL_DIR}/scripts/depth.py image \
  --model depthanything-v2 --size small \
  --in photo.jpg --out depth.png

Outputs a 16-bit single-channel PNG where 0 = far, 65535 = near (the HuggingFace pipeline yields "inverse depth", higher = closer). Also writes depth_preview.png — a turbo-colormap 8-bit visualization for human inspection.

Useful flags:

Step 4 — Per-frame depth for video

uv run ${CLAUDE_SKILL_DIR}/scripts/depth.py video \
  --model depthanything-v2 --size small \
  --in input.mp4 --out depth.mp4

Writes a grayscale H.264 MP4 at the source fps where each pixel encodes depth (8-bit). For compositing fidelity prefer:

--out-format png-seq --out depth_frames/   # 16-bit PNG sequence, frame_%06d.png
--out-format exr-seq --out depth_exr/      # 32-bit EXR sequence (best for Nuke)

Gotcha: per-frame monocular depth is temporally unstable — the depth of a static object can flicker frame-to-frame. Use --smooth temporal for a light 3-tap EMA across frames, or hand the output to ffmpeg-denoise-restore (hqdn3d on the grayscale depth track) before consuming it downstream.

Step 5 — 2D → stereo 3D (depth-driven parallax)

uv run ${CLAUDE_SKILL_DIR}/scripts/depth.py stereo \
  --model depthanything-v2 --size base \
  --in photo.jpg \
  --out-left left.png --out-right right.png \
  --baseline 6.5

--baseline is the interpupillary-distance shift in pixels (or millimeters at 96 dpi — it's an opinionated single scalar). Typical values:

The occlusion-aware parallax math (forward-warp with depth-sorted Z-buffer, then inpaint revealed background) is documented in references/parallax-math.md — read it when you need to tune the quality / hole-filling trade-off.

Pack side-by-side for a 3D TV / Meta Quest:

ffmpeg -i left.png -i right.png -filter_complex hstack sbs.png

Step 6 — Animate a 2.5D parallax shot (Ken Burns style)

uv run ${CLAUDE_SKILL_DIR}/scripts/depth.py parallax \
  --image photo.jpg --depth depth.png \
  --out parallax.mp4 \
  --frames 120 --fps 30 \
  --motion orbit --amplitude 40

--motion presets:

--amplitude is the max pixel shift of the nearest foreground. Values 20–80 work well for 1080p stills. Hole-fill is done via OpenCV INPAINT_TELEA on the revealed background — the reference doc covers why TELEA beats NS for this specific use case.

If you have both the --image and --depth already, parallax is a pure post-process (no model inference). Chain it after Step 3.

Available scripts

Reference docs

Gotchas

Examples

Example 1 — Depth PNG from a JPEG

uv run ${CLAUDE_SKILL_DIR}/scripts/depth.py image \
  --model depthanything-v2 --size small \
  --in portrait.jpg --out portrait_depth.png --invert

Produces a 16-bit grayscale PNG where near objects are dark, far is bright (Blender convention).

Example 2 — EXR depth sequence for Nuke

uv run ${CLAUDE_SKILL_DIR}/scripts/depth.py video \
  --model depthanything-v2 --size base \
  --in plate.mov --out depth_exr/ --out-format exr-seq \
  --smooth temporal

Writes depth_exr/frame_000001.exr … — 32-bit float Z-depth per frame with light temporal smoothing.

Example 3 — Side-by-side stereo from a landscape photo

# 1. depth
uv run ${CLAUDE_SKILL_DIR}/scripts/depth.py image \
  --model depthanything-v2 --size base \
  --in lake.jpg --out lake_depth.png

# 2. stereo pair
uv run ${CLAUDE_SKILL_DIR}/scripts/depth.py stereo \
  --in lake.jpg --depth lake_depth.png \
  --out-left L.png --out-right R.png --baseline 8.0

# 3. pack as SBS
ffmpeg -y -i L.png -i R.png -filter_complex hstack lake_sbs.png

Example 4 — 2.5D parallax reel from a single still

uv run ${CLAUDE_SKILL_DIR}/scripts/depth.py image \
  --model depthanything-v2 --size small \
  --in hero.jpg --out hero_depth.png

uv run ${CLAUDE_SKILL_DIR}/scripts/depth.py parallax \
  --image hero.jpg --depth hero_depth.png \
  --out hero_parallax.mp4 \
  --motion orbit --frames 150 --fps 30 --amplitude 35

Example 5 — Batch depth on a folder (GNU parallel)

Hand off to media-batch:

ls in/*.jpg | parallel --jobs 4 \
  uv run ${CLAUDE_SKILL_DIR}/scripts/depth.py image \
    --model depthanything-v2 --size small \
    --in {} --out out/{/.}_depth.png

Troubleshooting

`OSError: Can't load tokenizer/image processor for 'depth-anything/Depth-Anything-V2-Small-hf'`

Cause: No network on first run, or HF Hub outage.

Solution: Pre-download with depth.py install depthanything-v2 --size small when the network is up; weights cache to ~/.cache/huggingface/hub/ and work offline after.

Depth looks inverted (near=bright, I expected near=dark)

Cause: HuggingFace convention is inverse-depth (near=bright); most DCCs want the opposite.

Solution: Re-run with --invert. Or post-process: python -c "import cv2,numpy as np; d=cv2.imread('d.png',-1); cv2.imwrite('d_inv.png', 65535 - d)".

Video output flickers frame-to-frame

Cause: Monocular per-frame depth is temporally unstable.

Solution: Add --smooth temporal. For stronger stability, hand off the depth video to ffmpeg-denoise-restore with hqdn3d=4:3:6:4.5, or upgrade to a video-aware model (none of those are Apache/MIT as of April 2026 — track updates in references/models.md).

Stereo pair has obvious tearing / black slivers

Cause: Parallax baseline too large for the depth range; hole-fill can't invent enough background.

Solution: Reduce --baseline (try 3.0–5.0), or supply a higher-quality depth map (--size large).

`RuntimeError: MPS backend out of memory`

Cause: Apple Silicon MPS allocator fragments on large images.

Solution: --device cpu (slower), or downscale the input (ffmpeg -i in.jpg -vf scale=1280:-1 small.jpg) before running depth.

`ffmpeg` not found when writing video output

Cause: The video subcommand shells out to ffmpeg for encoding.

Solution: brew install ffmpeg (macOS) or your distro equivalent — the ffmpeg-transcode skill has install details.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.