pptinypose_utils.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright (c) 2022 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. #include "ultra_infer/vision/keypointdet/pptinypose/pptinypose_utils.h"
  15. #define PI 3.1415926535
  16. #define HALF_CIRCLE_DEGREE 180
  17. namespace ultra_infer {
  18. namespace vision {
  19. namespace keypointdetection {
  20. cv::Point2f Get3dPoint(const cv::Point2f &a, const cv::Point2f &b) {
  21. cv::Point2f direct{a.x - b.x, a.y - b.y};
  22. return cv::Point2f(a.x - direct.y, a.y + direct.x);
  23. }
  24. std::vector<float> GetDir(const float src_point_x, const float src_point_y,
  25. const float rot_rad) {
  26. float sn = sin(rot_rad);
  27. float cs = cos(rot_rad);
  28. std::vector<float> src_result{0.0, 0.0};
  29. src_result[0] = src_point_x * cs - src_point_y * sn;
  30. src_result[1] = src_point_x * sn + src_point_y * cs;
  31. return src_result;
  32. }
  33. void AffineTransform(const float pt_x, const float pt_y, const cv::Mat &trans,
  34. std::vector<float> *preds, const int p) {
  35. double new1[3] = {pt_x, pt_y, 1.0};
  36. cv::Mat new_pt(3, 1, trans.type(), new1);
  37. cv::Mat w = trans * new_pt;
  38. (*preds)[p * 3 + 1] = static_cast<float>(w.at<double>(0, 0));
  39. (*preds)[p * 3 + 2] = static_cast<float>(w.at<double>(1, 0));
  40. }
  41. void GetAffineTransform(const std::vector<float> &center,
  42. const std::vector<float> &scale, const float rot,
  43. const std::vector<int> &output_size, cv::Mat *trans,
  44. const int inv) {
  45. float src_w = scale[0];
  46. float dst_w = static_cast<float>(output_size[0]);
  47. float dst_h = static_cast<float>(output_size[1]);
  48. float rot_rad = rot * PI / HALF_CIRCLE_DEGREE;
  49. std::vector<float> src_dir = GetDir(-0.5 * src_w, 0, rot_rad);
  50. std::vector<float> dst_dir{-0.5f * dst_w, 0.0};
  51. cv::Point2f srcPoint2f[3], dstPoint2f[3];
  52. srcPoint2f[0] = cv::Point2f(center[0], center[1]);
  53. srcPoint2f[1] = cv::Point2f(center[0] + src_dir[0], center[1] + src_dir[1]);
  54. srcPoint2f[2] = Get3dPoint(srcPoint2f[0], srcPoint2f[1]);
  55. dstPoint2f[0] = cv::Point2f(dst_w * 0.5, dst_h * 0.5);
  56. dstPoint2f[1] =
  57. cv::Point2f(dst_w * 0.5 + dst_dir[0], dst_h * 0.5 + dst_dir[1]);
  58. dstPoint2f[2] = Get3dPoint(dstPoint2f[0], dstPoint2f[1]);
  59. if (inv == 0) {
  60. (*trans) = cv::getAffineTransform(srcPoint2f, dstPoint2f);
  61. } else {
  62. (*trans) = cv::getAffineTransform(dstPoint2f, srcPoint2f);
  63. }
  64. }
  65. void TransformPreds(std::vector<float> &coords,
  66. const std::vector<float> &center,
  67. const std::vector<float> &scale,
  68. const std::vector<int> &output_size,
  69. const std::vector<int> &dim,
  70. std::vector<float> *target_coords) {
  71. cv::Mat trans(2, 3, CV_64FC1);
  72. GetAffineTransform(center, scale, 0, output_size, &trans, 1);
  73. for (int p = 0; p < dim[1]; ++p) {
  74. AffineTransform(coords[p * 2], coords[p * 2 + 1], trans, target_coords, p);
  75. }
  76. }
  77. void GetFinalPredictions(const std::vector<float> &heatmap,
  78. const std::vector<int> &dim,
  79. const std::vector<int64_t> &idxout,
  80. const std::vector<float> &center,
  81. const std::vector<float> scale,
  82. std::vector<float> *preds, const bool DARK) {
  83. std::vector<float> coords(dim[1] * 2);
  84. int heatmap_height = dim[2];
  85. int heatmap_width = dim[3];
  86. for (int j = 0; j < dim[1]; ++j) {
  87. int index = j * dim[2] * dim[3];
  88. int idx = idxout[j];
  89. (*preds)[j * 3] = heatmap[index + idx];
  90. coords[j * 2] = idx % heatmap_width;
  91. coords[j * 2 + 1] = idx / heatmap_width;
  92. int px = int(coords[j * 2] + 0.5);
  93. int py = int(coords[j * 2 + 1] + 0.5);
  94. if (DARK && px > 1 && px < heatmap_width - 2) {
  95. utils::DarkParse(heatmap, dim, &coords, px, py, index, j);
  96. } else {
  97. if (px > 0 && px < heatmap_width - 1) {
  98. float diff_x = heatmap[index + py * dim[3] + px + 1] -
  99. heatmap[index + py * dim[3] + px - 1];
  100. coords[j * 2] += diff_x > 0 ? 1 : -1 * 0.25;
  101. }
  102. if (py > 0 && py < heatmap_height - 1) {
  103. float diff_y = heatmap[index + (py + 1) * dim[3] + px] -
  104. heatmap[index + (py - 1) * dim[3] + px];
  105. coords[j * 2 + 1] += diff_y > 0 ? 1 : -1 * 0.25;
  106. }
  107. }
  108. }
  109. std::vector<int> img_size{heatmap_width, heatmap_height};
  110. TransformPreds(coords, center, scale, img_size, dim, preds);
  111. }
  112. } // namespace keypointdetection
  113. } // namespace vision
  114. } // namespace ultra_infer