Scroll Top

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
Input
Depth Input
Input
Depth Result

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
 9  // register model at server
10  blace::util::registerModel(
11      depth_anything_v2_v5_small_v3_ALL_export_version_v17,
12      blace::util::getPathToExe());
13
14  // load image into op
15  auto exe_path = blace::util::getPathToExe();
16  std::filesystem::path photo_path = exe_path / "butterfly.jpg";
17  auto world_tensor_orig =
18      CONSTRUCT_OP_GET(blace::ops::FromImageFileOp(photo_path.string()));
19
20  // interpolate to size consumable by model
21  auto interpolated = CONSTRUCT_OP_GET(blace::ops::Interpolate2DOp(
22      world_tensor_orig, 700, 1288, blace::ml_core::BICUBIC, false, true));
23
24  // construct model inference arguments
25  blace::ml_core::InferenceArgsCollection infer_args;
26  infer_args.inference_args.device = blace::util::get_accelerator().value();
27
28  // construct inference operation
29  auto infer_op = CONSTRUCT_OP_GET(blace::ops::InferenceOp(
30      depth_anything_v2_v5_small_v3_ALL_export_version_v17_IDENT,
31      {interpolated}, infer_args, 0));
32
33  // normalize depth to zero-one range
34  auto result_depth =
35      CONSTRUCT_OP_GET(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  // unload all models before program exits
49  blace::util::unloadModels();
50
51  return 0;
52}

Follow the 5 minute instructions to build and run the demo.

Tested on version v0.9.28 of blace.ai sdk.

Artifacts

Payload Demo Project Header

License