object_detector.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 <ctime>
  16. #include <memory>
  17. #include <numeric>
  18. #include <string>
  19. #include <utility>
  20. #include <vector>
  21. #include <opencv2/core/core.hpp>
  22. #include <opencv2/highgui/highgui.hpp>
  23. #include <opencv2/imgproc/imgproc.hpp>
  24. #include "paddle_inference_api.h" // NOLINT
  25. #include "include/config_parser.h"
  26. #include "include/picodet_postprocess.h"
  27. #include "include/preprocess_op.h"
  28. #include "include/utils.h"
  29. using namespace paddle_infer;
  30. namespace PaddleDetection {
  31. // Generate visualization colormap for each class
  32. std::vector<int> GenerateColorMap(int num_class);
  33. // Visualiztion Detection Result
  34. cv::Mat VisualizeResult(
  35. const cv::Mat& img,
  36. const std::vector<PaddleDetection::ObjectResult>& results,
  37. const std::vector<std::string>& lables,
  38. const std::vector<int>& colormap,
  39. const bool is_rbox);
  40. class ObjectDetector {
  41. public:
  42. explicit ObjectDetector(const std::string& model_dir,
  43. const std::string& device = "CPU",
  44. bool use_mkldnn = false,
  45. int cpu_threads = 1,
  46. const std::string& run_mode = "paddle",
  47. const int batch_size = 1,
  48. const int gpu_id = 0,
  49. const int trt_min_shape = 1,
  50. const int trt_max_shape = 1280,
  51. const int trt_opt_shape = 640,
  52. bool trt_calib_mode = false) {
  53. this->device_ = device;
  54. this->gpu_id_ = gpu_id;
  55. this->cpu_math_library_num_threads_ = cpu_threads;
  56. this->use_mkldnn_ = use_mkldnn;
  57. this->trt_min_shape_ = trt_min_shape;
  58. this->trt_max_shape_ = trt_max_shape;
  59. this->trt_opt_shape_ = trt_opt_shape;
  60. this->trt_calib_mode_ = trt_calib_mode;
  61. config_.load_config(model_dir);
  62. this->use_dynamic_shape_ = config_.use_dynamic_shape_;
  63. this->min_subgraph_size_ = config_.min_subgraph_size_;
  64. threshold_ = config_.draw_threshold_;
  65. preprocessor_.Init(config_.preprocess_info_);
  66. LoadModel(model_dir, batch_size, run_mode);
  67. }
  68. // Load Paddle inference model
  69. void LoadModel(const std::string& model_dir,
  70. const int batch_size = 1,
  71. const std::string& run_mode = "paddle");
  72. // Run predictor
  73. void Predict(const std::vector<cv::Mat> imgs,
  74. const double threshold = 0.5,
  75. const int warmup = 0,
  76. const int repeats = 1,
  77. std::vector<PaddleDetection::ObjectResult>* result = nullptr,
  78. std::vector<int>* bbox_num = nullptr,
  79. std::vector<double>* times = nullptr);
  80. // Get Model Label list
  81. const std::vector<std::string>& GetLabelList() const {
  82. return config_.label_list_;
  83. }
  84. private:
  85. std::string device_ = "CPU";
  86. int gpu_id_ = 0;
  87. int cpu_math_library_num_threads_ = 1;
  88. bool use_mkldnn_ = false;
  89. int min_subgraph_size_ = 3;
  90. bool use_dynamic_shape_ = false;
  91. int trt_min_shape_ = 1;
  92. int trt_max_shape_ = 1280;
  93. int trt_opt_shape_ = 640;
  94. bool trt_calib_mode_ = false;
  95. // Preprocess image and copy data to input buffer
  96. void Preprocess(const cv::Mat& image_mat);
  97. // Postprocess result
  98. void Postprocess(const std::vector<cv::Mat> mats,
  99. std::vector<PaddleDetection::ObjectResult>* result,
  100. std::vector<int> bbox_num,
  101. std::vector<float> output_data_,
  102. std::vector<int> output_mask_data_,
  103. bool is_rbox);
  104. std::shared_ptr<Predictor> predictor_;
  105. Preprocessor preprocessor_;
  106. ImageBlob inputs_;
  107. float threshold_;
  108. ConfigPaser config_;
  109. };
  110. } // namespace PaddleDetection