ocr_rec.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #include "paddle_api.h"
  16. #include "paddle_inference_api.h"
  17. #include <include/ocr_cls.h>
  18. #include <include/utility.h>
  19. namespace PaddleOCR {
  20. class CRNNRecognizer {
  21. public:
  22. explicit CRNNRecognizer(const std::string &model_dir, const bool &use_gpu,
  23. const int &gpu_id, const int &gpu_mem,
  24. const int &cpu_math_library_num_threads,
  25. const bool &use_mkldnn, const std::string &label_path,
  26. const bool &use_tensorrt,
  27. const std::string &precision,
  28. const int &rec_batch_num, const int &rec_img_h,
  29. const int &rec_img_w) {
  30. this->use_gpu_ = use_gpu;
  31. this->gpu_id_ = gpu_id;
  32. this->gpu_mem_ = gpu_mem;
  33. this->cpu_math_library_num_threads_ = cpu_math_library_num_threads;
  34. this->use_mkldnn_ = use_mkldnn;
  35. this->use_tensorrt_ = use_tensorrt;
  36. this->precision_ = precision;
  37. this->rec_batch_num_ = rec_batch_num;
  38. this->rec_img_h_ = rec_img_h;
  39. this->rec_img_w_ = rec_img_w;
  40. std::vector<int> rec_image_shape = {3, rec_img_h, rec_img_w};
  41. this->rec_image_shape_ = rec_image_shape;
  42. this->label_list_ = Utility::ReadDict(label_path);
  43. this->label_list_.insert(this->label_list_.begin(),
  44. "#"); // blank char for ctc
  45. this->label_list_.push_back(" ");
  46. LoadModel(model_dir);
  47. }
  48. // Load Paddle inference model
  49. void LoadModel(const std::string &model_dir);
  50. void Run(std::vector<cv::Mat> img_list, std::vector<std::string> &rec_texts,
  51. std::vector<float> &rec_text_scores, std::vector<double> &times);
  52. private:
  53. std::shared_ptr<paddle_infer::Predictor> predictor_;
  54. bool use_gpu_ = false;
  55. int gpu_id_ = 0;
  56. int gpu_mem_ = 4000;
  57. int cpu_math_library_num_threads_ = 4;
  58. bool use_mkldnn_ = false;
  59. std::vector<std::string> label_list_;
  60. std::vector<float> mean_ = {0.5f, 0.5f, 0.5f};
  61. std::vector<float> scale_ = {1 / 0.5f, 1 / 0.5f, 1 / 0.5f};
  62. bool is_scale_ = true;
  63. bool use_tensorrt_ = false;
  64. std::string precision_ = "fp32";
  65. int rec_batch_num_ = 6;
  66. int rec_img_h_ = 32;
  67. int rec_img_w_ = 320;
  68. std::vector<int> rec_image_shape_ = {3, rec_img_h_, rec_img_w_};
  69. // pre-process
  70. CrnnResizeImg resize_op_;
  71. Normalize normalize_op_;
  72. PermuteBatch permute_op_;
  73. }; // class CrnnRecognizer
  74. } // namespace PaddleOCR