This project captures frames from a webcam (or a video file), estimates a monocular depth map using Depth Anything V2, extracts depth discontinuity edges, and overlays thick, coloured outlines onto the original video.
The outline colour encodes relative depth (not metres). The edge colouring is stabilised by deriving colours from a coarse, neighbour-pooled depth grid, while edge detection can optionally use CLAHE for stronger boundaries in low-contrast scenes (especially outdoors).
- Runs Depth Anything V2 locally using PyTorch on CUDA.
- Builds a depth-based edge mask (Sobel gradients + threshold).
- Colours edges by depth and overlays them on the RGB frame.
- Uses a coarse grid for edge colouring (fast, reduces “background-coloured” thick outlines).
- Optional CLAHE step improves edge detection without distorting edge colours.
Below is an image from a frame of video, illustrating how this tool can add coloured outlines around distinct objects in the scene, with the outline colour giving an indication of how far from the viewer each object is. Depth estimation uses Depth Anything V2 (a pretrained monocular depth estimation model) which isn't perfect (note it gets the cables wrong) but generally works well and is relatively fast.
Typically, the nearest objects will be highlighted in red, into oranges (further), and the farthest objects will be highlighed in blue, into green (nearer).
Below is a low-resolution representation of the sort of depth heatmap that is output from Depth Anything V2, using the colour sheme outlined above:
Finally, we have an illustration of the output from the edge-detection processing. This is performed on the heatmap to look for objects that are set-apart in distance from their surroundings.
- Windows 11 (tested setup), but the approach is OS-agnostic.
- Python 3.10 (recommended for this environment).
- NVIDIA GPU with a recent driver (tested with RTX 4060 Laptop GPU).
- Internet access on first run to download model weights.
This repo is typically used from a MinGW64 bash shell on Windows, but it works from PowerShell too.
From MinGW64 bash:
python -m venv env
. env/Scripts/activate
python -m pip install --upgrade pipThis uses the official PyTorch CUDA wheel index:
pip install torch==2.9.0 torchvision==0.24.0 torchaudio==2.9.0 --index-url https://download.pytorch.org/whl/cu128pip install opencv-python pillow transformerspython -c "import torch; print(torch.cuda.is_available()); print(torch.version.cuda); print(torch.cuda.get_device_name(0) if torch.cuda.is_available() else None)"You should see True and your GPU name.
python depth_edges.py -s 0 -w -a 0.75 -b 3 -c -e 0.15 -g 32 -i 320 -t 0.5 -r 1python depth_edges.py --helppython depth_edges.py --source 0python depth_edges.py --source path/to/video.mp4python depth_edges.py --source 0 --out out.mp4-
Show debug windows (edge mask, blocky depth grid):
python depth_edges.py --source 0 --show-debug
-
Improve edge detection in low-contrast scenes (recommended outdoors):
python depth_edges.py --source 0 --contrast-boost
-
Control inference speed/quality trade-off:
python depth_edges.py --source 0 --infer-max-side 384
-
Control edge sensitivity / thickness:
python depth_edges.py --source 0 --edge-thresh 0.30 --dilate 2
-
Control depth dynamic range allocation (prevents horizon dominating):
python depth_edges.py --source 0 --knee-near-frac 0.25
-
Zoom captured frames (applied immediately after capture):
python depth_edges.py --source 0 --zoom 1.25
Tip: run python depth_edges.py -h to see the exact set of supported flags in your current version.
- The first run downloads the model checkpoint from Hugging Face and caches it locally.
- Depth estimation is relative: colours represent nearer/farther ordering, not physical distances.
- Check your NVIDIA driver is installed and up to date.
- Ensure you installed the CUDA-enabled PyTorch wheels (the
--index-url .../cu128line).
If cv2.imshow() behaves oddly under MinGW64, try running the same command from PowerShell to confirm it is not a terminal integration issue.
If you see weird path behaviour when passing file paths as arguments, you can disable MSYS argument conversion:
export MSYS2_ARG_CONV_EXCL="*"depth_edges.py– main scriptenv/– Python virtual environment (local, not committed)
Copyright (c) 2026, 7th software Ltd.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at:
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.


