#ifndef DETECTIONNN_H #define DETECTIONNN_H #include #include #include #include #include #include "utils.h" #include #include #include #include "tkdnn.h" // #define OPENCV_CUDACONTRIB //if OPENCV has been compiled with CUDA and contrib. #ifdef OPENCV_CUDACONTRIB #include #include #endif namespace tk { namespace dnn { class DetectionNN { protected: tk::dnn::NetworkRT *netRT = nullptr; dnnType *input_d; std::vector originalSize; cv::Scalar colors[256]; int nBatches = 1; #ifdef OPENCV_CUDACONTRIB cv::cuda::GpuMat bgr[3]; cv::cuda::GpuMat imagePreproc; #else cv::Mat bgr[3]; cv::Mat imagePreproc; dnnType *input; #endif /** * This method preprocess the image, before feeding it to the NN. * * @param frame original frame to adapt for inference. * @param bi batch index */ virtual void preprocess(cv::Mat &frame, const int bi=0) = 0; /** * This method postprocess the output of the NN to obtain the correct * boundig boxes. * * @param bi batch index * @param mAP set to true only if all the probabilities for a bounding * box are needed, as in some cases for the mAP calculation */ virtual void postprocess(const int bi=0,const bool mAP=false) = 0; public: int classes = 0; float confThreshold = 0.3; /*threshold on the confidence of the boxes*/ std::vector detected; /*bounding boxes in output*/ std::vector> batchDetected; /*bounding boxes in output*/ std::vector stats; /*keeps track of inference times (ms)*/ std::vector classesNames; DetectionNN() {}; ~DetectionNN(){}; /** * Method used to inialize the class, allocate memory and compute * needed data. * * @param tensor_path path to the rt file og the NN. * @param n_classes number of classes for the given dataset. * @param n_batches maximum number of batches to use in inference * @return true if everything is correct, false otherwise. */ virtual bool init(const std::string& tensor_path, const int n_classes=80, const int n_batches=1) = 0; /** * This method performs the whole detection of the NN. * * @param frames frames to run detection on. * @param cur_batches number of batches to use in inference * @param save_times if set to true, preprocess, inference and postprocess times * are saved on a csv file, otherwise not. * @param times pointer to the output stream where to write times * @param mAP set to true only if all the probabilities for a bounding * box are needed, as in some cases for the mAP calculation */ void update(std::vector& frames, const int cur_batches=1, bool save_times=false, std::ofstream *times=nullptr, const bool mAP=false){ if(save_times && times==nullptr) FatalError("save_times set to true, but no valid ofstream given"); if(cur_batches > nBatches) FatalError("A batch size greater than nBatches cannot be used"); originalSize.clear(); if(TKDNN_VERBOSE) printCenteredTitle(" TENSORRT detection ", '=', 30); { TKDNN_TSTART for(int bi=0; biinput_dim; dim.n = cur_batches; { if(TKDNN_VERBOSE) dim.print(); TKDNN_TSTART netRT->infer(dim, input_d); TKDNN_TSTOP if(TKDNN_VERBOSE) dim.print(); stats.push_back(t_ns); if(save_times) *times<& frames) { tk::dnn::box b; int x0, w, x1, y0, h, y1; int objClass; std::string det_class; int baseline = 0; float font_scale = 0.5; int thickness = 2; for(int bi=0; bi