#include #include using namespace cv; std::string genArgument(const std::string& argName, const std::string& help, const std::string& modelName, const std::string& zooFile, char key = ' ', std::string defaultVal = ""); std::string genPreprocArguments(const std::string& modelName, const std::string& zooFile, const std::string& prefix); std::string findFile(const std::string& filename); std::string findModel(const std::string& filename, const std::string& sha1); std::vector findAliases(std::string& zooFile, const std::string& sampleType); inline int getBackendID(const String& backend) { std::map backendIDs = { {"default", cv::dnn::DNN_BACKEND_DEFAULT}, {"openvino", cv::dnn::DNN_BACKEND_INFERENCE_ENGINE}, {"opencv", cv::dnn::DNN_BACKEND_OPENCV}, {"vkcom", cv::dnn::DNN_BACKEND_VKCOM}, {"cuda", cv::dnn::DNN_BACKEND_CUDA}, {"webnn", cv::dnn::DNN_BACKEND_WEBNN} }; if(backendIDs.find(backend) != backendIDs.end()){ return backendIDs[backend]; }else { throw std::invalid_argument("Invalid backend name: " + backend); } } inline int getTargetID(const String& target) { std::map targetIDs = { {"cpu", cv::dnn::DNN_TARGET_CPU}, {"opencl", cv::dnn::DNN_TARGET_OPENCL}, {"opencl_fp16", cv::dnn::DNN_TARGET_OPENCL_FP16}, {"vpu", cv::dnn::DNN_TARGET_MYRIAD}, {"vulkan", cv::dnn::DNN_TARGET_VULKAN}, {"cuda", cv::dnn::DNN_TARGET_CUDA}, {"cuda_fp16", cv::dnn::DNN_TARGET_CUDA_FP16} }; if(targetIDs.find(target) != targetIDs.end()){ return targetIDs[target]; }else { throw std::invalid_argument("Invalid target name: " + target); } } std::string genArgument(const std::string& argName, const std::string& help, const std::string& modelName, const std::string& zooFile, char key, std::string defaultVal) { if (!modelName.empty()) { FileStorage fs(zooFile, FileStorage::READ); if (fs.isOpened()) { FileNode node = fs[modelName]; if (!node.empty()) { FileNode value = node[argName]; if (argName.find("sha1") != std::string::npos) { std::string prefix = argName.substr(0, argName.find("sha1")); if (prefix == "config_"){ value = node[prefix+"load_info"]["sha1"]; } else{ value = node[prefix+"load_info"][argName]; } } if (argName.find("download_sha") != std::string::npos) { std::string prefix = argName.substr(0, argName.find("download_sha")); value = node[prefix+"load_info"][argName]; } if (!value.empty()) { if (value.isReal()) defaultVal = format("%f", (float)value); else if (value.isString()) defaultVal = (std::string)value; else if (value.isInt()) defaultVal = format("%d", (int)value); else if (value.isSeq()) { for (size_t i = 0; i < value.size(); ++i) { FileNode v = value[(int)i]; if (v.isInt()) defaultVal += format("%d ", (int)v); else if (v.isReal()) defaultVal += format("%f ", (float)v); else CV_Error(Error::StsNotImplemented, "Unexpected value format"); } } else CV_Error(Error::StsNotImplemented, "Unexpected field format"); } } } } return "{ " + argName + " " + key + " | " + defaultVal + " | " + help + " }"; } std::string findModel(const std::string& filename, const std::string& sha1) { if (filename.empty() || utils::fs::exists(filename)) return filename; if(!getenv("OPENCV_DOWNLOAD_CACHE_DIR")){ std::cout<< "[WARN] Please specify a path to model download directory in OPENCV_DOWNLOAD_CACHE_DIR environment variable"< findAliases(std::string& zooFile, const std::string& sampleType) { std::vector aliases; zooFile = findFile(zooFile); cv::FileStorage fs(zooFile, cv::FileStorage::READ); cv::FileNode root = fs.root(); for (const auto& node : root) { std::string alias = node.name(); cv::FileNode sampleNode = node["sample"]; if (!sampleNode.empty() && sampleNode.isString()) { std::string sampleValue = (std::string)sampleNode; if (sampleValue == sampleType) { aliases.push_back(alias); } } } return aliases; }