PaddleOCR/deploy/cpp_infer/include/ocr_rec.h

90 lines
3.0 KiB
C
Raw Permalink Normal View History

2020-07-13 01:21:47 +08:00
// Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2021-08-11 21:04:47 +08:00
#pragma once
2020-07-13 01:21:47 +08:00
#include "paddle_api.h"
#include "paddle_inference_api.h"
[Cherry-pick] Cherry-pick from release/2.6 (#11092) * Update recognition_en.md (#10059) ic15_dict.txt only have 36 digits * Update ocr_rec.h (#9469) It is enough to include preprocess_op.h, we do not need to include ocr_cls.h. * 补充num_classes注释说明 (#10073) ser_vi_layoutxlm_xfund_zh.yml中的Architecture.Backbone.num_classes所赋值会设置给Loss.num_classes, 由于采用BIO标注,假设字典中包含n个字段(包含other)时,则类别数为2n-1;假设字典中包含n个字段(不含other)时,则类别数为2n+1。 * Update algorithm_overview_en.md (#9747) Fix links to super-resolution algorithm docs * 改进文档`deploy/hubserving/readme.md`和`doc/doc_ch/models_list.md` (#9110) * Update readme.md * Update readme.md * Update readme.md * Update models_list.md * trim trailling spaces @ `deploy/hubserving/readme_en.md` * `s/shell/bash/` @ `deploy/hubserving/readme_en.md` * Update `deploy/hubserving/readme_en.md` to sync with `deploy/hubserving/readme.md` * Update deploy/hubserving/readme_en.md to sync with `deploy/hubserving/readme.md` * Update deploy/hubserving/readme_en.md to sync with `deploy/hubserving/readme.md` * Update `doc/doc_en/models_list_en.md` to sync with `doc/doc_ch/models_list_en.md` * using Grammarly to weak `deploy/hubserving/readme_en.md` * using Grammarly to tweak `doc/doc_en/models_list_en.md` * `ocr_system` module will return with values of field `confidence` * Update README_CN.md * 修复测试服务中图片转Base64的引用地址错误。 (#8334) * Update application.md * [Doc] Fix 404 link. (#10318) * Update PP-OCRv3_det_train.md * Update knowledge_distillation.md * Update config.md * Fix fitz camelCase deprecation and .PDF not being recognized as pdf file (#10181) * Fix fitz camelCase deprecation and .PDF not being recognized as pdf file * refactor get_image_file_list function * Update customize.md (#10325) * Update FAQ.md (#10345) * Update FAQ.md (#10349) * Don't break overall processing on a bad image (#10216) * Add preprocessing common to OCR tasks (#10217) Add preprocessing to options * [MLU] add mlu device for infer (#10249) * Create newfeature.md * Update newfeature.md * remove unused imported module, so can avoid PyInstaller packaged binary's start-time not found module error. (#10502) * CV套件建设专项活动 - 文字识别返回单字识别坐标 (#10515) * modification of return word box * update_implements * Update rec_postprocess.py * Update utility.py * Update README_ch.md * revert README_ch.md update * Fixed Layout recovery README file (#10493) Co-authored-by: Shubham Chambhare <shubhamchambhare@zoop.one> * update_doc * bugfix --------- Co-authored-by: ChuongLoc <89434232+ChuongLoc@users.noreply.github.com> Co-authored-by: Wang Xin <xinwang614@gmail.com> Co-authored-by: tanjh <dtdhinjapan@gmail.com> Co-authored-by: Louis Maddox <lmmx@users.noreply.github.com> Co-authored-by: n0099 <n@n0099.net> Co-authored-by: zhenliang li <37922155+shouyong@users.noreply.github.com> Co-authored-by: itasli <ilyas.tasli@outlook.fr> Co-authored-by: UserUnknownFactor <63057995+UserUnknownFactor@users.noreply.github.com> Co-authored-by: PeiyuLau <135964669+PeiyuLau@users.noreply.github.com> Co-authored-by: kerneltravel <kjpioo2006@gmail.com> Co-authored-by: ToddBear <43341135+ToddBear@users.noreply.github.com> Co-authored-by: Ligoml <39876205+Ligoml@users.noreply.github.com> Co-authored-by: Shubham Chambhare <59397280+Shubham654@users.noreply.github.com> Co-authored-by: Shubham Chambhare <shubhamchambhare@zoop.one> Co-authored-by: andyj <87074272+andyjpaddle@users.noreply.github.com>
2023-10-18 17:37:23 +08:00
#include <include/preprocess_op.h>
2020-07-13 16:59:21 +08:00
#include <include/utility.h>
2020-07-13 01:21:47 +08:00
namespace PaddleOCR {
class CRNNRecognizer {
public:
2020-07-13 21:05:36 +08:00
explicit CRNNRecognizer(const std::string &model_dir, const bool &use_gpu,
const int &gpu_id, const int &gpu_mem,
const int &cpu_math_library_num_threads,
2022-09-20 11:40:05 +08:00
const bool &use_mkldnn, const std::string &label_path,
2022-04-03 16:56:16 +08:00
const bool &use_tensorrt,
const std::string &precision,
2022-04-22 17:08:01 +08:00
const int &rec_batch_num, const int &rec_img_h,
const int &rec_img_w) {
2020-07-13 16:59:21 +08:00
this->use_gpu_ = use_gpu;
this->gpu_id_ = gpu_id;
this->gpu_mem_ = gpu_mem;
this->cpu_math_library_num_threads_ = cpu_math_library_num_threads;
2020-07-14 13:40:35 +08:00
this->use_mkldnn_ = use_mkldnn;
this->use_tensorrt_ = use_tensorrt;
2021-08-16 16:52:21 +08:00
this->precision_ = precision;
2021-11-03 15:20:22 +08:00
this->rec_batch_num_ = rec_batch_num;
2022-04-22 17:08:01 +08:00
this->rec_img_h_ = rec_img_h;
this->rec_img_w_ = rec_img_w;
std::vector<int> rec_image_shape = {3, rec_img_h, rec_img_w};
this->rec_image_shape_ = rec_image_shape;
2020-07-13 16:59:21 +08:00
this->label_list_ = Utility::ReadDict(label_path);
this->label_list_.insert(this->label_list_.begin(),
"#"); // blank char for ctc
2020-07-15 21:33:26 +08:00
this->label_list_.push_back(" ");
2020-07-13 21:05:36 +08:00
LoadModel(model_dir);
2020-07-13 01:21:47 +08:00
}
// Load Paddle inference model
2020-07-13 16:59:21 +08:00
void LoadModel(const std::string &model_dir);
2020-07-13 01:21:47 +08:00
2022-04-03 16:56:16 +08:00
void Run(std::vector<cv::Mat> img_list, std::vector<std::string> &rec_texts,
std::vector<float> &rec_text_scores, std::vector<double> &times);
2020-07-13 01:21:47 +08:00
private:
2022-09-20 11:40:05 +08:00
std::shared_ptr<paddle_infer::Predictor> predictor_;
2020-07-13 01:21:47 +08:00
2020-07-13 16:59:21 +08:00
bool use_gpu_ = false;
int gpu_id_ = 0;
int gpu_mem_ = 4000;
int cpu_math_library_num_threads_ = 4;
2020-07-14 13:40:35 +08:00
bool use_mkldnn_ = false;
2020-07-13 16:59:21 +08:00
2020-07-13 01:21:47 +08:00
std::vector<std::string> label_list_;
std::vector<float> mean_ = {0.5f, 0.5f, 0.5f};
std::vector<float> scale_ = {1 / 0.5f, 1 / 0.5f, 1 / 0.5f};
bool is_scale_ = true;
bool use_tensorrt_ = false;
2021-08-16 16:52:21 +08:00
std::string precision_ = "fp32";
2021-11-03 15:20:22 +08:00
int rec_batch_num_ = 6;
2022-04-22 17:08:01 +08:00
int rec_img_h_ = 32;
int rec_img_w_ = 320;
std::vector<int> rec_image_shape_ = {3, rec_img_h_, rec_img_w_};
2020-07-13 01:21:47 +08:00
// pre-process
CrnnResizeImg resize_op_;
Normalize normalize_op_;
2021-11-03 17:24:52 +08:00
PermuteBatch permute_op_;
2020-07-13 01:21:47 +08:00
}; // class CrnnRecognizer
2020-07-15 21:33:26 +08:00
} // namespace PaddleOCR