Depths-CPP provides single c++ header with high-performance application designed for real-time depth estimation using models from Depth-Anything-V2. Leveraging the power of ONNX Runtime and OpenCV, this project provides seamless integration for image, video, and live camera inference. Whether you're developing for research, production, or hobbyist projects, this application offers flexibility and efficiency.
It features:
- Multi-threaded architecture
- Adaptive batch size
- Real-time display options
- ONNX GPU acceleration
- Cross-platform compatibility
Supports flexible execution providers: TensorRT, CUDA, or CPU.
-
[2025.04.01] 🔥 Depths-CPP v2 Released
The entire codebase has been reworked for improved efficiency, cleaner architecture, and added support for batch processing. -
[2024.12.15] 🚀 Initial Release of Depths-CPP
Launched the first version of Depths-CPP — a high-performance depth estimation application in C++.
#include <opencv2/opencv.hpp>
#include <iostream>
#include <string>
#include "depth_anything.hpp" // Ensure this is in your include path
int main() {
std::string modelPath = "models/vits_metric_indoor.onnx";
std::string imagePath = "data/indoor.jpg";
bool useCuda = false; // Change to true if using a CUDA-enabled GPU
DepthAnything depthEstimator(modelPath, useCuda);
cv::Mat inputImage = cv::imread(imagePath, cv::IMREAD_COLOR);
cv::Mat depthMap = depthEstimator.predict(inputImage);
cv::Mat depthVis;
cv::normalize(depthMap, depthVis, 0, 255, cv::NORM_MINMAX, CV_8U);
cv::Mat depthColor;
cv::applyColorMap(depthVis, depthColor, cv::COLORMAP_JET);
cv::imshow("Original", inputImage);
cv::imshow("Depth", depthColor);
cv::waitKey(0);
return 0;
}Note: For more usage, check the source files: image_depth_estimation.cpp, video_depth_estimation.cpp, camera_depth_estimation.cpp.
-
Multiple Depth Anything V2 Models: Supports Depth Anything V2 with standard and quantized ONNX models for flexibility in use cases.
-
ONNX Runtime Integration: Leverages ONNX Runtime for optimized inference on both CPU and GPU, ensuring high performance.
- Dynamic Shapes Handling: Adapts automatically to varying input sizes for improved versatility.
- Execution Providers: Configures sessions for CPU or GPU (e.g.,
CUDAExecutionProviderfor GPU support). - Input/Output Shape Management: Manages dynamic input tensor shapes per model specifications.
- Batch Processing: Supports processing multiple images, currently focused on single-image input.
-
Real-Time Inference: Capable of processing images, videos, and live camera feeds instantly.
-
Cross-Platform Support: Fully compatible with Linux, macOS, and Windows environments.
-
Easy-to-Use Scripts: Includes shell scripts for straightforward building and running of different inference modes.
- C++17 or later
- OpenCV ≥ 4.5.5
- CMake ≥ 3.14
- ONNX Runtime: Tested with version 1.16.3 and 1.19.2, backward compatibility [Installed and linked automatically during the build].
- CUDA Toolkit ≥ 11.0 (if using GPU)
git clone https://github.com/Geekgineer/Depths-CPP
cd Depths-CPPEnsure OpenCV is installed, and configure ONNX Runtime version inside build.sh. Then:
./build.shNote: This script will download onnxruntime headers, create a build directory, configure the project, and compile the source code. Upon successful completion, the executable files (camera_inference, image_inference, video_inference) will be available in the build directory.
Run inference from camera, image, or video:
./run_image.sh./run_video.sh./run_camera.shWhile ONNX provides a cross-platform format for model compatibility, exporting the model directly for the target device or optimizing it for specific hardware can significantly improve performance. To achieve the best inference speed and resource efficiency, it's generally recommended to tailor the export process to the hardware on which the model will run.
The project includes several pre-trained and pre-exported standard DepthAnything2 models, located in models. However, it’s not recommended to use these directly. Instead, you should always export your PyTorch models using the export_depthanything_onnx.ipynbscript.
| Model Name | Resolution | Type | Notes |
|---|---|---|---|
vits.onnx |
384x384 | FP32 Normilized Depth | ViT-Small base model |
vits_quint8.onnx |
384x384 | UNINT8 Normilized Depth Quantized | Compact, edge-optimized |
vits_metric_indoor.onnx |
384x384 | FP32 Metric Depth | Indoor scenes |
vits_metric_outdoor.onnx |
384x384 | FP32 Metric Depth | Outdoor scenes |
| Key | Action |
|---|---|
q / ESC |
Quit |
m |
Toggle display mode |
+ / - |
Adjust frame skipping |
Fork → Edit → PR!
Contributions are welcome! Please follow these steps to contribute:
-
Fork the Repository: Click the "Fork" button at the top-right corner of this repository to create a personal copy.
-
Clone Your Fork:
git clone https://github.com/Geekgineer/Depth-Anything-V2-CPP cd Depth-Anything-V2-CPP -
Create a New Branch:
git checkout -b feature/YourFeatureName
-
Make Your Changes: Implement your feature or bug fix.
-
Commit Your Changes:
git commit -m "Add feature: YourFeatureName" -
Push to Your Fork:
git push origin feature/YourFeatureName
-
Create a Pull Request: Navigate to the original repository and click "New Pull Request" to submit your changes for review.
Licensed under MIT License.
- https://github.com/DepthAnything/Depth-Anything-V2
- https://github.com/spacewalk01/depth-anything-tensorrt
- https://github.com/zhujiajian98/Depth-Anythingv2-TensorRT-python
- https://github.com/fabio-sim/Depth-Anything-ONNX
- https://huggingface.co/spaces/Xenova/webgpu-realtime-depth-estimation
- https://huggingface.co/onnx-community


