Skip to content

Commit eae6dc3

Browse files
Update YOLOv5Detector.cpp
1 parent d84c0e9 commit eae6dc3

File tree

1 file changed

+41
-16
lines changed

1 file changed

+41
-16
lines changed

detector/YOLOv5/src/YOLOv5Detector.cpp

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,41 @@
44
@Date : 2022-09-23 02:52:41
55
*/
66
#include <YOLOv5Detector.h>
7+
#include <iostream>
78

8-
void YOLOv5Detector::init(std::string onnxpath) {
9+
void YOLOv5Detector::init(std::string onnxpath)
10+
{
911

1012
this->net = cv::dnn::readNetFromONNX(onnxpath);
13+
14+
std::string file="./coco_80_labels_list.txt";
15+
std::ifstream ifs(file);
16+
if (!ifs.is_open())
17+
CV_Error(cv::Error::StsError, "File " + file + " not found");
18+
std::string line;
19+
while (std::getline(ifs, line))
20+
{
21+
classes_.push_back(line);
22+
}
1123
}
1224

1325
void YOLOv5Detector::detect(cv::Mat & frame, std::vector<detect_result> &results)
1426
{
1527

16-
17-
1828
int w = frame.cols;
1929
int h = frame.rows;
2030
int _max = std::max(h, w);
2131
cv::Mat image = cv::Mat::zeros(cv::Size(_max, _max), CV_8UC3);
2232
cv::Rect roi(0, 0, w, h);
2333
frame.copyTo(image(roi));
2434

25-
float x_factor = image.cols / model_input_width_;
26-
float y_factor = image.rows / model_input_height_;
27-
//std::cout<<image.cols<<":"<<image.rows<<std::endl;
35+
36+
float x_factor = static_cast<float>(image.cols) / model_input_width_;
37+
float y_factor = static_cast<float>(image.rows) / model_input_height_;
2838

2939

40+
std::cout<<x_factor<<std::endl;
41+
std::cout<<y_factor<<std::endl;
3042

3143
cv::Mat blob = cv::dnn::blobFromImage(image, 1 / 255.0, cv::Size(model_input_width_, model_input_height_), cv::Scalar(0, 0, 0), true, false);
3244
this->net.setInput(blob);
@@ -40,18 +52,19 @@ void YOLOv5Detector::detect(cv::Mat & frame, std::vector<detect_result> &results
4052
std::vector<float> confidences;
4153
for (int i = 0; i < det_output.rows; i++)
4254
{
43-
float tmp_confidence = det_output.at<float>(i, 4);
44-
if (tmp_confidence < nms_threshold_)
55+
float box_conf = det_output.at<float>(i, 4);
56+
if (box_conf < nms_threshold_)
4557
{
4658
continue;
4759
}
60+
4861
cv::Mat classes_confidences = det_output.row(i).colRange(5, 85);
4962
cv::Point classIdPoint;
50-
double confidence;
51-
minMaxLoc(classes_confidences, 0, &confidence, 0, &classIdPoint);
63+
double cls_conf;
64+
cv::minMaxLoc(classes_confidences, 0, &cls_conf, 0, &classIdPoint);
5265

5366

54-
if (confidence > confidence_threshold_)
67+
if (cls_conf > confidence_threshold_)
5568
{
5669
float cx = det_output.at<float>(i, 0);
5770
float cy = det_output.at<float>(i, 1);
@@ -69,7 +82,7 @@ void YOLOv5Detector::detect(cv::Mat & frame, std::vector<detect_result> &results
6982

7083
boxes.push_back(box);
7184
classIds.push_back(classIdPoint.x);
72-
confidences.push_back(confidence);
85+
confidences.push_back(cls_conf * box_conf);
7386
}
7487
}
7588

@@ -83,11 +96,23 @@ void YOLOv5Detector::detect(cv::Mat & frame, std::vector<detect_result> &results
8396
dr.box = boxes[index];
8497
dr.classId = idx;
8598
dr.confidence = confidences[index];
86-
cv::rectangle(frame, boxes[index], cv::Scalar(0, 0, 255), 2, 8);
87-
88-
cv::rectangle(frame, cv::Point(boxes[index].tl().x, boxes[index].tl().y - 20),
89-
cv::Point(boxes[index].br().x, boxes[index].tl().y), cv::Scalar(255, 0, 0), -1);
9099
results.push_back(dr);
91100
}
92101

93102
}
103+
void YOLOv5Detector::draw_frame(cv::Mat & frame, std::vector<detect_result> &results)
104+
{
105+
106+
for(auto dr : results)
107+
{
108+
109+
cv::rectangle(frame, dr.box , cv::Scalar(0, 0, 255), 2, 8);
110+
cv::rectangle(frame, cv::Point(dr.box .tl().x, dr.box .tl().y - 20), cv::Point(dr.box .br().x, dr.box .tl().y), cv::Scalar(255, 0, 0), -1);
111+
112+
std::string label = cv::format("%.2f", dr.confidence);
113+
label = classes_[dr.classId ] + ":" + label;
114+
115+
cv::putText(frame, label, cv::Point(dr.box.x, dr.box.y + 6), 1, 2, cv::Scalar(0, 255, 0),2);
116+
117+
}
118+
}

0 commit comments

Comments
 (0)