4
4
@Date : 2022-09-23 02:52:41
5
5
*/
6
6
#include < YOLOv5Detector.h>
7
+ #include < iostream>
7
8
8
- void YOLOv5Detector::init (std::string onnxpath) {
9
+ void YOLOv5Detector::init (std::string onnxpath)
10
+ {
9
11
10
12
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
+ }
11
23
}
12
24
13
25
void YOLOv5Detector::detect (cv::Mat & frame, std::vector<detect_result> &results)
14
26
{
15
27
16
-
17
-
18
28
int w = frame.cols ;
19
29
int h = frame.rows ;
20
30
int _max = std::max (h, w);
21
31
cv::Mat image = cv::Mat::zeros (cv::Size (_max, _max), CV_8UC3);
22
32
cv::Rect roi (0 , 0 , w, h);
23
33
frame.copyTo (image (roi));
24
34
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_ ;
28
38
29
39
40
+ std::cout<<x_factor<<std::endl;
41
+ std::cout<<y_factor<<std::endl;
30
42
31
43
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 );
32
44
this ->net .setInput (blob);
@@ -40,18 +52,19 @@ void YOLOv5Detector::detect(cv::Mat & frame, std::vector<detect_result> &results
40
52
std::vector<float > confidences;
41
53
for (int i = 0 ; i < det_output.rows ; i++)
42
54
{
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_)
45
57
{
46
58
continue ;
47
59
}
60
+
48
61
cv::Mat classes_confidences = det_output.row (i).colRange (5 , 85 );
49
62
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);
52
65
53
66
54
- if (confidence > confidence_threshold_)
67
+ if (cls_conf > confidence_threshold_)
55
68
{
56
69
float cx = det_output.at <float >(i, 0 );
57
70
float cy = det_output.at <float >(i, 1 );
@@ -69,7 +82,7 @@ void YOLOv5Detector::detect(cv::Mat & frame, std::vector<detect_result> &results
69
82
70
83
boxes.push_back (box);
71
84
classIds.push_back (classIdPoint.x );
72
- confidences.push_back (confidence );
85
+ confidences.push_back (cls_conf * box_conf );
73
86
}
74
87
}
75
88
@@ -83,11 +96,23 @@ void YOLOv5Detector::detect(cv::Mat & frame, std::vector<detect_result> &results
83
96
dr.box = boxes[index];
84
97
dr.classId = idx;
85
98
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 );
90
99
results.push_back (dr);
91
100
}
92
101
93
102
}
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