Skip to content

Commit

Permalink
CPP: codes view and some minimum fix (compiler warning...) (#14635)
Browse files Browse the repository at this point in the history
* CPP: minimum fix (compiler warning...)

* Codes Review
  • Loading branch information
nonwill authored Feb 8, 2025
1 parent 2c0c4be commit 02f106d
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 112 deletions.
26 changes: 17 additions & 9 deletions deploy/cpp_infer/include/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,33 +61,41 @@ class Utility {
GetRotateCropImage(const cv::Mat &srcimage,
const std::vector<std::vector<int>> &box) noexcept;

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

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

static bool PathExists(const std::string &path) noexcept;
static bool PathExists(const char *path) noexcept;
static inline bool PathExists(const std::string &path) noexcept {
return PathExists(path.c_str());
}

static void CreateDir(const std::string &path) noexcept;
static void CreateDir(const char *path) noexcept;
static inline void CreateDir(const std::string &path) noexcept {
CreateDir(path.c_str());
}

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

static cv::Mat crop_image(cv::Mat &img,
static cv::Mat crop_image(const cv::Mat &img,
const std::vector<int> &area) noexcept;
static cv::Mat crop_image(cv::Mat &img,
static cv::Mat crop_image(const cv::Mat &img,
const std::vector<float> &area) noexcept;

static void sorted_boxes(std::vector<OCRPredictResult> &ocr_result) noexcept;
static void sort_boxes(std::vector<OCRPredictResult> &ocr_result) noexcept;

static std::vector<int>
xyxyxyxy2xyxy(const std::vector<std::vector<int>> &box) noexcept;
static std::vector<int> xyxyxyxy2xyxy(const std::vector<int> &box) noexcept;

static float fast_exp(float x) noexcept;
static std::vector<float>
activation_function_softmax(std::vector<float> &src) noexcept;
static float iou(std::vector<int> &box1, std::vector<int> &box2) noexcept;
static float iou(std::vector<float> &box1, std::vector<float> &box2) noexcept;
activation_function_softmax(const std::vector<float> &src) noexcept;
static float iou(const std::vector<int> &box1,
const std::vector<int> &box2) noexcept;
static float iou(const std::vector<float> &box1,
const std::vector<float> &box2) noexcept;

private:
static bool comparison_box(const OCRPredictResult &result1,
Expand Down
8 changes: 4 additions & 4 deletions deploy/cpp_infer/src/clipper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2812,7 +2812,7 @@ bool Clipper::FixupIntersectionOrder() noexcept {
if (!EdgesAdjacent(*m_IntersectList[i])) {
size_t j = i + 1;
while (j < cnt && !EdgesAdjacent(*m_IntersectList[j]))
j++;
++j;
if (j == cnt)
return false;
std::swap(m_IntersectList[i], m_IntersectList[j]);
Expand Down Expand Up @@ -3078,7 +3078,7 @@ void Clipper::BuildResult2(PolyTree &polytree) noexcept {
pn->Index = 0;
pn->Contour.reserve(cnt);
OutPt *op = outRec->Pts->Prev;
for (int j = 0; j < cnt; j++) {
for (int j = 0; j < cnt; ++j) {
pn->Contour.emplace_back(op->Pt);
op = op->Prev;
}
Expand Down Expand Up @@ -3643,7 +3643,7 @@ void ClipperOffset::AddPath(const Path &path, JoinType joinType,
int j = 0, k = 0;
for (int i = 1; i <= highI; ++i)
if (newNode->Contour[j] != path[i]) {
j++;
++j;
newNode->Contour.emplace_back(path[i]);
if (path[i].Y > newNode->Contour[k].Y ||
(path[i].Y == newNode->Contour[k].Y &&
Expand Down Expand Up @@ -3826,7 +3826,7 @@ void ClipperOffset::DoOffset(double delta) noexcept {
if (len == 1) {
if (node.m_jointype == jtRound) {
double X = 1.0, Y = 0.0;
for (cInt j = 1; j <= steps; j++) {
for (cInt j = 1; j <= steps; ++j) {
m_destPoly.emplace_back(Round(m_srcPoly[0].X + X * delta),
Round(m_srcPoly[0].Y + Y * delta));
double X2 = X;
Expand Down
2 changes: 1 addition & 1 deletion deploy/cpp_infer/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void structure(std::vector<cv::String> &cv_all_img_names) {
std::vector<StructurePredictResult> structure_results = engine.structure(
img, FLAGS_layout, FLAGS_table, FLAGS_det && FLAGS_rec);

for (int j = 0; j < structure_results.size(); j++) {
for (size_t j = 0; j < structure_results.size(); ++j) {
std::cout << j << "\ttype: " << structure_results[j].type
<< ", region: [";
std::cout << structure_results[j].box[0] << ","
Expand Down
4 changes: 2 additions & 2 deletions deploy/cpp_infer/src/ocr_cls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void Classifier::Run(const std::vector<cv::Mat> &img_list,
int batch_num = end_img_no - beg_img_no;
// preprocess
std::vector<cv::Mat> norm_img_batch;
for (int ino = beg_img_no; ino < end_img_no; ino++) {
for (int ino = beg_img_no; ino < end_img_no; ++ino) {
cv::Mat srcimg;
img_list[ino].copyTo(srcimg);
cv::Mat resize_img;
Expand Down Expand Up @@ -87,7 +87,7 @@ void Classifier::Run(const std::vector<cv::Mat> &img_list,

// postprocess
auto postprocess_start = std::chrono::steady_clock::now();
for (int batch_idx = 0; batch_idx < predict_shape[0]; batch_idx++) {
for (int batch_idx = 0; batch_idx < predict_shape[0]; ++batch_idx) {
int label = int(
Utility::argmax(&predict_batch[batch_idx * predict_shape[1]],
&predict_batch[(batch_idx + 1) * predict_shape[1]]));
Expand Down
22 changes: 11 additions & 11 deletions deploy/cpp_infer/src/ocr_rec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ void CRNNRecognizer::Run(const std::vector<cv::Mat> &img_list,
std::chrono::duration<float> postprocess_diff =
std::chrono::duration<float>::zero();

int img_num = img_list.size();
size_t img_num = img_list.size();
std::vector<float> width_list;
for (int i = 0; i < img_num; ++i) {
for (size_t i = 0; i < img_num; ++i) {
width_list.emplace_back(float(img_list[i].cols) / img_list[i].rows);
}
std::vector<int> indices = std::move(Utility::argsort(width_list));
std::vector<size_t> indices = std::move(Utility::argsort(width_list));

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

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

int out_num = std::accumulate(predict_shape.begin(), predict_shape.end(), 1,
std::multiplies<int>());
size_t out_num = std::accumulate(predict_shape.begin(), predict_shape.end(),
1, std::multiplies<int>());
predict_batch.resize(out_num);
// predict_batch is the result of Last FC with softmax
output_t->CopyToCpu(predict_batch.data());
auto inference_end = std::chrono::steady_clock::now();
inference_diff += inference_end - inference_start;
// ctc decode
auto postprocess_start = std::chrono::steady_clock::now();
for (int m = 0; m < predict_shape[0]; m++) {
for (int m = 0; m < predict_shape[0]; ++m) {
std::string str_res;
int argmax_idx;
int last_index = 0;
float score = 0.f;
int count = 0;
float max_value = 0.0f;

for (int n = 0; n < predict_shape[1]; n++) {
for (int n = 0; n < predict_shape[1]; ++n) {
// get idx
argmax_idx = int(Utility::argmax(
&predict_batch[(m * predict_shape[1] + n) * predict_shape[2]],
Expand Down
18 changes: 9 additions & 9 deletions deploy/cpp_infer/src/paddleocr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ PPOCR::ocr(const std::vector<cv::Mat> &img_list, bool det, bool rec,
ocr_result.resize(img_list.size());
if (cls && this->pri_->classifier_) {
this->cls(img_list, ocr_result);
for (int i = 0; i < img_list.size(); ++i) {
for (size_t i = 0; i < img_list.size(); ++i) {
if (ocr_result[i].cls_label % 2 == 1 &&
ocr_result[i].cls_score > this->pri_->classifier_->cls_thresh) {
cv::rotate(img_list[i], img_list[i], 1);
Expand All @@ -75,11 +75,11 @@ PPOCR::ocr(const std::vector<cv::Mat> &img_list, bool det, bool rec,
if (rec) {
this->rec(img_list, ocr_result);
}
for (int i = 0; i < ocr_result.size(); ++i) {
for (size_t i = 0; i < ocr_result.size(); ++i) {
ocr_results.emplace_back(1, std::move(ocr_result[i]));
}
} else {
for (int i = 0; i < img_list.size(); ++i) {
for (size_t i = 0; i < img_list.size(); ++i) {
std::vector<OCRPredictResult> ocr_result =
this->ocr(img_list[i], true, rec, cls);
ocr_results.emplace_back(std::move(ocr_result));
Expand All @@ -96,14 +96,14 @@ std::vector<OCRPredictResult> PPOCR::ocr(const cv::Mat &img, bool det, bool rec,
this->det(img, ocr_result);
// crop image
std::vector<cv::Mat> img_list;
for (int j = 0; j < ocr_result.size(); j++) {
for (size_t j = 0; j < ocr_result.size(); ++j) {
cv::Mat crop_img = Utility::GetRotateCropImage(img, ocr_result[j].box);
img_list.emplace_back(std::move(crop_img));
}
// cls
if (cls && this->pri_->classifier_) {
this->cls(img_list, ocr_result);
for (int i = 0; i < img_list.size(); ++i) {
for (size_t i = 0; i < img_list.size(); ++i) {
if (ocr_result[i].cls_label % 2 == 1 &&
ocr_result[i].cls_score > this->pri_->classifier_->cls_thresh) {
cv::rotate(img_list[i], img_list[i], 1);
Expand All @@ -124,13 +124,13 @@ void PPOCR::det(const cv::Mat &img,

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

for (int i = 0; i < boxes.size(); ++i) {
for (size_t i = 0; i < boxes.size(); ++i) {
OCRPredictResult res;
res.box = std::move(boxes[i]);
ocr_results.emplace_back(std::move(res));
}
// sort boex from top to bottom, from left to right
Utility::sorted_boxes(ocr_results);
Utility::sort_boxes(ocr_results);
this->time_info_det[0] += det_times[0];
this->time_info_det[1] += det_times[1];
this->time_info_det[2] += det_times[2];
Expand All @@ -143,7 +143,7 @@ void PPOCR::rec(const std::vector<cv::Mat> &img_list,
std::vector<double> rec_times;
this->pri_->recognizer_->Run(img_list, rec_texts, rec_text_scores, rec_times);
// output rec results
for (int i = 0; i < rec_texts.size(); ++i) {
for (size_t i = 0; i < rec_texts.size(); ++i) {
ocr_results[i].text = std::move(rec_texts[i]);
ocr_results[i].score = rec_text_scores[i];
}
Expand All @@ -159,7 +159,7 @@ void PPOCR::cls(const std::vector<cv::Mat> &img_list,
std::vector<double> cls_times;
this->pri_->classifier_->Run(img_list, cls_labels, cls_scores, cls_times);
// output cls results
for (int i = 0; i < cls_labels.size(); ++i) {
for (size_t i = 0; i < cls_labels.size(); ++i) {
ocr_results[i].cls_label = cls_labels[i];
ocr_results[i].cls_score = cls_scores[i];
}
Expand Down
16 changes: 8 additions & 8 deletions deploy/cpp_infer/src/paddlestructure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ PaddleStructure::structure(const cv::Mat &srcimg, bool layout, bool table,
structure_results.emplace_back(std::move(res));
}
cv::Mat roi_img;
for (int i = 0; i < structure_results.size(); ++i) {
for (size_t i = 0; i < structure_results.size(); ++i) {
// crop image
roi_img = std::move(Utility::crop_image(img, structure_results[i].box));
if (structure_results[i].type == "table" && table) {
Expand Down Expand Up @@ -108,13 +108,13 @@ void PaddleStructure::table(const cv::Mat &img,
std::vector<OCRPredictResult> ocr_result;
int expand_pixel = 3;

for (int i = 0; i < img_list.size(); ++i) {
for (size_t i = 0; i < img_list.size(); ++i) {
// det
this->det(img_list[i], ocr_result);
// crop image
std::vector<cv::Mat> rec_img_list;
std::vector<int> ocr_box;
for (int j = 0; j < ocr_result.size(); j++) {
for (size_t j = 0; j < ocr_result.size(); ++j) {
ocr_box = std::move(Utility::xyxyxyxy2xyxy(ocr_result[j].box));
ocr_box[0] = std::max(0, ocr_box[0] - expand_pixel);
ocr_box[1] = std::max(0, ocr_box[1] - expand_pixel),
Expand Down Expand Up @@ -144,16 +144,16 @@ std::string PaddleStructure::rebuild_table(

std::vector<int> ocr_box;
std::vector<int> structure_box;
for (int i = 0; i < ocr_result.size(); ++i) {
for (size_t i = 0; i < ocr_result.size(); ++i) {
ocr_box = std::move(Utility::xyxyxyxy2xyxy(ocr_result[i].box));
ocr_box[0] -= 1;
ocr_box[1] -= 1;
ocr_box[2] += 1;
ocr_box[3] += 1;
std::vector<std::vector<float>> dis_list(structure_boxes.size(),
std::vector<float>(3, 100000.0));
for (int j = 0; j < structure_boxes.size(); j++) {
if (structure_boxes[i].size() == 8) {
for (size_t j = 0; j < structure_boxes.size(); ++j) {
if (structure_boxes[j].size() == 8) {
structure_box = std::move(Utility::xyxyxyxy2xyxy(structure_boxes[j]));
} else {
structure_box = structure_boxes[j];
Expand All @@ -171,7 +171,7 @@ std::string PaddleStructure::rebuild_table(
// get pred html
std::string html_str = "";
int td_tag_idx = 0;
for (int i = 0; i < structure_html_tags.size(); ++i) {
for (size_t i = 0; i < structure_html_tags.size(); ++i) {
if (structure_html_tags[i].find("</td>") != std::string::npos) {
if (structure_html_tags[i].find("<td></td>") != std::string::npos) {
html_str += "<td>";
Expand All @@ -183,7 +183,7 @@ std::string PaddleStructure::rebuild_table(
b_with = true;
html_str += "<b>";
}
for (int j = 0; j < matched[td_tag_idx].size(); j++) {
for (size_t j = 0; j < matched[td_tag_idx].size(); ++j) {
std::string content = matched[td_tag_idx][j];
if (matched[td_tag_idx].size() > 1) {
// remove blank, <b> and </b>
Expand Down
Loading

0 comments on commit 02f106d

Please sign in to comment.