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.
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?
- A model guessed 3D from a 2D picture. That's called depth estimation, and it worked with one ordinary camera.
- It only knows nearer and farther, not real centimetres. The nearest thing is always red and the farthest always blue, even if you step back.
- The cut-out is just a rule on top of the model's guess: “keep pixels nearer than the line”. Video-call background blur works in a similar way.
- Photos of rooms can fool it, because they have all the same clues as a real room. Your eyes can be fooled the same way!
- Robots, self-driving cars and phone cameras (portrait mode) use depth guesses like this to understand the world.
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.sizeto 196 / 266 / 364 instead of the default 518 for speed), rescales and normalises with ImageNet mean/std. Post-processing: bilinear upsampling ofpredicted_depthback to 320×240, then min–max touint8. - Our layer (pure functions in
live-depth-core.js, unit-tested in Node): an exponential moving averageD ← 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)withs = 20, composited per pixel asα·camera + (1−α)·backdropon 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.