Live AI Lab · Depth estimation

Depth Mirror

You have two eyes to tell near from far. Your camera only has one! Can a model guess how far away everything is from a single flat picture? Let's find out. Red means near, blue means far.

Mirror
Camera is offWake up the model, then press Start camera.
Depth map
FarNear
Or try a sample photo:

Point at any spot on either picture to measure how near it is. The camera picture never leaves your device.

How it works

One flat picture

Robo grabs a still from your camera. It has no idea how far anything is, just colours.

Look for clues

The model learned the clues artists use: things that look bigger are nearer, near things hide far things, faraway stuff is hazy, floors and walls shrink into the distance.

Paint the guess

It gives every single pixel a “how near” number. We paint high numbers red and low numbers blue.

Cut along a line

Pick a line. Pixels nearer than the line stay, the rest are swapped for space or a beach. That's the cut-out!

What just happened?

For grown-ups: what's really going on

Model: onnx-community/depth-anything-v2-small, the ONNX export of Depth Anything V2 Small (≈25 M parameters, Apache-2.0), run with Transformers.js 3.8 through the depth-estimation pipeline. On WebGPU with shader-f16 we load the fp16 weights (≈50 MB); otherwise we use the 8-bit quantised q8 weights (≈27 MB) on WebAssembly. Files are cached by the browser after the first visit.

  • Architecture: a DINOv2 ViT-S/14 encoder (14×14 patches) feeding a DPT (Dense Prediction Transformer) decoder that reassembles tokens from four encoder layers into a multi-scale feature pyramid and upsamples to a dense map. V2 was trained on 595K synthetic images with precise depth, then a large teacher pseudo-labelled ~62M real images to train the small student.
  • Output: relative inverse depth (disparity-like, larger = nearer), defined only up to an unknown scale and shift (affine-invariant). That is why we only talk about “nearer / farther”, and why the pipeline min–max normalises each frame to 0–255: the reddest pixel is just “the nearest thing in this frame”. No metric distances.
  • Pre-processing: the mirrored webcam frame is drawn to a 320×240 canvas; the DPT processor resizes it keeping aspect ratio to a multiple of 14 (we set image_processor.size to 196 / 266 / 364 instead of the default 518 for speed), rescales and normalises with ImageNet mean/std. Post-processing: bilinear upsampling of predicted_depth back to 320×240, then min–max to uint8.
  • Our layer (pure functions in live-depth-core.js, unit-tested in Node): an exponential moving average D ← D + 0.5·(Dnew − D) to reduce flicker; a 256-entry colour lookup table (turbo-like); the cut-out is a soft threshold α = clamp((d − (t − s/2)) / s, 0, 1) with s = 20, composited per pixel as α·camera + (1−α)·backdrop on every video frame using the latest depth map. Fog mixes towards grey by (1 − d)1.5. The “hand closer than face” check is a heuristic on the depth histogram (a small very-near band, an empty gap band, a large middle band), so it can misfire.
  • Latency: inference never overlaps (grab → await → draw → next animation frame). Rough guide at 266 px: WebGPU ≈ 20–60 ms, WebAssembly ≈ 250–900 ms depending on CPU. The mirror itself redraws at video frame rate; the depth map updates at the speed shown.
  • Failure modes: mirrors, glass and screens; textureless walls; pictures of scenes (it happily reconstructs depth in a photo of a room); thin structures at low resolution; strong backlight. Edges “bleed” because the map is upsampled from a coarse 14-px patch grid, which is why the cut-out has a halo.
  • Privacy: all processing is on-device; no frames are uploaded. The camera turns off when you press Stop or leave the page. Sample photos come from the Transformers.js docs dataset on Hugging Face.

Further reading: Yang et al., “Depth Anything V2” (2024) · Ranftl et al., “Vision Transformers for Dense Prediction” (DPT, 2021) · Hugging Face: monocular depth estimation.