Skip to content

Commit fab4e68

Browse files
committed
CPP: emplace_back() replaces many push_back()...to improve performance
1 parent 19c58a3 commit fab4e68

23 files changed

+426
-403
lines changed

deploy/cpp_infer/include/clipper.h

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,17 @@ struct IntPoint {
8686
#ifdef use_xyz
8787
cInt Z;
8888
IntPoint(cInt x = 0, cInt y = 0, cInt z = 0) : X(x), Y(y), Z(z){};
89+
IntPoint(IntPoint const &ip) : X(ip.X), Y(ip.Y), Z(ip.Z) {}
8990
#else
9091
IntPoint(cInt x = 0, cInt y = 0) : X(x), Y(y){};
92+
IntPoint(IntPoint const &ip) : X(ip.X), Y(ip.Y) {}
9193
#endif
9294

95+
inline void reset(cInt x = 0, cInt y = 0) noexcept {
96+
X = x;
97+
Y = y;
98+
}
99+
93100
friend inline bool operator==(const IntPoint &a, const IntPoint &b) {
94101
return a.X == b.X && a.Y == b.Y;
95102
}
@@ -102,12 +109,12 @@ struct IntPoint {
102109
typedef std::vector<IntPoint> Path;
103110
typedef std::vector<Path> Paths;
104111

105-
inline Path &operator<<(Path &poly, const IntPoint &p) {
106-
poly.push_back(p);
112+
inline Path &operator<<(Path &poly, IntPoint &&p) {
113+
poly.emplace_back(std::forward<IntPoint>(p));
107114
return poly;
108115
}
109-
inline Paths &operator<<(Paths &polys, const Path &p) {
110-
polys.push_back(p);
116+
inline Paths &operator<<(Paths &polys, Path &&p) {
117+
polys.emplace_back(std::forward<Path>(p));
111118
return polys;
112119
}
113120

@@ -118,8 +125,12 @@ std::ostream &operator<<(std::ostream &s, const Paths &p);
118125
struct DoublePoint {
119126
double X;
120127
double Y;
121-
DoublePoint(double x = 0, double y = 0) : X(x), Y(y) {}
122-
DoublePoint(IntPoint ip) : X((double)ip.X), Y((double)ip.Y) {}
128+
DoublePoint(double x = 0, double y = 0) noexcept : X(x), Y(y) {}
129+
DoublePoint(IntPoint const &ip) noexcept : X((double)ip.X), Y((double)ip.Y) {}
130+
inline void reset(double x = 0, double y = 0) noexcept {
131+
X = x;
132+
Y = y;
133+
}
123134
};
124135
//------------------------------------------------------------------------------
125136

deploy/cpp_infer/include/ocr_cls.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Classifier {
4848
// Load Paddle inference model
4949
void LoadModel(const std::string &model_dir);
5050

51-
void Run(std::vector<cv::Mat> img_list, std::vector<int> &cls_labels,
51+
void Run(const std::vector<cv::Mat> &img_list, std::vector<int> &cls_labels,
5252
std::vector<float> &cls_scores, std::vector<double> &times);
5353

5454
private:

deploy/cpp_infer/include/ocr_det.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ class DBDetector {
5959
void LoadModel(const std::string &model_dir);
6060

6161
// Run predictor
62-
void Run(cv::Mat &img, std::vector<std::vector<std::vector<int>>> &boxes,
62+
void Run(const cv::Mat &img,
63+
std::vector<std::vector<std::vector<int>>> &boxes,
6364
std::vector<double> &times);
6465

6566
private:

deploy/cpp_infer/include/ocr_rec.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,18 @@ class CRNNRecognizer {
4646
this->rec_image_shape_ = rec_image_shape;
4747

4848
this->label_list_ = Utility::ReadDict(label_path);
49-
this->label_list_.insert(this->label_list_.begin(),
50-
"#"); // blank char for ctc
51-
this->label_list_.push_back(" ");
49+
this->label_list_.emplace(this->label_list_.begin(),
50+
"#"); // blank char for ctc
51+
this->label_list_.emplace_back(" ");
5252

5353
LoadModel(model_dir);
5454
}
5555

5656
// Load Paddle inference model
5757
void LoadModel(const std::string &model_dir);
5858

59-
void Run(std::vector<cv::Mat> img_list, std::vector<std::string> &rec_texts,
59+
void Run(const std::vector<cv::Mat> &img_list,
60+
std::vector<std::string> &rec_texts,
6061
std::vector<float> &rec_text_scores, std::vector<double> &times);
6162

6263
private:

deploy/cpp_infer/include/paddleocr.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@ namespace PaddleOCR {
2323
class PPOCR {
2424
public:
2525
explicit PPOCR();
26-
~PPOCR() = default;
26+
virtual ~PPOCR(){};
2727

28-
std::vector<std::vector<OCRPredictResult>> ocr(std::vector<cv::Mat> img_list,
29-
bool det = true,
30-
bool rec = true,
31-
bool cls = true);
32-
std::vector<OCRPredictResult> ocr(cv::Mat img, bool det = true,
28+
std::vector<std::vector<OCRPredictResult>>
29+
ocr(const std::vector<cv::Mat> &img_list, bool det = true, bool rec = true,
30+
bool cls = true);
31+
std::vector<OCRPredictResult> ocr(const cv::Mat &img, bool det = true,
3332
bool rec = true, bool cls = true);
3433

3534
void reset_timer();
@@ -40,10 +39,10 @@ class PPOCR {
4039
std::vector<double> time_info_rec = {0, 0, 0};
4140
std::vector<double> time_info_cls = {0, 0, 0};
4241

43-
void det(cv::Mat img, std::vector<OCRPredictResult> &ocr_results);
44-
void rec(std::vector<cv::Mat> img_list,
42+
void det(const cv::Mat &img, std::vector<OCRPredictResult> &ocr_results);
43+
void rec(const std::vector<cv::Mat> &img_list,
4544
std::vector<OCRPredictResult> &ocr_results);
46-
void cls(std::vector<cv::Mat> img_list,
45+
void cls(const std::vector<cv::Mat> &img_list,
4746
std::vector<OCRPredictResult> &ocr_results);
4847

4948
private:

deploy/cpp_infer/include/paddlestructure.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class PaddleStructure : public PPOCR {
2525
explicit PaddleStructure();
2626
~PaddleStructure() = default;
2727

28-
std::vector<StructurePredictResult> structure(cv::Mat img,
28+
std::vector<StructurePredictResult> structure(const cv::Mat &img,
2929
bool layout = false,
3030
bool table = true,
3131
bool ocr = false);
@@ -40,16 +40,16 @@ class PaddleStructure : public PPOCR {
4040
std::unique_ptr<StructureTableRecognizer> table_model_;
4141
std::unique_ptr<StructureLayoutRecognizer> layout_model_;
4242

43-
void layout(cv::Mat img,
43+
void layout(const cv::Mat &img,
4444
std::vector<StructurePredictResult> &structure_result);
4545

46-
void table(cv::Mat img, StructurePredictResult &structure_result);
46+
void table(const cv::Mat &img, StructurePredictResult &structure_result);
4747

48-
std::string rebuild_table(std::vector<std::string> rec_html_tags,
49-
std::vector<std::vector<int>> rec_boxes,
48+
std::string rebuild_table(const std::vector<std::string> &rec_html_tags,
49+
const std::vector<std::vector<int>> &rec_boxes,
5050
std::vector<OCRPredictResult> &ocr_result);
5151

52-
float dis(std::vector<int> &box1, std::vector<int> &box2);
52+
float dis(const std::vector<int> &box1, const std::vector<int> &box2);
5353

5454
static bool comparison_dis(const std::vector<float> &dis1,
5555
const std::vector<float> &dis2) {

deploy/cpp_infer/include/postprocess_op.h

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,49 +24,51 @@ class DBPostProcessor {
2424
void GetContourArea(const std::vector<std::vector<float>> &box,
2525
float unclip_ratio, float &distance);
2626

27-
cv::RotatedRect UnClip(std::vector<std::vector<float>> box,
27+
cv::RotatedRect UnClip(const std::vector<std::vector<float>> &box,
2828
const float &unclip_ratio);
2929

30-
float **Mat2Vec(cv::Mat mat);
30+
float **Mat2Vec(const cv::Mat &mat);
3131

3232
std::vector<std::vector<int>>
33-
OrderPointsClockwise(std::vector<std::vector<int>> pts);
33+
OrderPointsClockwise(const std::vector<std::vector<int>> &pts);
3434

35-
std::vector<std::vector<float>> GetMiniBoxes(cv::RotatedRect box,
35+
std::vector<std::vector<float>> GetMiniBoxes(const cv::RotatedRect &box,
3636
float &ssid);
3737

38-
float BoxScoreFast(std::vector<std::vector<float>> box_array, cv::Mat pred);
39-
float PolygonScoreAcc(std::vector<cv::Point> contour, cv::Mat pred);
38+
float BoxScoreFast(const std::vector<std::vector<float>> &box_array,
39+
const cv::Mat &pred);
40+
float PolygonScoreAcc(const std::vector<cv::Point> &contour,
41+
const cv::Mat &pred);
4042

4143
std::vector<std::vector<std::vector<int>>>
42-
BoxesFromBitmap(const cv::Mat pred, const cv::Mat bitmap,
44+
BoxesFromBitmap(const cv::Mat &pred, const cv::Mat &bitmap,
4345
const float &box_thresh, const float &det_db_unclip_ratio,
4446
const std::string &det_db_score_mode);
4547

46-
std::vector<std::vector<std::vector<int>>>
47-
FilterTagDetRes(std::vector<std::vector<std::vector<int>>> boxes,
48-
float ratio_h, float ratio_w, cv::Mat srcimg);
48+
void FilterTagDetRes(std::vector<std::vector<std::vector<int>>> &boxes,
49+
float ratio_h, float ratio_w, const cv::Mat &srcimg);
4950

5051
private:
51-
static bool XsortInt(std::vector<int> a, std::vector<int> b);
52+
static bool XsortInt(const std::vector<int> &a, const std::vector<int> &b);
5253

53-
static bool XsortFp32(std::vector<float> a, std::vector<float> b);
54+
static bool XsortFp32(const std::vector<float> &a,
55+
const std::vector<float> &b);
5456

55-
std::vector<std::vector<float>> Mat2Vector(cv::Mat mat);
57+
std::vector<std::vector<float>> Mat2Vector(const cv::Mat &mat);
5658

57-
inline int _max(int a, int b) { return a >= b ? a : b; }
59+
inline int _max(int a, int b) const noexcept { return a >= b ? a : b; }
5860

59-
inline int _min(int a, int b) { return a >= b ? b : a; }
61+
inline int _min(int a, int b) const noexcept { return a >= b ? b : a; }
6062

61-
template <class T> inline T clamp(T x, T min, T max) {
63+
template <class T> inline T clamp(T x, T min, T max) const noexcept {
6264
if (x > max)
6365
return max;
6466
if (x < min)
6567
return min;
6668
return x;
6769
}
6870

69-
inline float clampf(float x, float min, float max) {
71+
inline float clampf(float x, float min, float max) const noexcept {
7072
if (x > max)
7173
return max;
7274
if (x < min)
@@ -77,37 +79,45 @@ class DBPostProcessor {
7779

7880
class TablePostProcessor {
7981
public:
80-
void init(std::string label_path, bool merge_no_span_structure = true);
81-
void Run(std::vector<float> &loc_preds, std::vector<float> &structure_probs,
82-
std::vector<float> &rec_scores, std::vector<int> &loc_preds_shape,
83-
std::vector<int> &structure_probs_shape,
82+
void init(const std::string &label_path, bool merge_no_span_structure = true);
83+
void Run(const std::vector<float> &loc_preds,
84+
const std::vector<float> &structure_probs,
85+
std::vector<float> &rec_scores,
86+
const std::vector<int> &loc_preds_shape,
87+
const std::vector<int> &structure_probs_shape,
8488
std::vector<std::vector<std::string>> &rec_html_tag_batch,
8589
std::vector<std::vector<std::vector<int>>> &rec_boxes_batch,
86-
std::vector<int> &width_list, std::vector<int> &height_list);
90+
const std::vector<int> &width_list,
91+
const std::vector<int> &height_list);
8792

8893
private:
8994
std::vector<std::string> label_list_;
90-
std::string end = "eos";
91-
std::string beg = "sos";
95+
const std::string end = "eos";
96+
const std::string beg = "sos";
9297
};
9398

9499
class PicodetPostProcessor {
95100
public:
96-
void init(std::string label_path, const double score_threshold = 0.4,
101+
void init(const std::string &label_path, const double score_threshold = 0.4,
97102
const double nms_threshold = 0.5,
98103
const std::vector<int> &fpn_stride = {8, 16, 32, 64});
99104
void Run(std::vector<StructurePredictResult> &results,
100-
std::vector<std::vector<float>> outs, std::vector<int> ori_shape,
101-
std::vector<int> resize_shape, int eg_max);
102-
std::vector<int> fpn_stride_ = {8, 16, 32, 64};
105+
const std::vector<std::vector<float>> &outs,
106+
const std::vector<int> &ori_shape,
107+
const std::vector<int> &resize_shape, int eg_max);
108+
inline size_t fpn_stride_size() const { return fpn_stride_.size(); }
103109

104110
private:
105-
StructurePredictResult disPred2Bbox(std::vector<float> bbox_pred, int label,
106-
float score, int x, int y, int stride,
107-
std::vector<int> im_shape, int reg_max);
111+
StructurePredictResult disPred2Bbox(const std::vector<float> &bbox_pred,
112+
int label, float score, int x, int y,
113+
int stride,
114+
const std::vector<int> &im_shape,
115+
int reg_max);
108116
void nms(std::vector<StructurePredictResult> &input_boxes,
109117
float nms_threshold);
110118

119+
std::vector<int> fpn_stride_ = {8, 16, 32, 64};
120+
111121
std::vector<std::string> label_list_;
112122
double score_threshold_ = 0.4;
113123
double nms_threshold_ = 0.5;

deploy/cpp_infer/include/preprocess_op.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,26 @@ namespace PaddleOCR {
2525

2626
class Normalize {
2727
public:
28-
virtual void Run(cv::Mat *im, const std::vector<float> &mean,
28+
virtual void Run(cv::Mat &im, const std::vector<float> &mean,
2929
const std::vector<float> &scale, const bool is_scale = true);
3030
};
3131

3232
// RGB -> CHW
3333
class Permute {
3434
public:
35-
virtual void Run(const cv::Mat *im, float *data);
35+
virtual void Run(const cv::Mat &im, float *data);
3636
};
3737

3838
class PermuteBatch {
3939
public:
40-
virtual void Run(const std::vector<cv::Mat> imgs, float *data);
40+
virtual void Run(const std::vector<cv::Mat> &imgs, float *data);
4141
};
4242

4343
class ResizeImgType0 {
4444
public:
4545
virtual void Run(const cv::Mat &img, cv::Mat &resize_img,
46-
std::string limit_type, int limit_side_len, float &ratio_h,
47-
float &ratio_w, bool use_tensorrt);
46+
const std::string &limit_type, int limit_side_len,
47+
float &ratio_h, float &ratio_w, bool use_tensorrt);
4848
};
4949

5050
class CrnnResizeImg {

deploy/cpp_infer/include/structure_layout.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class StructureLayoutRecognizer {
4747
// Load Paddle inference model
4848
void LoadModel(const std::string &model_dir);
4949

50-
void Run(cv::Mat img, std::vector<StructurePredictResult> &result,
50+
void Run(const cv::Mat &img, std::vector<StructurePredictResult> &result,
5151
std::vector<double> &times);
5252

5353
private:

deploy/cpp_infer/include/structure_table.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class StructureTableRecognizer {
4848
// Load Paddle inference model
4949
void LoadModel(const std::string &model_dir);
5050

51-
void Run(std::vector<cv::Mat> img_list,
51+
void Run(const std::vector<cv::Mat> &img_list,
5252
std::vector<std::vector<std::string>> &rec_html_tags,
5353
std::vector<float> &rec_scores,
5454
std::vector<std::vector<std::vector<int>>> &rec_boxes,

0 commit comments

Comments
 (0)