Depth Anything V2 (small)
C++ (Windows, Linux, MacOS / CUDA and Metal accelerated) port of https://github.com/DepthAnything/Depth-Anything-V2.
Example Input & Outputs
Inputs | Outputs |
![]() |
![]() |
Demo Code
1#include "blace_ai.h"
2#include <opencv2/opencv.hpp>
3
4// include the models you want to use
5#include "depth_anything_v2_v5_small_v3_ALL_export_version_v17.h"
6
7int main() {
8 ::blace::workload_management::BlaceWorld blace;
9
10 // register model at server
11 blace::util::registerModel(
12 depth_anything_v2_v5_small_v3_ALL_export_version_v17,
13 blace::util::getPathToExe());
14
15 // load image into op
16 auto exe_path = blace::util::getPathToExe();
17 std::filesystem::path photo_path = exe_path / "butterfly.jpg";
18 auto world_tensor_orig =
19 CONSTRUCT_OP(blace::ops::FromImageFileOp(photo_path.string()));
20
21 // interpolate to size consumable by model
22 auto interpolated = CONSTRUCT_OP(blace::ops::Interpolate2DOp(
23 world_tensor_orig, 700, 1288, blace::ml_core::BICUBIC, false, true));
24
25 // construct model inference arguments
26 blace::ml_core::InferenceArgsCollection infer_args;
27 infer_args.inference_args.device = blace::util::get_accelerator().value();
28
29 // construct inference operation
30 auto infer_op = CONSTRUCT_OP(blace::ops::InferenceOp(
31 depth_anything_v2_v5_small_v3_ALL_export_version_v17_IDENT,
32 {interpolated}, infer_args, 0));
33
34 // normalize depth to zero-one range
35 auto result_depth = CONSTRUCT_OP(blace::ops::NormalizeToZeroOneOP(infer_op));
36
37 // construct evaluator and evaluate to cv::Mat
38 blace::computation_graph::GraphEvaluator evaluator(result_depth);
39 auto cv_result = evaluator.evaluateToCVMat().value();
40
41 // multiply for plotting
42 cv_result *= 255.;
43
44 // save to disk and return
45 auto out_file = exe_path / "depth_result.png";
46 cv::imwrite(out_file.string(), cv_result);
47
48 return 0;
49}
Follow the 5 minute instructions to build and run the demo.
Tested on version v0.9.51 of blace.ai sdk. Might also work on newer or older releases (check if release notes of blace.ai state breaking changes).
Artifacts
Payload | Demo Project | Header |