@@ -8,9 +8,7 @@ namespace yolox_cpp
88/* *
99 * @brief Define names based depends on Unicode path support
1010 */
11- #define tcout std::cout
1211#define file_name_t std::string
13- #define imread_t cv::imread
1412
1513 struct Object
1614 {
@@ -24,17 +22,21 @@ namespace yolox_cpp
2422 int grid0;
2523 int grid1;
2624 int stride;
25+ GridAndStride (const int grid0_, const int grid1_, const int stride_)
26+ : grid0(grid0_), grid1(grid1_), stride(stride_)
27+ {
28+ }
2729 };
2830
2931 class AbcYoloX
3032 {
3133 public:
3234 AbcYoloX () {}
33- AbcYoloX (float nms_th = 0.45 , float conf_th = 0.3 ,
34- std::string model_version = " 0.1.1rc0" ,
35- int num_classes = 80 , bool p6 = false )
35+ AbcYoloX (const float nms_th = 0.45 , const float conf_th = 0.3 ,
36+ const std::string & model_version = " 0.1.1rc0" ,
37+ const int num_classes = 80 , const bool p6 = false )
3638 : nms_thresh_(nms_th), bbox_conf_thresh_(conf_th),
37- model_version_ (model_version), num_classes_(num_classes), p6_(p6)
39+ num_classes_ (num_classes), p6_(p6), model_version_(model_version )
3840 {
3941 }
4042 virtual std::vector<Object> inference (const cv::Mat &frame) = 0;
@@ -55,10 +57,10 @@ namespace yolox_cpp
5557
5658 cv::Mat static_resize (const cv::Mat &img)
5759 {
58- float r = std::min (input_w_ / (img.cols * 1.0 ), input_h_ / (img.rows * 1.0 ));
60+ const float r = std::min (input_w_ / (img.cols * 1.0 ), input_h_ / (img.rows * 1.0 ));
5961 // r = std::min(r, 1.0f);
60- int unpad_w = r * img.cols ;
61- int unpad_h = r * img.rows ;
62+ const int unpad_w = r * img.cols ;
63+ const int unpad_h = r * img.rows ;
6264 cv::Mat re (unpad_h, unpad_w, CV_8UC3);
6365 cv::resize (img, re, re.size ());
6466 cv::Mat out (input_h_, input_w_, CV_8UC3, cv::Scalar (114 , 114 , 114 ));
@@ -69,9 +71,9 @@ namespace yolox_cpp
6971 // for NCHW
7072 void blobFromImage (const cv::Mat &img, float *blob_data)
7173 {
72- size_t channels = 3 ;
73- size_t img_h = img.rows ;
74- size_t img_w = img.cols ;
74+ const size_t channels = 3 ;
75+ const size_t img_h = img.rows ;
76+ const size_t img_w = img.cols ;
7577 if (this ->model_version_ == " 0.1.0" )
7678 {
7779 for (size_t c = 0 ; c < channels; ++c)
@@ -104,9 +106,9 @@ namespace yolox_cpp
104106 // for NHWC
105107 void blobFromImage_nhwc (const cv::Mat &img, float *blob_data)
106108 {
107- size_t channels = 3 ;
108- size_t img_h = img.rows ;
109- size_t img_w = img.cols ;
109+ const size_t channels = 3 ;
110+ const size_t img_h = img.rows ;
111+ const size_t img_w = img.cols ;
110112 if (this ->model_version_ == " 0.1.0" )
111113 {
112114 for (size_t i = 0 ; i < img_h * img_w; ++i)
@@ -134,19 +136,19 @@ namespace yolox_cpp
134136 {
135137 for (auto stride : strides)
136138 {
137- int num_grid_w = target_w / stride;
138- int num_grid_h = target_h / stride;
139+ const int num_grid_w = target_w / stride;
140+ const int num_grid_h = target_h / stride;
139141 for (int g1 = 0 ; g1 < num_grid_h; ++g1)
140142 {
141143 for (int g0 = 0 ; g0 < num_grid_w; ++g0)
142144 {
143- grid_strides.push_back ((GridAndStride){ g0, g1, stride} );
145+ grid_strides.emplace_back ( g0, g1, stride);
144146 }
145147 }
146148 }
147149 }
148150
149- void generate_yolox_proposals (const std::vector<GridAndStride> grid_strides, const float *feat_ptr, const float prob_threshold, std::vector<Object> &objects)
151+ void generate_yolox_proposals (const std::vector<GridAndStride> & grid_strides, const float *feat_ptr, const float prob_threshold, std::vector<Object> &objects)
150152 {
151153 const int num_anchors = grid_strides.size ();
152154
@@ -158,13 +160,13 @@ namespace yolox_cpp
158160
159161 const int basic_pos = anchor_idx * (num_classes_ + 5 );
160162
161- float box_objectness = feat_ptr[basic_pos + 4 ];
163+ const float box_objectness = feat_ptr[basic_pos + 4 ];
162164 int class_id = 0 ;
163165 float max_class_score = 0.0 ;
164166 for (int class_idx = 0 ; class_idx < num_classes_; ++class_idx)
165167 {
166- float box_cls_score = feat_ptr[basic_pos + 5 + class_idx];
167- float box_prob = box_objectness * box_cls_score;
168+ const float box_cls_score = feat_ptr[basic_pos + 5 + class_idx];
169+ const float box_prob = box_objectness * box_cls_score;
168170 if (box_prob > max_class_score)
169171 {
170172 class_id = class_idx;
@@ -176,12 +178,12 @@ namespace yolox_cpp
176178 // yolox/models/yolo_head.py decode logic
177179 // outputs[..., :2] = (outputs[..., :2] + grids) * strides
178180 // outputs[..., 2:4] = torch.exp(outputs[..., 2:4]) * strides
179- float x_center = (feat_ptr[basic_pos + 0 ] + grid0) * stride;
180- float y_center = (feat_ptr[basic_pos + 1 ] + grid1) * stride;
181- float w = exp (feat_ptr[basic_pos + 2 ]) * stride;
182- float h = exp (feat_ptr[basic_pos + 3 ]) * stride;
183- float x0 = x_center - w * 0 .5f ;
184- float y0 = y_center - h * 0 .5f ;
181+ const float x_center = (feat_ptr[basic_pos + 0 ] + grid0) * stride;
182+ const float y_center = (feat_ptr[basic_pos + 1 ] + grid1) * stride;
183+ const float w = exp (feat_ptr[basic_pos + 2 ]) * stride;
184+ const float h = exp (feat_ptr[basic_pos + 3 ]) * stride;
185+ const float x0 = x_center - w * 0 .5f ;
186+ const float y0 = y_center - h * 0 .5f ;
185187
186188 Object obj;
187189 obj.rect .x = x0;
@@ -197,7 +199,7 @@ namespace yolox_cpp
197199
198200 float intersection_area (const Object &a, const Object &b)
199201 {
200- cv::Rect_<float > inter = a.rect & b.rect ;
202+ const cv::Rect_<float > inter = a.rect & b.rect ;
201203 return inter.area ();
202204 }
203205
@@ -260,8 +262,8 @@ namespace yolox_cpp
260262 const Object &b = faceobjects[picked[j]];
261263
262264 // intersection over union
263- float inter_area = intersection_area (a, b);
264- float union_area = areas[i] + areas[picked[j]] - inter_area;
265+ const float inter_area = intersection_area (a, b);
266+ const float union_area = areas[i] + areas[picked[j]] - inter_area;
265267 // float IoU = inter_area / union_area
266268 if (inter_area / union_area > nms_threshold)
267269 keep = 0 ;
@@ -272,7 +274,7 @@ namespace yolox_cpp
272274 }
273275 }
274276
275- void decode_outputs (const float *prob, const std::vector<GridAndStride> grid_strides,
277+ void decode_outputs (const float *prob, const std::vector<GridAndStride> & grid_strides,
276278 std::vector<Object> &objects, const float bbox_conf_thresh,
277279 const float scale, const int img_w, const int img_h)
278280 {
@@ -312,4 +314,4 @@ namespace yolox_cpp
312314 }
313315 };
314316}
315- #endif
317+ #endif
0 commit comments