/** * Unified Depth node demo. * * The Depth node picks an algorithm and backend config automatically from the connected device, * target FPS, and/or stereo resolution (Algorithm::AUTO). You can also pin the algorithm yourself * via CLI or Depth::build(). * * Supported algorithms: AUTO, STEREO, NEURAL, NEURAL_ASSISTED_STEREO, TOF, GPU_STEREO. * * Configure the node via CLI to exercise Depth::build() overloads (fps-only, algorithm + fps/res, * algorithm + config) or pre-built user stereo cameras. */ #include #include #include #include #include #include #include #include #include #include #include #include "depthai/common/DeviceModelZoo.hpp" #include "depthai/depthai.hpp" #include "depthai/pipeline/node/Camera.hpp" #include "depthai/pipeline/node/Depth.hpp" #include "depthai/pipeline/node/StereoDepth.hpp" namespace { cv::Mat colorizeConfidence(const cv::Mat& frame) { if(frame.empty() || frame.channels() != 1) { return cv::Mat::zeros(frame.size(), CV_8UC3); } cv::Mat normalized; if(frame.depth() == CV_16U) { double maxValue = 0.0; cv::minMaxLoc(frame, nullptr, &maxValue); if(maxValue <= 0.0) { return cv::Mat::zeros(frame.size(), CV_8UC3); } cv::Mat scaled; frame.convertTo(scaled, CV_32F, 255.0 / maxValue); scaled.convertTo(normalized, CV_8U); } else { frame.convertTo(normalized, CV_8U); } cv::Mat colorized; cv::applyColorMap(normalized, colorized, cv::COLORMAP_JET); return colorized; } const std::map& algorithmMap() { static const std::map map = { {"auto", dai::node::Depth::Algorithm::AUTO}, {"stereo", dai::node::Depth::Algorithm::STEREO}, {"neural", dai::node::Depth::Algorithm::NEURAL}, {"neural_assisted_stereo", dai::node::Depth::Algorithm::NEURAL_ASSISTED_STEREO}, {"tof", dai::node::Depth::Algorithm::TOF}, {"gpu_stereo", dai::node::Depth::Algorithm::GPU_STEREO}, }; return map; } const char* algorithmName(dai::node::Depth::Algorithm algorithm) { for(const auto& [name, value] : algorithmMap()) { if(value == algorithm) { return name.c_str(); } } return "unknown"; } std::optional parseAlgorithm(const std::string& name) { const auto it = algorithmMap().find(name); if(it == algorithmMap().end()) { return std::nullopt; } return it->second; } std::optional parseConfig(const std::string& name) { static const std::map modelMap = { {"NEURAL_DEPTH_1248X780", dai::DeviceModelZoo::NEURAL_DEPTH_1248X780}, {"NEURAL_DEPTH_768X480", dai::DeviceModelZoo::NEURAL_DEPTH_768X480}, {"NEURAL_DEPTH_576X360", dai::DeviceModelZoo::NEURAL_DEPTH_576X360}, {"NEURAL_DEPTH_480X300", dai::DeviceModelZoo::NEURAL_DEPTH_480X300}, {"NEURAL_DEPTH_384X240", dai::DeviceModelZoo::NEURAL_DEPTH_384X240}, {"NEURAL_DEPTH_1056X660", dai::DeviceModelZoo::NEURAL_DEPTH_1056X660}, {"NEURAL_DEPTH_960X600", dai::DeviceModelZoo::NEURAL_DEPTH_960X600}, {"NEURAL_DEPTH_864X540", dai::DeviceModelZoo::NEURAL_DEPTH_864X540}, {"NEURAL_DEPTH_288X180", dai::DeviceModelZoo::NEURAL_DEPTH_288X180}, {"NEURAL_DEPTH_192X120", dai::DeviceModelZoo::NEURAL_DEPTH_192X120}, {"NEURAL_DEPTH_EXTRA_LARGE", dai::DeviceModelZoo::NEURAL_DEPTH_EXTRA_LARGE}, {"NEURAL_DEPTH_LARGE", dai::DeviceModelZoo::NEURAL_DEPTH_LARGE}, {"NEURAL_DEPTH_MEDIUM", dai::DeviceModelZoo::NEURAL_DEPTH_MEDIUM}, {"NEURAL_DEPTH_SMALL", dai::DeviceModelZoo::NEURAL_DEPTH_SMALL}, {"NEURAL_DEPTH_NANO", dai::DeviceModelZoo::NEURAL_DEPTH_NANO}, }; static const std::map presetMap = { {"FAST_ACCURACY", dai::node::StereoDepth::PresetMode::FAST_ACCURACY}, {"FAST_DENSITY", dai::node::StereoDepth::PresetMode::FAST_DENSITY}, {"DEFAULT", dai::node::StereoDepth::PresetMode::DEFAULT}, {"FACE", dai::node::StereoDepth::PresetMode::FACE}, {"HIGH_DETAIL", dai::node::StereoDepth::PresetMode::HIGH_DETAIL}, {"ROBOTICS", dai::node::StereoDepth::PresetMode::ROBOTICS}, {"DENSITY", dai::node::StereoDepth::PresetMode::DENSITY}, {"ACCURACY", dai::node::StereoDepth::PresetMode::ACCURACY}, }; if(const auto modelIt = modelMap.find(name); modelIt != modelMap.end()) { return dai::node::Depth::Config{modelIt->second}; } if(const auto presetIt = presetMap.find(name); presetIt != presetMap.end()) { return dai::node::Depth::Config{presetIt->second}; } return std::nullopt; } struct CliOptions { std::string ip; std::optional fps; std::optional width; std::optional height; std::optional algorithm; std::optional config; bool userCameras{false}; }; std::optional> sizeFromOptions(const CliOptions& options) { if(!options.width && !options.height) { return std::nullopt; } if(!options.width || !options.height) { throw std::runtime_error("--width and --height must be specified together"); } return std::make_pair(*options.width, *options.height); } void buildUserStereoCameras(dai::Pipeline& pipeline, const CliOptions& options) { const auto device = pipeline.getDefaultDevice(); if(device == nullptr) { throw std::runtime_error("Connect a device (host-only pipeline cannot use Depth)."); } const auto stereoPairs = device->getStereoPairs(); if(stereoPairs.empty()) { throw std::runtime_error("This device has no stereo pair; Depth cannot run."); } const auto stereoPair = stereoPairs[0]; const auto size = sizeFromOptions(options); const std::optional fps = options.fps; pipeline.create()->build(stereoPair.left, size, fps); pipeline.create()->build(stereoPair.right, size, fps); } void configureDepth(const std::shared_ptr& depth, const CliOptions& options) { const auto fps = options.userCameras ? std::nullopt : options.fps; const auto size = options.userCameras ? std::nullopt : sizeFromOptions(options); const auto algorithm = options.algorithm ? parseAlgorithm(*options.algorithm) : std::nullopt; const auto config = options.config ? parseConfig(*options.config) : std::nullopt; if(options.algorithm && !algorithm) { throw std::runtime_error("Invalid --algorithm value"); } if(options.config && !config) { throw std::runtime_error("Unknown --config value. Use a DeviceModelZoo or StereoDepth.PresetMode name."); } if(algorithm && config) { depth->build(*algorithm, *config, fps, size); } else if(algorithm) { depth->build(*algorithm, fps, size); } else if(fps && size) { depth->build(dai::node::Depth::Algorithm::AUTO, *fps, size); } else if(fps) { depth->build(*fps); } else if(size) { depth->build(dai::node::Depth::Algorithm::AUTO, std::nullopt, size); } } void printResolvedConfig(const dai::node::Depth::Config& config) { std::visit( [](const auto& value) { using T = std::decay_t; if constexpr(std::is_same_v) { std::cout << "none"; } else if constexpr(std::is_same_v) { std::cout << static_cast(value); } else { std::cout << static_cast(value); } }, config); std::cout << std::endl; } dai::Pipeline makePipeline(const CliOptions& options) { if(options.ip.empty()) { return dai::Pipeline(); } auto device = std::make_shared(dai::DeviceInfo(options.ip)); return dai::Pipeline(device); } } // namespace int main(int argc, char** argv) { argparse::ArgumentParser program("unified_depth", "1.0.0"); program.add_description( "Unified Depth node demo.\n\n" "The Depth node picks an algorithm and backend config automatically from the connected device, " "target FPS, and/or stereo resolution (Algorithm::AUTO). You can also pin the algorithm yourself " "via CLI or Depth::build().\n\n" "Supported algorithms: AUTO, STEREO, NEURAL, NEURAL_ASSISTED_STEREO, TOF, GPU_STEREO.\n\n" "Configure the node via CLI to exercise Depth::build() overloads (fps-only, algorithm + fps/res, " "algorithm + config) or pre-built user stereo cameras."); program.add_argument("--ip").default_value(std::string("")).help("Device IP for TCP/IP (e.g. PoE)"); program.add_argument("--fps").scan<'g', float>().help("Stereo camera FPS for Depth.build() or user cameras"); program.add_argument("--width").scan<'u', uint32_t>().help("Stereo frame width (requires --height)"); program.add_argument("--height").scan<'u', uint32_t>().help("Stereo frame height (requires --width)"); program.add_argument("--algorithm").help("Depth backend: auto, stereo, neural, neural_assisted_stereo, tof, gpu_stereo"); program.add_argument("--config").help("DeviceModelZoo (NEURAL_*) or StereoDepth.PresetMode (DEFAULT, ...)"); program.add_argument("--user-cameras").flag().help("Pre-build stereo Camera nodes instead of using Depth.build() for fps/res"); try { program.parse_args(argc, argv); } catch(const std::runtime_error& err) { std::cerr << err.what() << '\n'; std::cerr << program; return EXIT_FAILURE; } CliOptions options{}; options.ip = program.get("--ip"); if(program.is_used("--fps")) { options.fps = program.get("--fps"); } if(program.is_used("--width")) { options.width = program.get("--width"); } if(program.is_used("--height")) { options.height = program.get("--height"); } if(program.is_used("--algorithm")) { options.algorithm = program.get("--algorithm"); } if(program.is_used("--config")) { options.config = program.get("--config"); } options.userCameras = program.get("--user-cameras"); if(options.config && !options.algorithm) { std::cerr << "--config requires --algorithm\n"; return EXIT_FAILURE; } try { sizeFromOptions(options); } catch(const std::runtime_error& err) { std::cerr << err.what() << '\n'; return EXIT_FAILURE; } try { dai::Pipeline pipeline = makePipeline(options); if(options.userCameras) { buildUserStereoCameras(pipeline, options); } auto depthNode = pipeline.create(); configureDepth(depthNode, options); auto depthQueue = depthNode->depth().createOutputQueue(); auto confidenceQueue = depthNode->confidence().createOutputQueue(); pipeline.build(); std::cout << "Resolved algorithm: " << algorithmName(depthNode->getResolvedAlgorithm()) << std::endl; std::cout << "Resolved config: "; printResolvedConfig(depthNode->getResolvedConfig()); pipeline.start(); while(pipeline.isRunning()) { auto depthFrame = depthQueue->get(); auto confidenceFrame = confidenceQueue->get(); if(depthFrame != nullptr) { cv::imshow("depth", dai::utility::colorizeDepthFrame(*depthFrame).getCvFrame()); } if(confidenceFrame != nullptr) { cv::imshow("confidence", colorizeConfidence(confidenceFrame->getFrame())); } if(cv::waitKey(1) == 'q') { pipeline.stop(); break; } } cv::destroyAllWindows(); } catch(const std::exception& ex) { std::cerr << ex.what() << '\n'; return EXIT_FAILURE; } return EXIT_SUCCESS; }