keypoint_detector.h 4.5 KB

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