utils.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #pragma once
  15. #include <opencv2/opencv.hpp>
  16. #include <set>
  17. #include <vector>
  18. #include "ultra_infer/core/fd_tensor.h"
  19. #include "ultra_infer/utils/utils.h"
  20. #include "ultra_infer/vision/common/result.h"
  21. // #include "unsupported/Eigen/CXX11/Tensor"
  22. #include "ultra_infer/function/reduce.h"
  23. #include "ultra_infer/function/softmax.h"
  24. #include "ultra_infer/function/transpose.h"
  25. #include "ultra_infer/vision/common/processors/mat.h"
  26. namespace ultra_infer {
  27. namespace vision {
  28. namespace utils {
  29. // topk sometimes is a very small value
  30. // so this implementation is simple but I don't think it will
  31. // cost too much time
  32. // Also there may be cause problem since we suppose the minimum value is
  33. // -99999999
  34. // Do not use this function on array which topk contains value less than
  35. // -99999999
  36. template <typename T>
  37. std::vector<int32_t> TopKIndices(const T *array, int array_size, int topk) {
  38. topk = std::min(array_size, topk);
  39. std::vector<int32_t> res(topk);
  40. std::set<int32_t> searched;
  41. for (int32_t i = 0; i < topk; ++i) {
  42. T min = static_cast<T>(-99999999);
  43. for (int32_t j = 0; j < array_size; ++j) {
  44. if (searched.find(j) != searched.end()) {
  45. continue;
  46. }
  47. if (*(array + j) > min) {
  48. res[i] = j;
  49. min = *(array + j);
  50. }
  51. }
  52. searched.insert(res[i]);
  53. }
  54. return res;
  55. }
  56. void NMS(DetectionResult *output, float iou_threshold = 0.5,
  57. std::vector<int> *index = nullptr);
  58. void NMS(FaceDetectionResult *result, float iou_threshold = 0.5);
  59. /// Sort DetectionResult/FaceDetectionResult by score
  60. ULTRAINFER_DECL void SortDetectionResult(DetectionResult *result);
  61. ULTRAINFER_DECL void SortDetectionResult(FaceDetectionResult *result);
  62. /// Lex Sort DetectionResult by x(w) & y(h) axis
  63. ULTRAINFER_DECL void LexSortDetectionResultByXY(DetectionResult *result);
  64. /// Lex Sort OCRDet Result by x(w) & y(h) axis
  65. ULTRAINFER_DECL void
  66. LexSortOCRDetResultByXY(std::vector<std::array<int, 8>> *result);
  67. /// L2 Norm / cosine similarity (for face recognition, ...)
  68. ULTRAINFER_DECL std::vector<float>
  69. L2Normalize(const std::vector<float> &values);
  70. ULTRAINFER_DECL float CosineSimilarity(const std::vector<float> &a,
  71. const std::vector<float> &b,
  72. bool normalized = true);
  73. /** \brief Do face align for model with five points.
  74. *
  75. * \param[in] image The original image
  76. * \param[in] result FaceDetectionResult
  77. * \param[in] std_landmarks Standard face template
  78. * \param[in] output_size The size of output mat
  79. */
  80. ULTRAINFER_DECL std::vector<cv::Mat> AlignFaceWithFivePoints(
  81. cv::Mat &image, FaceDetectionResult &result,
  82. std::vector<std::array<float, 2>> std_landmarks = {{38.2946f, 51.6963f},
  83. {73.5318f, 51.5014f},
  84. {56.0252f, 71.7366f},
  85. {41.5493f, 92.3655f},
  86. {70.7299f, 92.2041f}},
  87. std::array<int, 2> output_size = {112, 112});
  88. bool CropImageByBox(Mat &src_im, Mat *dst_im, const std::vector<float> &box,
  89. std::vector<float> *center, std::vector<float> *scale,
  90. const float expandratio = 0.3);
  91. /**
  92. * Function: for keypoint detection model, fine positioning of keypoints in
  93. * postprocess
  94. * Parameters:
  95. * heatmap: model inference results for keypoint detection models
  96. * dim: shape information of the inference result
  97. * coords: coordinates after refined positioning
  98. * px: px = int(coords[ch * 2] + 0.5) , refer to API
  99. * detection::GetFinalPredictions py: px = int(coords[ch * 2 + 1] + 0.5), refer
  100. * to API detection::GetFinalPredictions index: index information of heatmap
  101. * pixels ch: channel Paper reference: DARK postprocessing, Zhang et al.
  102. * Distribution-Aware Coordinate Representation for Human Pose Estimation (CVPR
  103. * 2020).
  104. */
  105. void DarkParse(const std::vector<float> &heatmap, const std::vector<int> &dim,
  106. std::vector<float> *coords, const int px, const int py,
  107. const int index, const int ch);
  108. } // namespace utils
  109. } // namespace vision
  110. } // namespace ultra_infer