keypoint_postprocess.h 4.6 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 <math.h>
  16. #include <opencv2/core/core.hpp>
  17. #include <opencv2/highgui/highgui.hpp>
  18. #include <opencv2/imgproc/imgproc.hpp>
  19. #include <vector>
  20. namespace PaddleDetection {
  21. std::vector<float> get_3rd_point(std::vector<float>& a, std::vector<float>& b);
  22. std::vector<float> get_dir(float src_point_x, float src_point_y, float rot_rad);
  23. void affine_tranform(
  24. float pt_x, float pt_y, cv::Mat& trans, std::vector<float>& preds, int p);
  25. cv::Mat get_affine_transform(std::vector<float>& center,
  26. std::vector<float>& scale,
  27. float rot,
  28. std::vector<int>& output_size,
  29. int inv);
  30. void transform_preds(std::vector<float>& coords,
  31. std::vector<float>& center,
  32. std::vector<float>& scale,
  33. std::vector<int>& output_size,
  34. std::vector<int>& dim,
  35. std::vector<float>& target_coords,
  36. bool affine = false);
  37. void box_to_center_scale(std::vector<int>& box,
  38. int width,
  39. int height,
  40. std::vector<float>& center,
  41. std::vector<float>& scale);
  42. void get_max_preds(float* heatmap,
  43. std::vector<int>& dim,
  44. std::vector<float>& preds,
  45. float* maxvals,
  46. int batchid,
  47. int joint_idx);
  48. void get_final_preds(std::vector<float>& heatmap,
  49. std::vector<int>& dim,
  50. std::vector<int64_t>& idxout,
  51. std::vector<int>& idxdim,
  52. std::vector<float>& center,
  53. std::vector<float> scale,
  54. std::vector<float>& preds,
  55. int batchid,
  56. bool DARK = true);
  57. // Object KeyPoint Result
  58. struct KeyPointResult {
  59. // Keypoints: shape(N x 3); N: number of Joints; 3: x,y,conf
  60. std::vector<float> keypoints;
  61. int num_joints = -1;
  62. };
  63. class PoseSmooth {
  64. public:
  65. explicit PoseSmooth(const int width,
  66. const int height,
  67. std::string filter_type = "OneEuro",
  68. float alpha = 0.5,
  69. float fc_d = 0.1,
  70. float fc_min = 0.1,
  71. float beta = 0.1,
  72. float thres_mult = 0.3)
  73. : width(width),
  74. height(height),
  75. alpha(alpha),
  76. fc_d(fc_d),
  77. fc_min(fc_min),
  78. beta(beta),
  79. filter_type(filter_type),
  80. thres_mult(thres_mult){};
  81. // Run predictor
  82. KeyPointResult smooth_process(KeyPointResult* result);
  83. void PointSmooth(KeyPointResult* result,
  84. KeyPointResult* keypoint_smoothed,
  85. std::vector<float> thresholds,
  86. int index);
  87. float OneEuroFilter(float x_cur, float x_pre, int loc);
  88. float smoothing_factor(float te, float fc);
  89. float ExpSmoothing(float x_cur, float x_pre, int loc = 0);
  90. private:
  91. int width = 0;
  92. int height = 0;
  93. float alpha = 0.;
  94. float fc_d = 1.;
  95. float fc_min = 0.;
  96. float beta = 1.;
  97. float thres_mult = 1.;
  98. std::string filter_type = "OneEuro";
  99. std::vector<float> thresholds = {0.005,
  100. 0.005,
  101. 0.005,
  102. 0.005,
  103. 0.005,
  104. 0.01,
  105. 0.01,
  106. 0.01,
  107. 0.01,
  108. 0.01,
  109. 0.01,
  110. 0.01,
  111. 0.01,
  112. 0.01,
  113. 0.01,
  114. 0.01,
  115. 0.01};
  116. KeyPointResult x_prev_hat;
  117. KeyPointResult dx_prev_hat;
  118. };
  119. } // namespace PaddleDetection