fix memory leak (#10441)
* fix memory leak * update: Using smart pointers instead of raw pointerspull/7416/merge
parent
9884073e71
commit
b938161236
|
@ -23,7 +23,7 @@ namespace PaddleOCR {
|
|||
class PPOCR {
|
||||
public:
|
||||
explicit PPOCR();
|
||||
~PPOCR();
|
||||
~PPOCR() = default;
|
||||
|
||||
std::vector<std::vector<OCRPredictResult>> ocr(std::vector<cv::Mat> img_list,
|
||||
bool det = true,
|
||||
|
@ -47,9 +47,9 @@ protected:
|
|||
std::vector<OCRPredictResult> &ocr_results);
|
||||
|
||||
private:
|
||||
DBDetector *detector_ = nullptr;
|
||||
Classifier *classifier_ = nullptr;
|
||||
CRNNRecognizer *recognizer_ = nullptr;
|
||||
std::unique_ptr<DBDetector> detector_;
|
||||
std::unique_ptr<Classifier> classifier_;
|
||||
std::unique_ptr<CRNNRecognizer> recognizer_;
|
||||
};
|
||||
|
||||
} // namespace PaddleOCR
|
||||
|
|
|
@ -23,7 +23,7 @@ namespace PaddleOCR {
|
|||
class PaddleStructure : public PPOCR {
|
||||
public:
|
||||
explicit PaddleStructure();
|
||||
~PaddleStructure();
|
||||
~PaddleStructure() = default;
|
||||
|
||||
std::vector<StructurePredictResult> structure(cv::Mat img,
|
||||
bool layout = false,
|
||||
|
@ -37,8 +37,8 @@ private:
|
|||
std::vector<double> time_info_table = {0, 0, 0};
|
||||
std::vector<double> time_info_layout = {0, 0, 0};
|
||||
|
||||
StructureTableRecognizer *table_model_ = nullptr;
|
||||
StructureLayoutRecognizer *layout_model_ = nullptr;
|
||||
std::unique_ptr<StructureTableRecognizer> table_model_;
|
||||
std::unique_ptr<StructureLayoutRecognizer> layout_model_;
|
||||
|
||||
void layout(cv::Mat img,
|
||||
std::vector<StructurePredictResult> &structure_result);
|
||||
|
|
|
@ -82,7 +82,7 @@ void check_params() {
|
|||
}
|
||||
|
||||
void ocr(std::vector<cv::String> &cv_all_img_names) {
|
||||
PPOCR ocr = PPOCR();
|
||||
PPOCR ocr;
|
||||
|
||||
if (FLAGS_benchmark) {
|
||||
ocr.reset_timer();
|
||||
|
@ -120,7 +120,7 @@ void ocr(std::vector<cv::String> &cv_all_img_names) {
|
|||
}
|
||||
|
||||
void structure(std::vector<cv::String> &cv_all_img_names) {
|
||||
PaddleOCR::PaddleStructure engine = PaddleOCR::PaddleStructure();
|
||||
PaddleOCR::PaddleStructure engine;
|
||||
|
||||
if (FLAGS_benchmark) {
|
||||
engine.reset_timer();
|
||||
|
|
|
@ -21,28 +21,28 @@ namespace PaddleOCR {
|
|||
|
||||
PPOCR::PPOCR() {
|
||||
if (FLAGS_det) {
|
||||
this->detector_ = new DBDetector(
|
||||
this->detector_.reset(new DBDetector(
|
||||
FLAGS_det_model_dir, FLAGS_use_gpu, FLAGS_gpu_id, FLAGS_gpu_mem,
|
||||
FLAGS_cpu_threads, FLAGS_enable_mkldnn, FLAGS_limit_type,
|
||||
FLAGS_limit_side_len, FLAGS_det_db_thresh, FLAGS_det_db_box_thresh,
|
||||
FLAGS_det_db_unclip_ratio, FLAGS_det_db_score_mode, FLAGS_use_dilation,
|
||||
FLAGS_use_tensorrt, FLAGS_precision);
|
||||
FLAGS_use_tensorrt, FLAGS_precision));
|
||||
}
|
||||
|
||||
if (FLAGS_cls && FLAGS_use_angle_cls) {
|
||||
this->classifier_ = new Classifier(
|
||||
this->classifier_.reset(new Classifier(
|
||||
FLAGS_cls_model_dir, FLAGS_use_gpu, FLAGS_gpu_id, FLAGS_gpu_mem,
|
||||
FLAGS_cpu_threads, FLAGS_enable_mkldnn, FLAGS_cls_thresh,
|
||||
FLAGS_use_tensorrt, FLAGS_precision, FLAGS_cls_batch_num);
|
||||
FLAGS_use_tensorrt, FLAGS_precision, FLAGS_cls_batch_num));
|
||||
}
|
||||
if (FLAGS_rec) {
|
||||
this->recognizer_ = new CRNNRecognizer(
|
||||
this->recognizer_.reset(new CRNNRecognizer(
|
||||
FLAGS_rec_model_dir, FLAGS_use_gpu, FLAGS_gpu_id, FLAGS_gpu_mem,
|
||||
FLAGS_cpu_threads, FLAGS_enable_mkldnn, FLAGS_rec_char_dict_path,
|
||||
FLAGS_use_tensorrt, FLAGS_precision, FLAGS_rec_batch_num,
|
||||
FLAGS_rec_img_h, FLAGS_rec_img_w);
|
||||
FLAGS_rec_img_h, FLAGS_rec_img_w));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
std::vector<std::vector<OCRPredictResult>>
|
||||
PPOCR::ocr(std::vector<cv::Mat> img_list, bool det, bool rec, bool cls) {
|
||||
|
@ -51,7 +51,7 @@ PPOCR::ocr(std::vector<cv::Mat> img_list, bool det, bool rec, bool cls) {
|
|||
if (!det) {
|
||||
std::vector<OCRPredictResult> ocr_result;
|
||||
ocr_result.resize(img_list.size());
|
||||
if (cls && this->classifier_ != nullptr) {
|
||||
if (cls && this->classifier_) {
|
||||
this->cls(img_list, ocr_result);
|
||||
for (int i = 0; i < img_list.size(); i++) {
|
||||
if (ocr_result[i].cls_label % 2 == 1 &&
|
||||
|
@ -92,7 +92,7 @@ std::vector<OCRPredictResult> PPOCR::ocr(cv::Mat img, bool det, bool rec,
|
|||
img_list.push_back(crop_img);
|
||||
}
|
||||
// cls
|
||||
if (cls && this->classifier_ != nullptr) {
|
||||
if (cls && this->classifier_) {
|
||||
this->cls(img_list, ocr_result);
|
||||
for (int i = 0; i < img_list.size(); i++) {
|
||||
if (ocr_result[i].cls_label % 2 == 1 &&
|
||||
|
@ -190,16 +190,4 @@ void PPOCR::benchmark_log(int img_num) {
|
|||
}
|
||||
}
|
||||
|
||||
PPOCR::~PPOCR() {
|
||||
if (this->detector_ != nullptr) {
|
||||
delete this->detector_;
|
||||
}
|
||||
if (this->classifier_ != nullptr) {
|
||||
delete this->classifier_;
|
||||
}
|
||||
if (this->recognizer_ != nullptr) {
|
||||
delete this->recognizer_;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace PaddleOCR
|
||||
|
|
|
@ -21,20 +21,20 @@ namespace PaddleOCR {
|
|||
|
||||
PaddleStructure::PaddleStructure() {
|
||||
if (FLAGS_layout) {
|
||||
this->layout_model_ = new StructureLayoutRecognizer(
|
||||
this->layout_model_.reset(new StructureLayoutRecognizer(
|
||||
FLAGS_layout_model_dir, FLAGS_use_gpu, FLAGS_gpu_id, FLAGS_gpu_mem,
|
||||
FLAGS_cpu_threads, FLAGS_enable_mkldnn, FLAGS_layout_dict_path,
|
||||
FLAGS_use_tensorrt, FLAGS_precision, FLAGS_layout_score_threshold,
|
||||
FLAGS_layout_nms_threshold);
|
||||
FLAGS_layout_nms_threshold));
|
||||
}
|
||||
if (FLAGS_table) {
|
||||
this->table_model_ = new StructureTableRecognizer(
|
||||
this->table_model_.reset(new StructureTableRecognizer(
|
||||
FLAGS_table_model_dir, FLAGS_use_gpu, FLAGS_gpu_id, FLAGS_gpu_mem,
|
||||
FLAGS_cpu_threads, FLAGS_enable_mkldnn, FLAGS_table_char_dict_path,
|
||||
FLAGS_use_tensorrt, FLAGS_precision, FLAGS_table_batch_num,
|
||||
FLAGS_table_max_len, FLAGS_merge_no_span_structure);
|
||||
FLAGS_table_max_len, FLAGS_merge_no_span_structure));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
std::vector<StructurePredictResult>
|
||||
PaddleStructure::structure(cv::Mat srcimg, bool layout, bool table, bool ocr) {
|
||||
|
@ -65,7 +65,7 @@ PaddleStructure::structure(cv::Mat srcimg, bool layout, bool table, bool ocr) {
|
|||
}
|
||||
|
||||
return structure_results;
|
||||
};
|
||||
}
|
||||
|
||||
void PaddleStructure::layout(
|
||||
cv::Mat img, std::vector<StructurePredictResult> &structure_result) {
|
||||
|
@ -123,7 +123,7 @@ void PaddleStructure::table(cv::Mat img,
|
|||
structure_result.cell_box = structure_boxes[i];
|
||||
structure_result.html_score = structure_scores[i];
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
std::string
|
||||
PaddleStructure::rebuild_table(std::vector<std::string> structure_html_tags,
|
||||
|
@ -286,10 +286,4 @@ void PaddleStructure::benchmark_log(int img_num) {
|
|||
}
|
||||
}
|
||||
|
||||
PaddleStructure::~PaddleStructure() {
|
||||
if (this->table_model_ != nullptr) {
|
||||
delete this->table_model_;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace PaddleOCR
|
||||
} // namespace PaddleOCR
|
||||
|
|
Loading…
Reference in New Issue