keypoint_detector.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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/keypoint_postprocess.h"
  26. #include "include/preprocess_op.h"
  27. using namespace paddle_infer;
  28. namespace PaddleDetection {
  29. // Visualiztion KeyPoint Result
  30. cv::Mat VisualizeKptsResult(const cv::Mat& img,
  31. const std::vector<KeyPointResult>& results,
  32. const std::vector<int>& colormap);
  33. class KeyPointDetector {
  34. public:
  35. explicit KeyPointDetector(const std::string& model_dir,
  36. const std::string& device = "CPU",
  37. bool use_mkldnn = false,
  38. int cpu_threads = 1,
  39. const std::string& run_mode = "paddle",
  40. const int batch_size = 1,
  41. const int gpu_id = 0,
  42. const int trt_min_shape = 1,
  43. const int trt_max_shape = 1280,
  44. const int trt_opt_shape = 640,
  45. bool trt_calib_mode = false,
  46. bool use_dark = true) {
  47. this->device_ = device;
  48. this->gpu_id_ = gpu_id;
  49. this->cpu_math_library_num_threads_ = cpu_threads;
  50. this->use_mkldnn_ = use_mkldnn;
  51. this->use_dark = use_dark;
  52. this->trt_min_shape_ = trt_min_shape;
  53. this->trt_max_shape_ = trt_max_shape;
  54. this->trt_opt_shape_ = trt_opt_shape;
  55. this->trt_calib_mode_ = trt_calib_mode;
  56. config_.load_config(model_dir);
  57. this->use_dynamic_shape_ = config_.use_dynamic_shape_;
  58. this->min_subgraph_size_ = config_.min_subgraph_size_;
  59. threshold_ = config_.draw_threshold_;
  60. preprocessor_.Init(config_.preprocess_info_);
  61. LoadModel(model_dir, batch_size, run_mode);
  62. }
  63. // Load Paddle inference model
  64. void LoadModel(const std::string& model_dir,
  65. const int batch_size = 1,
  66. const std::string& run_mode = "paddle");
  67. // Run predictor
  68. void Predict(const std::vector<cv::Mat> imgs,
  69. std::vector<std::vector<float>>& center,
  70. std::vector<std::vector<float>>& scale,
  71. const double threshold = 0.5,
  72. const int warmup = 0,
  73. const int repeats = 1,
  74. std::vector<KeyPointResult>* result = nullptr,
  75. std::vector<double>* times = nullptr);
  76. // Get Model Label list
  77. const std::vector<std::string>& GetLabelList() const {
  78. return config_.label_list_;
  79. }
  80. private:
  81. std::string device_ = "CPU";
  82. int gpu_id_ = 0;
  83. int cpu_math_library_num_threads_ = 1;
  84. bool use_dark = true;
  85. bool use_mkldnn_ = false;
  86. int min_subgraph_size_ = 3;
  87. bool use_dynamic_shape_ = false;
  88. int trt_min_shape_ = 1;
  89. int trt_max_shape_ = 1280;
  90. int trt_opt_shape_ = 640;
  91. bool trt_calib_mode_ = false;
  92. // Preprocess image and copy data to input buffer
  93. void Preprocess(const cv::Mat& image_mat);
  94. // Postprocess result
  95. void Postprocess(std::vector<float>& output,
  96. std::vector<int> output_shape,
  97. std::vector<int64_t>& idxout,
  98. std::vector<int> idx_shape,
  99. std::vector<KeyPointResult>* result,
  100. std::vector<std::vector<float>>& center,
  101. std::vector<std::vector<float>>& scale);
  102. std::shared_ptr<Predictor> predictor_;
  103. Preprocessor preprocessor_;
  104. ImageBlob inputs_;
  105. std::vector<float> output_data_;
  106. std::vector<int64_t> idx_data_;
  107. float threshold_;
  108. ConfigPaser config_;
  109. };
  110. } // namespace PaddleDetection