Skip to content

Commit 142f6c9

Browse files
committed
Codes Review
1 parent 3304a83 commit 142f6c9

File tree

4 files changed

+42
-32
lines changed

4 files changed

+42
-32
lines changed

deploy/cpp_infer/include/utility.h

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,33 +61,41 @@ class Utility {
6161
GetRotateCropImage(const cv::Mat &srcimage,
6262
const std::vector<std::vector<int>> &box) noexcept;
6363

64-
static std::vector<int> argsort(const std::vector<float> &array) noexcept;
64+
static std::vector<size_t> argsort(const std::vector<float> &array) noexcept;
6565

6666
static std::string basename(const std::string &filename) noexcept;
6767

68-
static bool PathExists(const std::string &path) noexcept;
68+
static bool PathExists(const char *path) noexcept;
69+
static inline bool PathExists(const std::string &path) noexcept {
70+
return PathExists(path.c_str());
71+
}
6972

70-
static void CreateDir(const std::string &path) noexcept;
73+
static void CreateDir(const char *path) noexcept;
74+
static inline void CreateDir(const std::string &path) noexcept {
75+
CreateDir(path.c_str());
76+
}
7177

7278
static void
7379
print_result(const std::vector<OCRPredictResult> &ocr_result) noexcept;
7480

75-
static cv::Mat crop_image(cv::Mat &img,
81+
static cv::Mat crop_image(const cv::Mat &img,
7682
const std::vector<int> &area) noexcept;
77-
static cv::Mat crop_image(cv::Mat &img,
83+
static cv::Mat crop_image(const cv::Mat &img,
7884
const std::vector<float> &area) noexcept;
7985

80-
static void sorted_boxes(std::vector<OCRPredictResult> &ocr_result) noexcept;
86+
static void sort_boxes(std::vector<OCRPredictResult> &ocr_result) noexcept;
8187

8288
static std::vector<int>
8389
xyxyxyxy2xyxy(const std::vector<std::vector<int>> &box) noexcept;
8490
static std::vector<int> xyxyxyxy2xyxy(const std::vector<int> &box) noexcept;
8591

8692
static float fast_exp(float x) noexcept;
8793
static std::vector<float>
88-
activation_function_softmax(std::vector<float> &src) noexcept;
89-
static float iou(std::vector<int> &box1, std::vector<int> &box2) noexcept;
90-
static float iou(std::vector<float> &box1, std::vector<float> &box2) noexcept;
94+
activation_function_softmax(const std::vector<float> &src) noexcept;
95+
static float iou(const std::vector<int> &box1,
96+
const std::vector<int> &box2) noexcept;
97+
static float iou(const std::vector<float> &box1,
98+
const std::vector<float> &box2) noexcept;
9199

92100
private:
93101
static bool comparison_box(const OCRPredictResult &result1,

deploy/cpp_infer/src/ocr_rec.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void CRNNRecognizer::Run(const std::vector<cv::Mat> &img_list,
3737
for (size_t i = 0; i < img_num; ++i) {
3838
width_list.emplace_back(float(img_list[i].cols) / img_list[i].rows);
3939
}
40-
std::vector<int> indices = std::move(Utility::argsort(width_list));
40+
std::vector<size_t> indices = std::move(Utility::argsort(width_list));
4141

4242
for (size_t beg_img_no = 0; beg_img_no < img_num;
4343
beg_img_no += this->rec_batch_num_) {

deploy/cpp_infer/src/paddleocr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ PPOCR::ocr(const std::vector<cv::Mat> &img_list, bool det, bool rec,
7575
if (rec) {
7676
this->rec(img_list, ocr_result);
7777
}
78-
for (int i = 0; i < ocr_result.size(); ++i) {
78+
for (size_t i = 0; i < ocr_result.size(); ++i) {
7979
ocr_results.emplace_back(1, std::move(ocr_result[i]));
8080
}
8181
} else {
@@ -130,7 +130,7 @@ void PPOCR::det(const cv::Mat &img,
130130
ocr_results.emplace_back(std::move(res));
131131
}
132132
// sort boex from top to bottom, from left to right
133-
Utility::sorted_boxes(ocr_results);
133+
Utility::sort_boxes(ocr_results);
134134
this->time_info_det[0] += det_times[0];
135135
this->time_info_det[1] += det_times[1];
136136
this->time_info_det[2] += det_times[2];

deploy/cpp_infer/src/utility.cpp

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,15 @@ Utility::GetRotateCropImage(const cv::Mat &srcimage,
190190
}
191191
}
192192

193-
std::vector<int> Utility::argsort(const std::vector<float> &array) noexcept {
194-
std::vector<int> array_index(array.size(), 0);
195-
for (int i = 0; i < array.size(); ++i)
193+
std::vector<size_t> Utility::argsort(const std::vector<float> &array) noexcept {
194+
std::vector<size_t> array_index(array.size(), 0);
195+
for (size_t i = 0; i < array.size(); ++i)
196196
array_index[i] = i;
197197

198-
std::sort(
199-
array_index.begin(), array_index.end(),
200-
[&array](int pos1, int pos2) { return (array[pos1] < array[pos2]); });
198+
std::sort(array_index.begin(), array_index.end(),
199+
[&array](size_t pos1, size_t pos2) {
200+
return (array[pos1] < array[pos2]);
201+
});
201202

202203
return array_index;
203204
}
@@ -237,23 +238,23 @@ std::string Utility::basename(const std::string &filename) noexcept {
237238
return filename.substr(index + 1, len - index);
238239
}
239240

240-
bool Utility::PathExists(const std::string &path) noexcept {
241+
bool Utility::PathExists(const char *path) noexcept {
241242
#ifdef _WIN32
242243
struct _stat buffer;
243-
return (_stat(path.c_str(), &buffer) == 0);
244+
return (_stat(path, &buffer) == 0);
244245
#else
245246
struct stat buffer;
246-
return (stat(path.c_str(), &buffer) == 0);
247+
return (stat(path, &buffer) == 0);
247248
#endif // !_WIN32
248249
}
249250

250-
void Utility::CreateDir(const std::string &path) noexcept {
251+
void Utility::CreateDir(const char *path) noexcept {
251252
#ifdef _MSC_VER
252-
_mkdir(path.c_str());
253+
_mkdir(path);
253254
#elif defined __MINGW32__
254-
mkdir(path.c_str());
255+
mkdir(path);
255256
#else
256-
mkdir(path.c_str(), 0777);
257+
mkdir(path, 0777);
257258
#endif // !_WIN32
258259
}
259260

@@ -288,7 +289,7 @@ void Utility::print_result(
288289
}
289290
}
290291

291-
cv::Mat Utility::crop_image(cv::Mat &img,
292+
cv::Mat Utility::crop_image(const cv::Mat &img,
292293
const std::vector<int> &box) noexcept {
293294
cv::Mat crop_im = cv::Mat::zeros(box[3] - box[1], box[2] - box[0], 16);
294295
int crop_x1 = std::max(0, box[0]);
@@ -305,14 +306,14 @@ cv::Mat Utility::crop_image(cv::Mat &img,
305306
return crop_im;
306307
}
307308

308-
cv::Mat Utility::crop_image(cv::Mat &img,
309+
cv::Mat Utility::crop_image(const cv::Mat &img,
309310
const std::vector<float> &box) noexcept {
310311
std::vector<int> box_int = {(int)box[0], (int)box[1], (int)box[2],
311312
(int)box[3]};
312313
return crop_image(img, box_int);
313314
}
314315

315-
void Utility::sorted_boxes(std::vector<OCRPredictResult> &ocr_result) noexcept {
316+
void Utility::sort_boxes(std::vector<OCRPredictResult> &ocr_result) noexcept {
316317
std::sort(ocr_result.begin(), ocr_result.end(), Utility::comparison_box);
317318
if (ocr_result.size() > 1) {
318319
for (size_t i = 0; i < ocr_result.size() - 1; ++i) {
@@ -367,7 +368,7 @@ float Utility::fast_exp(float x) noexcept {
367368
}
368369

369370
std::vector<float>
370-
Utility::activation_function_softmax(std::vector<float> &src) noexcept {
371+
Utility::activation_function_softmax(const std::vector<float> &src) noexcept {
371372
size_t length = src.size();
372373
std::vector<float> dst;
373374
dst.resize(length);
@@ -385,7 +386,8 @@ Utility::activation_function_softmax(std::vector<float> &src) noexcept {
385386
return dst;
386387
}
387388

388-
float Utility::iou(std::vector<int> &box1, std::vector<int> &box2) noexcept {
389+
float Utility::iou(const std::vector<int> &box1,
390+
const std::vector<int> &box2) noexcept {
389391
int area1 = std::max(0, box1[2] - box1[0]) * std::max(0, box1[3] - box1[1]);
390392
int area2 = std::max(0, box2[2] - box2[0]) * std::max(0, box2[3] - box2[1]);
391393

@@ -407,8 +409,8 @@ float Utility::iou(std::vector<int> &box1, std::vector<int> &box2) noexcept {
407409
}
408410
}
409411

410-
float Utility::iou(std::vector<float> &box1,
411-
std::vector<float> &box2) noexcept {
412+
float Utility::iou(const std::vector<float> &box1,
413+
const std::vector<float> &box2) noexcept {
412414
float area1 = std::max((float)0.0, box1[2] - box1[0]) *
413415
std::max((float)0.0, box1[3] - box1[1]);
414416
float area2 = std::max((float)0.0, box2[2] - box2[0]) *

0 commit comments

Comments
 (0)