ocr_cls.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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/preprocess_op.h>
  18. #include <include/utility.h>
  19. namespace PaddleOCR {
  20. class Classifier {
  21. public:
  22. explicit Classifier(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 double &cls_thresh,
  26. const bool &use_tensorrt, const std::string &precision,
  27. const int &cls_batch_num) {
  28. this->use_gpu_ = use_gpu;
  29. this->gpu_id_ = gpu_id;
  30. this->gpu_mem_ = gpu_mem;
  31. this->cpu_math_library_num_threads_ = cpu_math_library_num_threads;
  32. this->use_mkldnn_ = use_mkldnn;
  33. this->cls_thresh = cls_thresh;
  34. this->use_tensorrt_ = use_tensorrt;
  35. this->precision_ = precision;
  36. this->cls_batch_num_ = cls_batch_num;
  37. LoadModel(model_dir);
  38. }
  39. double cls_thresh = 0.9;
  40. // Load Paddle inference model
  41. void LoadModel(const std::string &model_dir);
  42. void Run(std::vector<cv::Mat> img_list, std::vector<int> &cls_labels,
  43. std::vector<float> &cls_scores, std::vector<double> &times);
  44. private:
  45. std::shared_ptr<paddle_infer::Predictor> predictor_;
  46. bool use_gpu_ = false;
  47. int gpu_id_ = 0;
  48. int gpu_mem_ = 4000;
  49. int cpu_math_library_num_threads_ = 4;
  50. bool use_mkldnn_ = false;
  51. std::vector<float> mean_ = {0.5f, 0.5f, 0.5f};
  52. std::vector<float> scale_ = {1 / 0.5f, 1 / 0.5f, 1 / 0.5f};
  53. bool is_scale_ = true;
  54. bool use_tensorrt_ = false;
  55. std::string precision_ = "fp32";
  56. int cls_batch_num_ = 1;
  57. // pre-process
  58. ClsResizeImg resize_op_;
  59. Normalize normalize_op_;
  60. PermuteBatch permute_op_;
  61. }; // class Classifier
  62. } // namespace PaddleOCR