object_detector.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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
  35. VisualizeResult(const cv::Mat &img,
  36. const std::vector<PaddleDetection::ObjectResult> &results,
  37. const std::vector<std::string> &lables,
  38. const std::vector<int> &colormap, const bool is_rbox);
  39. class ObjectDetector {
  40. public:
  41. explicit ObjectDetector(const std::string &model_dir,
  42. const std::string &device = "CPU",
  43. bool use_mkldnn = false, int cpu_threads = 1,
  44. const std::string &run_mode = "paddle",
  45. const int batch_size = 1, const int gpu_id = 0,
  46. const int trt_min_shape = 1,
  47. const int trt_max_shape = 1280,
  48. const int trt_opt_shape = 640,
  49. bool trt_calib_mode = false) {
  50. this->device_ = device;
  51. this->gpu_id_ = gpu_id;
  52. this->cpu_math_library_num_threads_ = cpu_threads;
  53. this->use_mkldnn_ = use_mkldnn;
  54. this->trt_min_shape_ = trt_min_shape;
  55. this->trt_max_shape_ = trt_max_shape;
  56. this->trt_opt_shape_ = trt_opt_shape;
  57. this->trt_calib_mode_ = trt_calib_mode;
  58. config_.load_config(model_dir);
  59. this->use_dynamic_shape_ = config_.use_dynamic_shape_;
  60. this->min_subgraph_size_ = config_.min_subgraph_size_;
  61. threshold_ = config_.draw_threshold_;
  62. preprocessor_.Init(config_.preprocess_info_);
  63. LoadModel(model_dir, batch_size, run_mode);
  64. }
  65. // Load Paddle inference model
  66. void LoadModel(const std::string &model_dir, const int batch_size = 1,
  67. const std::string &run_mode = "paddle");
  68. // Run predictor
  69. void Predict(const std::vector<cv::Mat> imgs, const double threshold = 0.5,
  70. const int warmup = 0, const int repeats = 1,
  71. std::vector<PaddleDetection::ObjectResult> *result = nullptr,
  72. std::vector<int> *bbox_num = nullptr,
  73. std::vector<double> *times = nullptr);
  74. // Get Model Label list
  75. const std::vector<std::string> &GetLabelList() const {
  76. return config_.label_list_;
  77. }
  78. private:
  79. std::string device_ = "CPU";
  80. int gpu_id_ = 0;
  81. int cpu_math_library_num_threads_ = 1;
  82. bool use_mkldnn_ = false;
  83. int min_subgraph_size_ = 3;
  84. bool use_dynamic_shape_ = false;
  85. int trt_min_shape_ = 1;
  86. int trt_max_shape_ = 1280;
  87. int trt_opt_shape_ = 640;
  88. bool trt_calib_mode_ = false;
  89. // Preprocess image and copy data to input buffer
  90. void Preprocess(const cv::Mat &image_mat);
  91. // Postprocess result
  92. void Postprocess(const std::vector<cv::Mat> mats,
  93. std::vector<PaddleDetection::ObjectResult> *result,
  94. std::vector<int> bbox_num, std::vector<float> output_data_,
  95. std::vector<int> output_mask_data_, bool is_rbox);
  96. void SOLOv2Postprocess(
  97. const std::vector<cv::Mat> mats, std::vector<ObjectResult> *result,
  98. std::vector<int> *bbox_num, std::vector<int> out_bbox_num_data_,
  99. std::vector<int64_t> out_label_data_, std::vector<float> out_score_data_,
  100. std::vector<uint8_t> out_global_mask_data_, float threshold = 0.5);
  101. std::shared_ptr<Predictor> predictor_;
  102. Preprocessor preprocessor_;
  103. ImageBlob inputs_;
  104. float threshold_;
  105. ConfigPaser config_;
  106. };
  107. } // namespace PaddleDetection