ocr_cls.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #include "opencv2/core.hpp"
  15. #include "opencv2/imgcodecs.hpp"
  16. #include "opencv2/imgproc.hpp"
  17. #include "paddle_api.h"
  18. #include "paddle_inference_api.h"
  19. #include <chrono>
  20. #include <iomanip>
  21. #include <iostream>
  22. #include <ostream>
  23. #include <vector>
  24. #include <cstring>
  25. #include <fstream>
  26. #include <numeric>
  27. #include <include/preprocess_op.h>
  28. #include <include/utility.h>
  29. using namespace paddle_infer;
  30. namespace PaddleOCR {
  31. class Classifier {
  32. public:
  33. explicit Classifier(const std::string &model_dir, const bool &use_gpu,
  34. const int &gpu_id, const int &gpu_mem,
  35. const int &cpu_math_library_num_threads,
  36. const bool &use_mkldnn, const double &cls_thresh,
  37. const bool &use_tensorrt, const bool &use_fp16) {
  38. this->use_gpu_ = use_gpu;
  39. this->gpu_id_ = gpu_id;
  40. this->gpu_mem_ = gpu_mem;
  41. this->cpu_math_library_num_threads_ = cpu_math_library_num_threads;
  42. this->use_mkldnn_ = use_mkldnn;
  43. this->cls_thresh = cls_thresh;
  44. this->use_tensorrt_ = use_tensorrt;
  45. this->use_fp16_ = use_fp16;
  46. LoadModel(model_dir);
  47. }
  48. // Load Paddle inference model
  49. void LoadModel(const std::string &model_dir);
  50. cv::Mat Run(cv::Mat &img);
  51. private:
  52. std::shared_ptr<Predictor> predictor_;
  53. bool use_gpu_ = false;
  54. int gpu_id_ = 0;
  55. int gpu_mem_ = 4000;
  56. int cpu_math_library_num_threads_ = 4;
  57. bool use_mkldnn_ = false;
  58. double cls_thresh = 0.5;
  59. std::vector<float> mean_ = {0.5f, 0.5f, 0.5f};
  60. std::vector<float> scale_ = {1 / 0.5f, 1 / 0.5f, 1 / 0.5f};
  61. bool is_scale_ = true;
  62. bool use_tensorrt_ = false;
  63. bool use_fp16_ = false;
  64. // pre-process
  65. ClsResizeImg resize_op_;
  66. Normalize normalize_op_;
  67. Permute permute_op_;
  68. }; // class Classifier
  69. } // namespace PaddleOCR