From ca639958aa34b459b3b17055e7ddc99ff1b769d7 Mon Sep 17 00:00:00 2001 From: Biparnak Roy <54219771+biparnakroy@users.noreply.github.com> Date: Sat, 25 Jul 2020 21:05:44 +0530 Subject: [PATCH] added YOLO output --- include/tkDNN/DetectionNN.h | 232 +++++++++++++++++++++--------------- 1 file changed, 133 insertions(+), 99 deletions(-) diff --git a/include/tkDNN/DetectionNN.h b/include/tkDNN/DetectionNN.h index 030cf8f..c70247d 100644 --- a/include/tkDNN/DetectionNN.h +++ b/include/tkDNN/DetectionNN.h @@ -3,11 +3,13 @@ #include #include -#include +#include #include #include #include "utils.h" +#include + #include #include #include @@ -21,39 +23,42 @@ #include #endif +namespace tk +{ + namespace dnn + { -namespace tk { namespace dnn { + class DetectionNN + { -class DetectionNN { + protected: + tk::dnn::NetworkRT *netRT = nullptr; + dnnType *input_d; - protected: - tk::dnn::NetworkRT *netRT = nullptr; - dnnType *input_d; + std::vector originalSize; - std::vector originalSize; + cv::Scalar colors[256]; - cv::Scalar colors[256]; - - int nBatches = 1; + int nBatches = 1; #ifdef OPENCV_CUDACONTRIB - cv::cuda::GpuMat bgr[3]; - cv::cuda::GpuMat imagePreproc; + cv::cuda::GpuMat bgr[3]; + cv::cuda::GpuMat imagePreproc; #else - cv::Mat bgr[3]; - cv::Mat imagePreproc; - dnnType *input; + 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; + 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. * @@ -61,21 +66,21 @@ class DetectionNN { * @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; + 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*/ + 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; + 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(){}; + DetectionNN(){}; + ~DetectionNN(){}; - /** + /** * Method used to inialize the class, allocate memory and compute * needed data. * @@ -84,9 +89,9 @@ class DetectionNN { * @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; - - /** + 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. @@ -97,87 +102,116 @@ class DetectionNN { * @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); + void update(std::vector &frames, const int cur_batches = 1, bool save_times = false, std::ofstream *times = nullptr, const bool mAP = false) { - TKDNN_TSTART - for(int bi=0; bi 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; bi < cur_batches; ++bi) + { + if (!frames[bi].data) + FatalError("No image data feed to detection"); + originalSize.push_back(frames[bi].size()); + preprocess(frames[bi], bi); + } + TKDNN_TSTOP + if (save_times) + *times << t_ns << ";"; + } + + //do inference + tk::dnn::dataDim_t dim = netRT->input_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 << t_ns << ";"; + } + + batchDetected.clear(); + { + TKDNN_TSTART + for (int bi = 0; bi < cur_batches; ++bi) + postprocess(bi, mAP); + TKDNN_TSTOP + if (save_times) + *times << t_ns << "\n"; } - TKDNN_TSTOP - if(save_times) *times<input_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; + void draw(std::vector &frames, bool ext_yolo) + { + 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; + //yolo detctions output + std::string yoloBox; + float Yx, Yy, Yw, Yh; + cv::Size sz = frames[0].size(); + int imageWidth = sz.width; + int imageHeight = sz.height; - for(int bi=0; bi