jde_detector.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright (c) 2021 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 <string>
  18. #include <utility>
  19. #include <vector>
  20. #include <opencv2/core/core.hpp>
  21. #include <opencv2/highgui/highgui.hpp>
  22. #include <opencv2/imgproc/imgproc.hpp>
  23. #include "paddle_inference_api.h" // NOLINT
  24. #include "include/config_parser.h"
  25. #include "include/preprocess_op.h"
  26. #include "include/tracker.h"
  27. using namespace paddle_infer;
  28. namespace PaddleDetection {
  29. // JDE Detection Result
  30. struct MOT_Rect {
  31. float left;
  32. float top;
  33. float right;
  34. float bottom;
  35. };
  36. struct MOT_Track {
  37. int ids;
  38. float score;
  39. MOT_Rect rects;
  40. };
  41. typedef std::vector<MOT_Track> MOT_Result;
  42. // Generate visualization color
  43. cv::Scalar GetColor(int idx);
  44. // Visualiztion Detection Result
  45. cv::Mat VisualizeTrackResult(const cv::Mat& img,
  46. const MOT_Result& results,
  47. const float fps,
  48. const int frame_id);
  49. class JDEDetector {
  50. public:
  51. explicit JDEDetector(const std::string& model_dir,
  52. const std::string& device = "CPU",
  53. bool use_mkldnn = false,
  54. int cpu_threads = 1,
  55. const std::string& run_mode = "paddle",
  56. const int batch_size = 1,
  57. const int gpu_id = 0,
  58. const int trt_min_shape = 1,
  59. const int trt_max_shape = 1280,
  60. const int trt_opt_shape = 640,
  61. bool trt_calib_mode = false,
  62. const int min_box_area = 200) {
  63. this->device_ = device;
  64. this->gpu_id_ = gpu_id;
  65. this->cpu_math_library_num_threads_ = cpu_threads;
  66. this->use_mkldnn_ = use_mkldnn;
  67. this->trt_min_shape_ = trt_min_shape;
  68. this->trt_max_shape_ = trt_max_shape;
  69. this->trt_opt_shape_ = trt_opt_shape;
  70. this->trt_calib_mode_ = trt_calib_mode;
  71. config_.load_config(model_dir);
  72. this->use_dynamic_shape_ = config_.use_dynamic_shape_;
  73. this->min_subgraph_size_ = config_.min_subgraph_size_;
  74. threshold_ = config_.draw_threshold_;
  75. preprocessor_.Init(config_.preprocess_info_);
  76. LoadModel(model_dir, batch_size, run_mode);
  77. this->min_box_area_ = min_box_area;
  78. this->conf_thresh_ = config_.conf_thresh_;
  79. }
  80. // Load Paddle inference model
  81. void LoadModel(const std::string& model_dir,
  82. const int batch_size = 1,
  83. const std::string& run_mode = "paddle");
  84. // Run predictor
  85. void Predict(const std::vector<cv::Mat> imgs,
  86. const double threshold = 0.5,
  87. const int warmup = 0,
  88. const int repeats = 1,
  89. MOT_Result* result = nullptr,
  90. std::vector<double>* times = nullptr);
  91. private:
  92. std::string device_ = "CPU";
  93. int gpu_id_ = 0;
  94. int cpu_math_library_num_threads_ = 1;
  95. bool use_mkldnn_ = false;
  96. int min_subgraph_size_ = 3;
  97. bool use_dynamic_shape_ = false;
  98. int trt_min_shape_ = 1;
  99. int trt_max_shape_ = 1280;
  100. int trt_opt_shape_ = 640;
  101. bool trt_calib_mode_ = false;
  102. // Preprocess image and copy data to input buffer
  103. void Preprocess(const cv::Mat& image_mat);
  104. // Postprocess result
  105. void Postprocess(const cv::Mat dets, const cv::Mat emb, MOT_Result* result);
  106. std::shared_ptr<Predictor> predictor_;
  107. Preprocessor preprocessor_;
  108. ImageBlob inputs_;
  109. std::vector<float> bbox_data_;
  110. std::vector<float> emb_data_;
  111. float threshold_;
  112. ConfigPaser config_;
  113. float min_box_area_;
  114. float conf_thresh_;
  115. };
  116. } // namespace PaddleDetection