Skip to content

Commit 02f106d

Browse files
authored
CPP: codes view and some minimum fix (compiler warning...) (#14635)
* CPP: minimum fix (compiler warning...) * Codes Review
1 parent 2c0c4be commit 02f106d

12 files changed

+124
-112
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/clipper.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2812,7 +2812,7 @@ bool Clipper::FixupIntersectionOrder() noexcept {
28122812
if (!EdgesAdjacent(*m_IntersectList[i])) {
28132813
size_t j = i + 1;
28142814
while (j < cnt && !EdgesAdjacent(*m_IntersectList[j]))
2815-
j++;
2815+
++j;
28162816
if (j == cnt)
28172817
return false;
28182818
std::swap(m_IntersectList[i], m_IntersectList[j]);
@@ -3078,7 +3078,7 @@ void Clipper::BuildResult2(PolyTree &polytree) noexcept {
30783078
pn->Index = 0;
30793079
pn->Contour.reserve(cnt);
30803080
OutPt *op = outRec->Pts->Prev;
3081-
for (int j = 0; j < cnt; j++) {
3081+
for (int j = 0; j < cnt; ++j) {
30823082
pn->Contour.emplace_back(op->Pt);
30833083
op = op->Prev;
30843084
}
@@ -3643,7 +3643,7 @@ void ClipperOffset::AddPath(const Path &path, JoinType joinType,
36433643
int j = 0, k = 0;
36443644
for (int i = 1; i <= highI; ++i)
36453645
if (newNode->Contour[j] != path[i]) {
3646-
j++;
3646+
++j;
36473647
newNode->Contour.emplace_back(path[i]);
36483648
if (path[i].Y > newNode->Contour[k].Y ||
36493649
(path[i].Y == newNode->Contour[k].Y &&
@@ -3826,7 +3826,7 @@ void ClipperOffset::DoOffset(double delta) noexcept {
38263826
if (len == 1) {
38273827
if (node.m_jointype == jtRound) {
38283828
double X = 1.0, Y = 0.0;
3829-
for (cInt j = 1; j <= steps; j++) {
3829+
for (cInt j = 1; j <= steps; ++j) {
38303830
m_destPoly.emplace_back(Round(m_srcPoly[0].X + X * delta),
38313831
Round(m_srcPoly[0].Y + Y * delta));
38323832
double X2 = X;

deploy/cpp_infer/src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ void structure(std::vector<cv::String> &cv_all_img_names) {
137137
std::vector<StructurePredictResult> structure_results = engine.structure(
138138
img, FLAGS_layout, FLAGS_table, FLAGS_det && FLAGS_rec);
139139

140-
for (int j = 0; j < structure_results.size(); j++) {
140+
for (size_t j = 0; j < structure_results.size(); ++j) {
141141
std::cout << j << "\ttype: " << structure_results[j].type
142142
<< ", region: [";
143143
std::cout << structure_results[j].box[0] << ","

deploy/cpp_infer/src/ocr_cls.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void Classifier::Run(const std::vector<cv::Mat> &img_list,
4040
int batch_num = end_img_no - beg_img_no;
4141
// preprocess
4242
std::vector<cv::Mat> norm_img_batch;
43-
for (int ino = beg_img_no; ino < end_img_no; ino++) {
43+
for (int ino = beg_img_no; ino < end_img_no; ++ino) {
4444
cv::Mat srcimg;
4545
img_list[ino].copyTo(srcimg);
4646
cv::Mat resize_img;
@@ -87,7 +87,7 @@ void Classifier::Run(const std::vector<cv::Mat> &img_list,
8787

8888
// postprocess
8989
auto postprocess_start = std::chrono::steady_clock::now();
90-
for (int batch_idx = 0; batch_idx < predict_shape[0]; batch_idx++) {
90+
for (int batch_idx = 0; batch_idx < predict_shape[0]; ++batch_idx) {
9191
int label = int(
9292
Utility::argmax(&predict_batch[batch_idx * predict_shape[1]],
9393
&predict_batch[(batch_idx + 1) * predict_shape[1]]));

deploy/cpp_infer/src/ocr_rec.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,22 @@ void CRNNRecognizer::Run(const std::vector<cv::Mat> &img_list,
3232
std::chrono::duration<float> postprocess_diff =
3333
std::chrono::duration<float>::zero();
3434

35-
int img_num = img_list.size();
35+
size_t img_num = img_list.size();
3636
std::vector<float> width_list;
37-
for (int i = 0; i < img_num; ++i) {
37+
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

42-
for (int beg_img_no = 0; beg_img_no < img_num;
42+
for (size_t beg_img_no = 0; beg_img_no < img_num;
4343
beg_img_no += this->rec_batch_num_) {
4444
auto preprocess_start = std::chrono::steady_clock::now();
45-
int end_img_no = std::min(img_num, beg_img_no + this->rec_batch_num_);
45+
size_t end_img_no = std::min(img_num, beg_img_no + this->rec_batch_num_);
4646
int batch_num = end_img_no - beg_img_no;
4747
int imgH = this->rec_image_shape_[1];
4848
int imgW = this->rec_image_shape_[2];
4949
float max_wh_ratio = imgW * 1.0 / imgH;
50-
for (int ino = beg_img_no; ino < end_img_no; ino++) {
50+
for (size_t ino = beg_img_no; ino < end_img_no; ++ino) {
5151
int h = img_list[indices[ino]].rows;
5252
int w = img_list[indices[ino]].cols;
5353
float wh_ratio = w * 1.0 / h;
@@ -56,7 +56,7 @@ void CRNNRecognizer::Run(const std::vector<cv::Mat> &img_list,
5656

5757
int batch_width = imgW;
5858
std::vector<cv::Mat> norm_img_batch;
59-
for (int ino = beg_img_no; ino < end_img_no; ino++) {
59+
for (size_t ino = beg_img_no; ino < end_img_no; ++ino) {
6060
cv::Mat srcimg;
6161
img_list[indices[ino]].copyTo(srcimg);
6262
cv::Mat resize_img;
@@ -85,24 +85,24 @@ void CRNNRecognizer::Run(const std::vector<cv::Mat> &img_list,
8585
auto output_t = this->predictor_->GetOutputHandle(output_names[0]);
8686
auto predict_shape = output_t->shape();
8787

88-
int out_num = std::accumulate(predict_shape.begin(), predict_shape.end(), 1,
89-
std::multiplies<int>());
88+
size_t out_num = std::accumulate(predict_shape.begin(), predict_shape.end(),
89+
1, std::multiplies<int>());
9090
predict_batch.resize(out_num);
9191
// predict_batch is the result of Last FC with softmax
9292
output_t->CopyToCpu(predict_batch.data());
9393
auto inference_end = std::chrono::steady_clock::now();
9494
inference_diff += inference_end - inference_start;
9595
// ctc decode
9696
auto postprocess_start = std::chrono::steady_clock::now();
97-
for (int m = 0; m < predict_shape[0]; m++) {
97+
for (int m = 0; m < predict_shape[0]; ++m) {
9898
std::string str_res;
9999
int argmax_idx;
100100
int last_index = 0;
101101
float score = 0.f;
102102
int count = 0;
103103
float max_value = 0.0f;
104104

105-
for (int n = 0; n < predict_shape[1]; n++) {
105+
for (int n = 0; n < predict_shape[1]; ++n) {
106106
// get idx
107107
argmax_idx = int(Utility::argmax(
108108
&predict_batch[(m * predict_shape[1] + n) * predict_shape[2]],

deploy/cpp_infer/src/paddleocr.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ PPOCR::ocr(const std::vector<cv::Mat> &img_list, bool det, bool rec,
6565
ocr_result.resize(img_list.size());
6666
if (cls && this->pri_->classifier_) {
6767
this->cls(img_list, ocr_result);
68-
for (int i = 0; i < img_list.size(); ++i) {
68+
for (size_t i = 0; i < img_list.size(); ++i) {
6969
if (ocr_result[i].cls_label % 2 == 1 &&
7070
ocr_result[i].cls_score > this->pri_->classifier_->cls_thresh) {
7171
cv::rotate(img_list[i], img_list[i], 1);
@@ -75,11 +75,11 @@ 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 {
82-
for (int i = 0; i < img_list.size(); ++i) {
82+
for (size_t i = 0; i < img_list.size(); ++i) {
8383
std::vector<OCRPredictResult> ocr_result =
8484
this->ocr(img_list[i], true, rec, cls);
8585
ocr_results.emplace_back(std::move(ocr_result));
@@ -96,14 +96,14 @@ std::vector<OCRPredictResult> PPOCR::ocr(const cv::Mat &img, bool det, bool rec,
9696
this->det(img, ocr_result);
9797
// crop image
9898
std::vector<cv::Mat> img_list;
99-
for (int j = 0; j < ocr_result.size(); j++) {
99+
for (size_t j = 0; j < ocr_result.size(); ++j) {
100100
cv::Mat crop_img = Utility::GetRotateCropImage(img, ocr_result[j].box);
101101
img_list.emplace_back(std::move(crop_img));
102102
}
103103
// cls
104104
if (cls && this->pri_->classifier_) {
105105
this->cls(img_list, ocr_result);
106-
for (int i = 0; i < img_list.size(); ++i) {
106+
for (size_t i = 0; i < img_list.size(); ++i) {
107107
if (ocr_result[i].cls_label % 2 == 1 &&
108108
ocr_result[i].cls_score > this->pri_->classifier_->cls_thresh) {
109109
cv::rotate(img_list[i], img_list[i], 1);
@@ -124,13 +124,13 @@ void PPOCR::det(const cv::Mat &img,
124124

125125
this->pri_->detector_->Run(img, boxes, det_times);
126126

127-
for (int i = 0; i < boxes.size(); ++i) {
127+
for (size_t i = 0; i < boxes.size(); ++i) {
128128
OCRPredictResult res;
129129
res.box = std::move(boxes[i]);
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];
@@ -143,7 +143,7 @@ void PPOCR::rec(const std::vector<cv::Mat> &img_list,
143143
std::vector<double> rec_times;
144144
this->pri_->recognizer_->Run(img_list, rec_texts, rec_text_scores, rec_times);
145145
// output rec results
146-
for (int i = 0; i < rec_texts.size(); ++i) {
146+
for (size_t i = 0; i < rec_texts.size(); ++i) {
147147
ocr_results[i].text = std::move(rec_texts[i]);
148148
ocr_results[i].score = rec_text_scores[i];
149149
}
@@ -159,7 +159,7 @@ void PPOCR::cls(const std::vector<cv::Mat> &img_list,
159159
std::vector<double> cls_times;
160160
this->pri_->classifier_->Run(img_list, cls_labels, cls_scores, cls_times);
161161
// output cls results
162-
for (int i = 0; i < cls_labels.size(); ++i) {
162+
for (size_t i = 0; i < cls_labels.size(); ++i) {
163163
ocr_results[i].cls_label = cls_labels[i];
164164
ocr_results[i].cls_score = cls_scores[i];
165165
}

deploy/cpp_infer/src/paddlestructure.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ PaddleStructure::structure(const cv::Mat &srcimg, bool layout, bool table,
6464
structure_results.emplace_back(std::move(res));
6565
}
6666
cv::Mat roi_img;
67-
for (int i = 0; i < structure_results.size(); ++i) {
67+
for (size_t i = 0; i < structure_results.size(); ++i) {
6868
// crop image
6969
roi_img = std::move(Utility::crop_image(img, structure_results[i].box));
7070
if (structure_results[i].type == "table" && table) {
@@ -108,13 +108,13 @@ void PaddleStructure::table(const cv::Mat &img,
108108
std::vector<OCRPredictResult> ocr_result;
109109
int expand_pixel = 3;
110110

111-
for (int i = 0; i < img_list.size(); ++i) {
111+
for (size_t i = 0; i < img_list.size(); ++i) {
112112
// det
113113
this->det(img_list[i], ocr_result);
114114
// crop image
115115
std::vector<cv::Mat> rec_img_list;
116116
std::vector<int> ocr_box;
117-
for (int j = 0; j < ocr_result.size(); j++) {
117+
for (size_t j = 0; j < ocr_result.size(); ++j) {
118118
ocr_box = std::move(Utility::xyxyxyxy2xyxy(ocr_result[j].box));
119119
ocr_box[0] = std::max(0, ocr_box[0] - expand_pixel);
120120
ocr_box[1] = std::max(0, ocr_box[1] - expand_pixel),
@@ -144,16 +144,16 @@ std::string PaddleStructure::rebuild_table(
144144

145145
std::vector<int> ocr_box;
146146
std::vector<int> structure_box;
147-
for (int i = 0; i < ocr_result.size(); ++i) {
147+
for (size_t i = 0; i < ocr_result.size(); ++i) {
148148
ocr_box = std::move(Utility::xyxyxyxy2xyxy(ocr_result[i].box));
149149
ocr_box[0] -= 1;
150150
ocr_box[1] -= 1;
151151
ocr_box[2] += 1;
152152
ocr_box[3] += 1;
153153
std::vector<std::vector<float>> dis_list(structure_boxes.size(),
154154
std::vector<float>(3, 100000.0));
155-
for (int j = 0; j < structure_boxes.size(); j++) {
156-
if (structure_boxes[i].size() == 8) {
155+
for (size_t j = 0; j < structure_boxes.size(); ++j) {
156+
if (structure_boxes[j].size() == 8) {
157157
structure_box = std::move(Utility::xyxyxyxy2xyxy(structure_boxes[j]));
158158
} else {
159159
structure_box = structure_boxes[j];
@@ -171,7 +171,7 @@ std::string PaddleStructure::rebuild_table(
171171
// get pred html
172172
std::string html_str = "";
173173
int td_tag_idx = 0;
174-
for (int i = 0; i < structure_html_tags.size(); ++i) {
174+
for (size_t i = 0; i < structure_html_tags.size(); ++i) {
175175
if (structure_html_tags[i].find("</td>") != std::string::npos) {
176176
if (structure_html_tags[i].find("<td></td>") != std::string::npos) {
177177
html_str += "<td>";
@@ -183,7 +183,7 @@ std::string PaddleStructure::rebuild_table(
183183
b_with = true;
184184
html_str += "<b>";
185185
}
186-
for (int j = 0; j < matched[td_tag_idx].size(); j++) {
186+
for (size_t j = 0; j < matched[td_tag_idx].size(); ++j) {
187187
std::string content = matched[td_tag_idx][j];
188188
if (matched[td_tag_idx].size() > 1) {
189189
// remove blank, <b> and </b>

0 commit comments

Comments
 (0)