From 1ba73b82f20a0f2297648f25b377c84a5a031a2d Mon Sep 17 00:00:00 2001 From: Jeffrey_Li <412100639@qq.com> Date: Sun, 22 Oct 2023 19:21:30 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E6=A0=87=E6=A1=86=E9=87=8D?= =?UTF-8?q?=E5=8F=A0=E6=97=A0=E6=B3=95=E8=AF=86=E5=88=AB=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cigarette/Cleanthread.cpp | 2 +- Cigarette/alg_jd.cpp | 102 +++++++++++++++++--------------------- Cigarette/alg_jd.h | 12 ++++- 3 files changed, 58 insertions(+), 58 deletions(-) diff --git a/Cigarette/Cleanthread.cpp b/Cigarette/Cleanthread.cpp index 9d52d14..b0d274f 100644 --- a/Cigarette/Cleanthread.cpp +++ b/Cigarette/Cleanthread.cpp @@ -92,8 +92,8 @@ void CleanWorkThread::CleanImageFile(const QString& path, const qint64& delDays) QRegExpValidator v(rx, 0); int pos = 0; int match; - match = v.validate(fileInfo.fileName(), pos); QString name = fileInfo.fileName(); + match = v.validate(name, pos); if (match == 2) { QDate delDate = QDate::currentDate(); diff --git a/Cigarette/alg_jd.cpp b/Cigarette/alg_jd.cpp index 6f1176d..ce56d5b 100644 --- a/Cigarette/alg_jd.cpp +++ b/Cigarette/alg_jd.cpp @@ -3,17 +3,6 @@ extern SysConf g_sys_conf; -// Remove the bounding boxes with low confidence using non-maxima suppression -static void post_process(cv::Mat& frame, std::vector& outs, std::vector > &results); - -static void post_process_batch(std::vector& vec_frame, std::vector& outs, std::vector > > &vec_results); - -// Get the names of the output layers -static std::vector getOutputsNames(const cv::dnn::Net& net); - -// Draw the predicted bounding box -static void drawPred(int classId, float conf, int left, int top, int right, int bottom, cv::Mat& frame); - // Initialize the parameters static float confThreshold = g_sys_conf.ConfThreshold*0.01; // Confidence threshold static float nmsThreshold = 0.4; // Non-maximum suppression threshold @@ -164,9 +153,9 @@ void AlgJd::analyse(cv::Mat vec_in, std::vector >& vec_ } // Get the names of the output layers -static std::vector getOutputsNames(const cv::dnn::Net& net) +std::vector AlgJd::getOutputsNames(const cv::dnn::Net& net) { - static std::vector names; + std::vector names; if (names.empty()) { //Get the indices of the output layers, i.e. the layers with unconnected outputs @@ -183,12 +172,11 @@ static std::vector getOutputsNames(const cv::dnn::Net& net) return names; } -static void post_process(cv::Mat& frame, std::vector& outs, std::vector > &results) +void AlgJd::post_process(cv::Mat& frame, std::vector& outs, std::vector > &results) { - std::vector classIds; - std::vector confidences; - std::vector boxes; - + std::vector < std::vector> classIds(classes.size()); + std::vector < std::vector> confidences(classes.size()); + std::vector < std::vector> boxes(classes.size()); for (size_t i = 0; i < outs.size(); ++i) { // Scan through all the bounding boxes output from the network and keep only the @@ -212,9 +200,9 @@ static void post_process(cv::Mat& frame, std::vector& outs, std::vector int left = centerX - width / 2; int top = centerY - height / 2; - classIds.push_back(classIdPoint.x); - confidences.push_back((float)confidence); - boxes.push_back(cv::Rect(left, top, width, height)); + classIds[classIdPoint.x].push_back(classIdPoint.x); + confidences[classIdPoint.x].push_back((float)confidence); + boxes[classIdPoint.x].push_back(cv::Rect(left, top, width, height)); } } } @@ -222,21 +210,24 @@ static void post_process(cv::Mat& frame, std::vector& outs, std::vector // Perform non maximum suppression to eliminate redundant overlapping boxes with // lower confidences - std::vector indices; - cv::dnn::NMSBoxes(boxes, confidences, confThreshold, nmsThreshold, indices); - for (size_t i = 0; i < indices.size(); ++i) + for (size_t i = 0; i < classes.size(); ++i) { - int idx = indices[i]; - cv::Rect box = boxes[idx]; - drawPred(classIds[idx], confidences[idx], box.x, box.y, - box.x + box.width, box.y + box.height, frame); - results.push_back(std::make_pair(classIds[idx], box)); + std::vector indices; + cv::dnn::NMSBoxes(boxes[i], confidences[i], confThreshold, nmsThreshold, indices); + for (size_t j = 0; j < indices.size(); ++j) + { + int idx = indices[j]; + cv::Rect box = boxes[i][idx]; + drawPred(classIds[i][idx], confidences[i][idx], box.x, box.y, + box.x + box.width, box.y + box.height, frame); + results.push_back(std::make_pair(classIds[i][idx], box)); + } } } // Draw the predicted bounding box -static void drawPred(int classId, float conf, int left, int top, int right, int bottom, cv::Mat& frame) +void AlgJd::drawPred(int classId, float conf, int left, int top, int right, int bottom, cv::Mat& frame) { cv::Scalar color; if(classId==0) @@ -300,18 +291,18 @@ void AlgJd::detect_batch(std::vector& vec_in, std::vector &vec } } -static void post_process_batch(std::vector& vec_frame, std::vector& outs, std::vector > > &vec_results) +void AlgJd::post_process_batch(std::vector& vec_frame, std::vector& outs, std::vector > > &vec_results) { int batch = vec_frame.size(); double confidence;/// for (int k = 0; k < batch; k++) { - std::vector classIds; - std::vector confidences; - std::vector boxes; - //std::cout << "outs.size()\t" << outs.size() << std::endl; - //std::cout << "Type\t" << outs[0].type() << std::endl; + std::vector < std::vector> classIds(classes.size()); + std::vector < std::vector> confidences(classes.size()); + std::vector < std::vector> boxes(classes.size()); + //std::cout << "outs.size()\t" << outs.size() << std::endl; + //std::cout << "Type\t" << outs[0].type() << std::endl; for (size_t i = 0; i < outs.size(); ++i) { //std::cout << "outs.dims\t" << outs[i].dims << std::endl; @@ -341,34 +332,33 @@ static void post_process_batch(std::vector& vec_frame, std::vector indices; - cv::dnn::NMSBoxes(boxes, confidences, confThreshold, nmsThreshold, indices); - std::vector > results; - for (size_t i = 0; i < indices.size(); ++i) - { - int idx = indices[i]; - cv::Rect box = boxes[idx]; - - if (confidences[idx] > g_sys_conf.ConfThreshold * 0.01)///识别度低于阈值NG处理 + // Perform non maximum suppression to eliminate redundant overlapping boxes with + // lower confidences + for (size_t i = 0; i < classes.size(); ++i) + { + std::vector indices; + cv::dnn::NMSBoxes(boxes[i], confidences[i], confThreshold, nmsThreshold, indices); + for (size_t j = 0; j < indices.size(); ++j) { - if (box.width > 15) - {//识别框宽度大于15显示识别,小于认为无胶点,NG处理 - drawPred(classIds[idx], confidences[idx], box.x, box.y, + int idx = indices[j]; + cv::Rect box = boxes[i][idx]; + if (confidences[i][idx] > g_sys_conf.ConfThreshold * 0.01)///识别度低于阈值NG处理 + { + if (box.width > 15) + {//识别框宽度大于15显示识别,小于认为无胶点,NG处理 + drawPred(classIds[i][idx], confidences[i][idx], box.x, box.y, box.x + box.width, box.y + box.height, vec_frame[k]); - results.push_back(std::make_pair(classIds[idx], box)); + results.push_back(std::make_pair(classIds[i][idx], box)); + } } } - } vec_results.push_back(results); } diff --git a/Cigarette/alg_jd.h b/Cigarette/alg_jd.h index 4f19712..36e4259 100644 --- a/Cigarette/alg_jd.h +++ b/Cigarette/alg_jd.h @@ -15,11 +15,21 @@ class AlgJd bool init(QString model_path, QString model_name); bool test_detect(); bool test_detect_batcht(int shoot); - int detect(cv::Mat& in, cv::Mat &out, std::vector > &results); + int detect(cv::Mat& in, cv::Mat &out, std::vector > &results); + // Remove the bounding boxes with low confidence using non-maxima suppression + void post_process(cv::Mat& frame, std::vector& outs, std::vector > &results); + void CircleImagePro(cv::Mat src, cv::Mat dst, std::vector radius); void detect_batch(std::vector& vec_in, std::vector &vec_out, std::vector > > &vec_results); + // Remove the bounding boxes with low confidence using non-maxima suppression + void post_process_batch(std::vector& vec_frame, std::vector& outs, std::vector > > &vec_results); void analyse(cv::Mat vec_in, std::vector > & vec_results); + // Get the names of the output layers + std::vector getOutputsNames(const cv::dnn::Net& net); + // Draw the predicted bounding box + void drawPred(int classId, float conf, int left, int top, int right, int bottom, cv::Mat& frame); private: cv::dnn::Net net; + std::vector classes; }; #endif //end of _CIGARETTE_JD