解决标框重叠无法识别的问题

main
Jeffrey_Li 1 year ago
parent b9d04918b9
commit 1ba73b82f2

@ -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();

@ -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<cv::Mat>& outs, std::vector<std::pair<int, cv::Rect> > &results);
static void post_process_batch(std::vector<cv::Mat>& vec_frame, std::vector<cv::Mat>& outs, std::vector<std::vector<std::pair<int, cv::Rect> > > &vec_results);
// Get the names of the output layers
static std::vector<cv::String> 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<std::pair<int, cv::Rect> >& vec_
}
// Get the names of the output layers
static std::vector<cv::String> getOutputsNames(const cv::dnn::Net& net)
std::vector<cv::String> AlgJd::getOutputsNames(const cv::dnn::Net& net)
{
static std::vector<cv::String> names;
std::vector<cv::String> 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<cv::String> getOutputsNames(const cv::dnn::Net& net)
return names;
}
static void post_process(cv::Mat& frame, std::vector<cv::Mat>& outs, std::vector<std::pair<int, cv::Rect> > &results)
void AlgJd::post_process(cv::Mat& frame, std::vector<cv::Mat>& outs, std::vector<std::pair<int, cv::Rect> > &results)
{
std::vector<int> classIds;
std::vector<float> confidences;
std::vector<cv::Rect> boxes;
std::vector < std::vector<int>> classIds(classes.size());
std::vector < std::vector<float>> confidences(classes.size());
std::vector < std::vector<cv::Rect>> 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<cv::Mat>& 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<cv::Mat>& outs, std::vector
// Perform non maximum suppression to eliminate redundant overlapping boxes with
// lower confidences
std::vector<int> 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<int> 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<cv::Mat>& vec_in, std::vector<cv::Mat> &vec
}
}
static void post_process_batch(std::vector<cv::Mat>& vec_frame, std::vector<cv::Mat>& outs, std::vector<std::vector<std::pair<int, cv::Rect> > > &vec_results)
void AlgJd::post_process_batch(std::vector<cv::Mat>& vec_frame, std::vector<cv::Mat>& outs, std::vector<std::vector<std::pair<int, cv::Rect> > > &vec_results)
{
int batch = vec_frame.size();
double confidence;///
for (int k = 0; k < batch; k++)
{
std::vector<int> classIds;
std::vector<float> confidences;
std::vector<cv::Rect> boxes;
//std::cout << "outs.size()\t" << outs.size() << std::endl;
//std::cout << "Type\t" << outs[0].type() << std::endl;
std::vector < std::vector<int>> classIds(classes.size());
std::vector < std::vector<float>> confidences(classes.size());
std::vector < std::vector<cv::Rect>> 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<cv::Mat>& vec_frame, std::vector<cv::
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));
}
}
}
// Perform non maximum suppression to eliminate redundant overlapping boxes with
// lower confidences
std::vector<int> indices;
cv::dnn::NMSBoxes(boxes, confidences, confThreshold, nmsThreshold, indices);
std::vector<std::pair<int, cv::Rect> > 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<int> 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);
}

@ -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<std::pair<int, cv::Rect> > &results);
int detect(cv::Mat& in, cv::Mat &out, std::vector<std::pair<int, cv::Rect> > &results);
// Remove the bounding boxes with low confidence using non-maxima suppression
void post_process(cv::Mat& frame, std::vector<cv::Mat>& outs, std::vector<std::pair<int, cv::Rect> > &results);
void CircleImagePro(cv::Mat src, cv::Mat dst, std::vector<float> radius);
void detect_batch(std::vector<cv::Mat>& vec_in, std::vector<cv::Mat> &vec_out, std::vector<std::vector<std::pair<int, cv::Rect> > > &vec_results);
// Remove the bounding boxes with low confidence using non-maxima suppression
void post_process_batch(std::vector<cv::Mat>& vec_frame, std::vector<cv::Mat>& outs, std::vector<std::vector<std::pair<int, cv::Rect> > > &vec_results);
void analyse(cv::Mat vec_in, std::vector<std::pair<int, cv::Rect> > & vec_results);
// Get the names of the output layers
std::vector<cv::String> 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<std::string> classes;
};
#endif //end of _CIGARETTE_JD

Loading…
Cancel
Save