Monocular-Depth

Twitter: @NorowaretaGemu License: MIT



ko-fi


Python
OpenCV
PowerShell Shell Batch


Monocular-Depth

Real-time depth estimation from a single webcam using MiDaS v2.1 and OpenCV’s DNN module. It shows the camera feed next to a depth map (brighter = closer). Press q to quit.

How to Run:

Quick Start

Needs Python 3 installed. Run one script and it does everything:

Windows:

Unix-like systems (Linux/macOS):

On the first run it creates a psdenv virtual environment, installs requirements.txt into it and downloads the MiDaS v2.1 Large model (~400 MB) to models/. After that it skips whatever is already there and starts the depth demo straight away.


Download the Model Manually

The model is not included in the repo. The run scripts download it for you, or download one of these and put it in a models/ folder in the repo root:

Model File Notes
MiDaS v2.1 Large model-f6b98070.onnx Better quality, slower (384x384)
MiDaS v2.1 Small model-small.onnx Faster (256x256), used by the C++ version
Monocular-Depth/
  models/
    model-f6b98070.onnx
    model-small.onnx

The Python script uses the Large model if present, otherwise the Small one. It runs on the GPU when OpenCV is built with CUDA, otherwise on the CPU (the pip opencv-python package is CPU-only).

PowerShell:

New-Item -ItemType Directory -Force models
Invoke-WebRequest https://github.com/isl-org/MiDaS/releases/download/v2_1/model-f6b98070.onnx -OutFile models/model-f6b98070.onnx
Invoke-WebRequest https://github.com/isl-org/MiDaS/releases/download/v2_1/model-small.onnx -OutFile models/model-small.onnx

Linux/macOS:

mkdir -p models
curl -L -o models/model-f6b98070.onnx https://github.com/isl-org/MiDaS/releases/download/v2_1/model-f6b98070.onnx
curl -L -o models/model-small.onnx https://github.com/isl-org/MiDaS/releases/download/v2_1/model-small.onnx


Install Requirements

Using Python directly:

pip install -r requirements.txt

Or run:

The run scripts above do this for you inside the psdenv virtual environment.


Run main.py

Opens a menu to pick a script:

python main.py


Build the C++ Version

The C++ version (cpp/monocularDepth.cpp) uses models/model-small.onnx. It needs g++ and OpenCV with pkg-config. On Windows the easiest way is MSYS2:

pacman -S mingw-w64-ucrt-x86_64-gcc mingw-w64-ucrt-x86_64-opencv mingw-w64-ucrt-x86_64-pkgconf

Build:

g++ cpp/monocularDepth.cpp -o main.exe $(pkg-config --cflags --libs opencv4)

or Run:

Run from the repo root (so models/ is found):


Docker

The dockerfile builds a Linux image for the Python version. The webcam and windows need access to the host, so this works on Linux with X11:

docker build -t monocular-depth .
docker run -it --device /dev/video0 -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix -v "$(pwd)/models:/app/models" monocular-depth


© Cursed Entertainment


CursedEntertainment Logo