Skip to content

Commit

Permalink
CPP: emplace_back() replaces many push_back() ...
Browse files Browse the repository at this point in the history
  • Loading branch information
nonwill committed Feb 2, 2025
1 parent 19c58a3 commit 561b8d4
Show file tree
Hide file tree
Showing 23 changed files with 389 additions and 387 deletions.
19 changes: 13 additions & 6 deletions deploy/cpp_infer/include/clipper.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,15 @@ struct IntPoint {
#ifdef use_xyz
cInt Z;
IntPoint(cInt x = 0, cInt y = 0, cInt z = 0) : X(x), Y(y), Z(z){};
IntPoint(IntPoint const &ip) : X(ip.X), Y(ip.Y), Z(ip.Z) {}
#else
IntPoint(cInt x = 0, cInt y = 0) : X(x), Y(y){};
IntPoint(IntPoint const &ip) : X(ip.X), Y(ip.Y) {}
#endif

inline void reset(cInt x = 0, cInt y = 0) noexcept
{ X = x; Y = y; }

friend inline bool operator==(const IntPoint &a, const IntPoint &b) {
return a.X == b.X && a.Y == b.Y;
}
Expand All @@ -102,12 +107,12 @@ struct IntPoint {
typedef std::vector<IntPoint> Path;
typedef std::vector<Path> Paths;

inline Path &operator<<(Path &poly, const IntPoint &p) {
poly.push_back(p);
inline Path &operator<<(Path &poly, IntPoint&& p) {
poly.emplace_back(std::forward<IntPoint>(p));
return poly;
}
inline Paths &operator<<(Paths &polys, const Path &p) {
polys.push_back(p);
inline Paths &operator<<(Paths &polys, Path&& p) {
polys.emplace_back(std::forward<Path>(p));
return polys;
}

Expand All @@ -118,8 +123,10 @@ std::ostream &operator<<(std::ostream &s, const Paths &p);
struct DoublePoint {
double X;
double Y;
DoublePoint(double x = 0, double y = 0) : X(x), Y(y) {}
DoublePoint(IntPoint ip) : X((double)ip.X), Y((double)ip.Y) {}
DoublePoint(double x = 0, double y = 0) noexcept : X(x), Y(y) {}
DoublePoint(IntPoint const &ip) noexcept : X((double)ip.X), Y((double)ip.Y) {}
inline void reset(double x = 0, double y = 0) noexcept
{ X = x; Y = y; }
};
//------------------------------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion deploy/cpp_infer/include/ocr_cls.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Classifier {
// Load Paddle inference model
void LoadModel(const std::string &model_dir);

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

private:
Expand Down
2 changes: 1 addition & 1 deletion deploy/cpp_infer/include/ocr_det.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class DBDetector {
void LoadModel(const std::string &model_dir);

// Run predictor
void Run(cv::Mat &img, std::vector<std::vector<std::vector<int>>> &boxes,
void Run(const cv::Mat &img, std::vector<std::vector<std::vector<int>>> &boxes,
std::vector<double> &times);

private:
Expand Down
6 changes: 3 additions & 3 deletions deploy/cpp_infer/include/ocr_rec.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ class CRNNRecognizer {
this->rec_image_shape_ = rec_image_shape;

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

LoadModel(model_dir);
}

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

void Run(std::vector<cv::Mat> img_list, std::vector<std::string> &rec_texts,
void Run(const std::vector<cv::Mat> &img_list, std::vector<std::string> &rec_texts,
std::vector<float> &rec_text_scores, std::vector<double> &times);

private:
Expand Down
12 changes: 6 additions & 6 deletions deploy/cpp_infer/include/paddleocr.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ namespace PaddleOCR {
class PPOCR {
public:
explicit PPOCR();
~PPOCR() = default;
virtual ~PPOCR(){};

std::vector<std::vector<OCRPredictResult>> ocr(std::vector<cv::Mat> img_list,
std::vector<std::vector<OCRPredictResult>> ocr(const std::vector<cv::Mat> &img_list,
bool det = true,
bool rec = true,
bool cls = true);
std::vector<OCRPredictResult> ocr(cv::Mat img, bool det = true,
std::vector<OCRPredictResult> ocr(const cv::Mat &img, bool det = true,
bool rec = true, bool cls = true);

void reset_timer();
Expand All @@ -40,10 +40,10 @@ class PPOCR {
std::vector<double> time_info_rec = {0, 0, 0};
std::vector<double> time_info_cls = {0, 0, 0};

void det(cv::Mat img, std::vector<OCRPredictResult> &ocr_results);
void rec(std::vector<cv::Mat> img_list,
void det(const cv::Mat &img, std::vector<OCRPredictResult> &ocr_results);
void rec(const std::vector<cv::Mat> &img_list,
std::vector<OCRPredictResult> &ocr_results);
void cls(std::vector<cv::Mat> img_list,
void cls(const std::vector<cv::Mat> &img_list,
std::vector<OCRPredictResult> &ocr_results);

private:
Expand Down
12 changes: 6 additions & 6 deletions deploy/cpp_infer/include/paddlestructure.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class PaddleStructure : public PPOCR {
explicit PaddleStructure();
~PaddleStructure() = default;

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

void layout(cv::Mat img,
void layout(const cv::Mat &img,
std::vector<StructurePredictResult> &structure_result);

void table(cv::Mat img, StructurePredictResult &structure_result);
void table(const cv::Mat &img, StructurePredictResult &structure_result);

std::string rebuild_table(std::vector<std::string> rec_html_tags,
std::vector<std::vector<int>> rec_boxes,
std::string rebuild_table(const std::vector<std::string> &rec_html_tags,
const std::vector<std::vector<int>> &rec_boxes,
std::vector<OCRPredictResult> &ocr_result);

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

static bool comparison_dis(const std::vector<float> &dis1,
const std::vector<float> &dis2) {
Expand Down
63 changes: 33 additions & 30 deletions deploy/cpp_infer/include/postprocess_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,49 +24,48 @@ class DBPostProcessor {
void GetContourArea(const std::vector<std::vector<float>> &box,
float unclip_ratio, float &distance);

cv::RotatedRect UnClip(std::vector<std::vector<float>> box,
cv::RotatedRect UnClip(const std::vector<std::vector<float>> &box,
const float &unclip_ratio);

float **Mat2Vec(cv::Mat mat);
float **Mat2Vec(const cv::Mat &mat);

std::vector<std::vector<int>>
OrderPointsClockwise(std::vector<std::vector<int>> pts);
OrderPointsClockwise(const std::vector<std::vector<int>> &pts);

std::vector<std::vector<float>> GetMiniBoxes(cv::RotatedRect box,
std::vector<std::vector<float>> GetMiniBoxes(const cv::RotatedRect &box,
float &ssid);

float BoxScoreFast(std::vector<std::vector<float>> box_array, cv::Mat pred);
float PolygonScoreAcc(std::vector<cv::Point> contour, cv::Mat pred);
float BoxScoreFast(const std::vector<std::vector<float>> &box_array, const cv::Mat &pred);
float PolygonScoreAcc(const std::vector<cv::Point> &contour, const cv::Mat &pred);

std::vector<std::vector<std::vector<int>>>
BoxesFromBitmap(const cv::Mat pred, const cv::Mat bitmap,
BoxesFromBitmap(const cv::Mat &pred, const cv::Mat &bitmap,
const float &box_thresh, const float &det_db_unclip_ratio,
const std::string &det_db_score_mode);

std::vector<std::vector<std::vector<int>>>
FilterTagDetRes(std::vector<std::vector<std::vector<int>>> boxes,
float ratio_h, float ratio_w, cv::Mat srcimg);
void FilterTagDetRes(std::vector<std::vector<std::vector<int>>> &boxes,
float ratio_h, float ratio_w, const cv::Mat &srcimg);

private:
static bool XsortInt(std::vector<int> a, std::vector<int> b);
static bool XsortInt(const std::vector<int> &a, const std::vector<int> &b);

static bool XsortFp32(std::vector<float> a, std::vector<float> b);
static bool XsortFp32(const std::vector<float> &a, const std::vector<float> &b);

std::vector<std::vector<float>> Mat2Vector(cv::Mat mat);
std::vector<std::vector<float>> Mat2Vector(const cv::Mat &mat);

inline int _max(int a, int b) { return a >= b ? a : b; }
inline int _max(int a, int b) const noexcept { return a >= b ? a : b; }

inline int _min(int a, int b) { return a >= b ? b : a; }
inline int _min(int a, int b) const noexcept { return a >= b ? b : a; }

template <class T> inline T clamp(T x, T min, T max) {
template <class T> inline T clamp(T x, T min, T max) const noexcept {
if (x > max)
return max;
if (x < min)
return min;
return x;
}

inline float clampf(float x, float min, float max) {
inline float clampf(float x, float min, float max) const noexcept {
if (x > max)
return max;
if (x < min)
Expand All @@ -77,37 +76,41 @@ class DBPostProcessor {

class TablePostProcessor {
public:
void init(std::string label_path, bool merge_no_span_structure = true);
void Run(std::vector<float> &loc_preds, std::vector<float> &structure_probs,
std::vector<float> &rec_scores, std::vector<int> &loc_preds_shape,
std::vector<int> &structure_probs_shape,
void init(const std::string &label_path, bool merge_no_span_structure = true);
void Run(const std::vector<float> &loc_preds, const std::vector<float> &structure_probs,
std::vector<float> &rec_scores, const std::vector<int> &loc_preds_shape,
const std::vector<int> &structure_probs_shape,
std::vector<std::vector<std::string>> &rec_html_tag_batch,
std::vector<std::vector<std::vector<int>>> &rec_boxes_batch,
std::vector<int> &width_list, std::vector<int> &height_list);
const std::vector<int> &width_list, const std::vector<int> &height_list);

private:
std::vector<std::string> label_list_;
std::string end = "eos";
std::string beg = "sos";
const std::string end = "eos";
const std::string beg = "sos";
};

class PicodetPostProcessor {
public:
void init(std::string label_path, const double score_threshold = 0.4,
void init(const std::string &label_path, const double score_threshold = 0.4,
const double nms_threshold = 0.5,
const std::vector<int> &fpn_stride = {8, 16, 32, 64});
void Run(std::vector<StructurePredictResult> &results,
std::vector<std::vector<float>> outs, std::vector<int> ori_shape,
std::vector<int> resize_shape, int eg_max);
std::vector<int> fpn_stride_ = {8, 16, 32, 64};
const std::vector<std::vector<float>> &outs,
const std::vector<int> &ori_shape,
const std::vector<int> &resize_shape, int eg_max);
inline size_t fpn_stride_size() const { return fpn_stride_.size(); }

private:
StructurePredictResult disPred2Bbox(std::vector<float> bbox_pred, int label,
StructurePredictResult disPred2Bbox(const std::vector<float> &bbox_pred, int label,
float score, int x, int y, int stride,
std::vector<int> im_shape, int reg_max);
const std::vector<int> &im_shape,
int reg_max);
void nms(std::vector<StructurePredictResult> &input_boxes,
float nms_threshold);

std::vector<int> fpn_stride_ = {8, 16, 32, 64};

std::vector<std::string> label_list_;
double score_threshold_ = 0.4;
double nms_threshold_ = 0.5;
Expand Down
8 changes: 4 additions & 4 deletions deploy/cpp_infer/include/preprocess_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,25 @@ namespace PaddleOCR {

class Normalize {
public:
virtual void Run(cv::Mat *im, const std::vector<float> &mean,
virtual void Run(cv::Mat &im, const std::vector<float> &mean,
const std::vector<float> &scale, const bool is_scale = true);
};

// RGB -> CHW
class Permute {
public:
virtual void Run(const cv::Mat *im, float *data);
virtual void Run(const cv::Mat &im, float *data);
};

class PermuteBatch {
public:
virtual void Run(const std::vector<cv::Mat> imgs, float *data);
virtual void Run(const std::vector<cv::Mat> &imgs, float *data);
};

class ResizeImgType0 {
public:
virtual void Run(const cv::Mat &img, cv::Mat &resize_img,
std::string limit_type, int limit_side_len, float &ratio_h,
const std::string &limit_type, int limit_side_len, float &ratio_h,
float &ratio_w, bool use_tensorrt);
};

Expand Down
2 changes: 1 addition & 1 deletion deploy/cpp_infer/include/structure_layout.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class StructureLayoutRecognizer {
// Load Paddle inference model
void LoadModel(const std::string &model_dir);

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

private:
Expand Down
2 changes: 1 addition & 1 deletion deploy/cpp_infer/include/structure_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class StructureTableRecognizer {
// Load Paddle inference model
void LoadModel(const std::string &model_dir);

void Run(std::vector<cv::Mat> img_list,
void Run(const std::vector<cv::Mat> &img_list,
std::vector<std::vector<std::string>> &rec_html_tags,
std::vector<float> &rec_scores,
std::vector<std::vector<std::vector<int>>> &rec_boxes,
Expand Down
6 changes: 3 additions & 3 deletions deploy/cpp_infer/include/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class Utility {
std::vector<std::string> &all_inputs);

static cv::Mat GetRotateCropImage(const cv::Mat &srcimage,
std::vector<std::vector<int>> box);
const std::vector<std::vector<int>> &box);

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

Expand All @@ -88,8 +88,8 @@ class Utility {

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

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

static float fast_exp(float x);
static std::vector<float>
Expand Down
Loading

0 comments on commit 561b8d4

Please sign in to comment.