#include #include #include #include #include #include #include "common.hpp" using namespace cv; using namespace std; using namespace cv::dnn; const string about = "Use this script for testing Object Tracking using OpenCV. \n\n" "Firstly, download required models using the download_models.py .\n" "Valid alias names are nanotrack, vit and dasiamrpn.\n\n" "To run:\n" "\t nanotrack: \n" "\t\t e.g: ./example_dnn_object_tracker nanotrack\n\n" "\t vit: \n" "\t\t e.g: ./example_dnn_object_tracker vit\n\n" "\t dasiamrpn: \n" "\t\t e.g: ./example_dnn_object_tracker dasiamrpn\n\n" "To switch between models in runtime, make sure all the models are downloaded using download_models.py\n"; const string param_keys = "{ help h | | Print help message }" "{ @alias | vit | An alias name of model to extract preprocessing parameters from models.yml file. }" "{ zoo | ../dnn/models.yml | An optional path to file with preprocessing parameters }" "{ input i | | Full path to input video folder, the specific camera index. (empty for camera 0) }" "{ tracking_thrs | 0.3 | Tracking score threshold. If a bbox of score >= 0.3, it is considered as found }"; const string backend_keys = format( "{ backend | default | Choose one of computation backends: " "default: automatically (by default), " "openvino: Intel's Deep Learning Inference Engine (https://software.intel.com/openvino-toolkit), " "opencv: OpenCV implementation, " "vkcom: VKCOM, " "cuda: CUDA, " "webnn: WebNN }"); const string target_keys = format( "{ target | cpu | Choose one of target computation devices: " "cpu: CPU target (by default), " "opencl: OpenCL, " "opencl_fp16: OpenCL fp16 (half-float precision), " "vpu: VPU, " "vulkan: Vulkan, " "cuda: CUDA, " "cuda_fp16: CUDA fp16 (half-float preprocess) }"); string keys = param_keys + backend_keys + target_keys; static void loadParser(const string &modelName, const string &zooFile) { // Load appropriate preprocessing arguments based on model name if (modelName == "vit") { keys += genPreprocArguments(modelName, zooFile, ""); } else if (modelName == "nanotrack") { keys += genPreprocArguments(modelName, zooFile, "nanotrack_head_"); keys += genPreprocArguments(modelName, zooFile, "nanotrack_back_"); } else if (modelName == "dasiamrpn") { keys += genPreprocArguments(modelName, zooFile, "dasiamrpn_"); keys += genPreprocArguments(modelName, zooFile, "dasiamrpn_kernel_r1_"); keys += genPreprocArguments(modelName, zooFile, "dasiamrpn_kernel_cls_"); } return; } static void createTracker(const string &modelName, CommandLineParser &parser, Ptr &tracker) { int backend = getBackendID(parser.get("backend")); int target = getTargetID(parser.get("target")); if (modelName == "dasiamrpn") { const string net = parser.get("dasiamrpn_model"); const string sha1 = parser.get("dasiamrpn_sha1"); const string kernel_cls1 = parser.get("dasiamrpn_kernel_cls_model"); const string kernel_cls_sha1 = parser.get("dasiamrpn_kernel_cls_sha1"); const string kernel_r1 = parser.get("dasiamrpn_kernel_r1_model"); const string kernel_sha1 = parser.get("dasiamrpn_kernel_r1_sha1"); TrackerDaSiamRPN::Params params; params.model = findModel(net, sha1); params.kernel_cls1 = findModel(kernel_cls1, kernel_cls_sha1); params.kernel_r1 = findModel(kernel_r1, kernel_sha1); params.backend = backend; params.target = target; tracker = TrackerDaSiamRPN::create(params); } else if (modelName == "nanotrack") { const string backbone = parser.get("nanotrack_back_model"); const string backSha1 = parser.get("nanotrack_back_sha1"); const string headneck = parser.get("nanotrack_head_model"); const string headSha1 = parser.get("nanotrack_head_sha1"); TrackerNano::Params params; params.backbone = findModel(backbone, backSha1); params.neckhead = findModel(headneck, headSha1); params.backend = backend; params.target = target; tracker = TrackerNano::create(params); } else if (modelName == "vit") { const string net = parser.get("model"); const string sha1 = parser.get("sha1"); float tracking_score_threshold = parser.get("tracking_thrs"); TrackerVit::Params params; params.net = findModel(net, sha1); params.backend = backend; params.target = target; params.tracking_score_threshold = tracking_score_threshold; tracker = TrackerVit::create(params); } else { cout<<"Pass the valid alias. Choices are {vit, nanotrack, dasiamrpn }."<("@alias"); const string zooFile = findFile(parser.get("zoo")); loadParser(modelName, zooFile); parser = CommandLineParser(argc, argv, keys); Ptr tracker; createTracker(modelName, parser, tracker); const string windowName = "TRACKING"; namedWindow(windowName, WINDOW_NORMAL); FontFace fontFace("sans"); int stdSize = 20; int stdWeight = 400; int stdImgSize = 512; int imgWidth = -1; int fontSize = 50; int fontWeight = 500; double alpha = 0.4; Rect selectRect; string inputName = parser.get("input"); string instructionLabel = "Press space bar to pause video to draw bounding box."; Rect banner; // Open a video file or an image file or a camera stream. VideoCapture cap; if (inputName.empty() || (isdigit(inputName[0]) && inputName.size() == 1)) { int c = inputName.empty() ? 0 : inputName[0] - '0'; cout << "Trying to open camera #" << c << " ..." << endl; if (!cap.open(c)) { cout << "Capture from camera #" << c << " didn't work. Specify -i=