preprocess_op.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. #include <string>
  15. #include <thread>
  16. #include <vector>
  17. #include "include/preprocess_op.h"
  18. namespace PaddleDetection {
  19. void InitInfo::Run(cv::Mat* im, ImageBlob* data) {
  20. data->im_shape_ = {static_cast<float>(im->rows),
  21. static_cast<float>(im->cols)};
  22. data->scale_factor_ = {1., 1.};
  23. data->in_net_shape_ = {static_cast<float>(im->rows),
  24. static_cast<float>(im->cols)};
  25. }
  26. void NormalizeImage::Run(cv::Mat* im, ImageBlob* data) {
  27. double e = 1.0;
  28. if (is_scale_) {
  29. e *= 1./255.0;
  30. }
  31. (*im).convertTo(*im, CV_32FC3, e);
  32. for (int h = 0; h < im->rows; h++) {
  33. for (int w = 0; w < im->cols; w++) {
  34. im->at<cv::Vec3f>(h, w)[0] =
  35. (im->at<cv::Vec3f>(h, w)[0] - mean_[0]) / scale_[0];
  36. im->at<cv::Vec3f>(h, w)[1] =
  37. (im->at<cv::Vec3f>(h, w)[1] - mean_[1]) / scale_[1];
  38. im->at<cv::Vec3f>(h, w)[2] =
  39. (im->at<cv::Vec3f>(h, w)[2] - mean_[2]) / scale_[2];
  40. }
  41. }
  42. }
  43. void Permute::Run(cv::Mat* im, ImageBlob* data) {
  44. (*im).convertTo(*im, CV_32FC3);
  45. int rh = im->rows;
  46. int rw = im->cols;
  47. int rc = im->channels();
  48. (data->im_data_).resize(rc * rh * rw);
  49. float* base = (data->im_data_).data();
  50. for (int i = 0; i < rc; ++i) {
  51. cv::extractChannel(*im, cv::Mat(rh, rw, CV_32FC1, base + i * rh * rw), i);
  52. }
  53. }
  54. void Resize::Run(cv::Mat* im, ImageBlob* data) {
  55. auto resize_scale = GenerateScale(*im);
  56. data->im_shape_ = {static_cast<float>(im->cols * resize_scale.first),
  57. static_cast<float>(im->rows * resize_scale.second)};
  58. data->in_net_shape_ = {static_cast<float>(im->cols * resize_scale.first),
  59. static_cast<float>(im->rows * resize_scale.second)};
  60. cv::resize(
  61. *im, *im, cv::Size(), resize_scale.first, resize_scale.second, interp_);
  62. data->im_shape_ = {
  63. static_cast<float>(im->rows), static_cast<float>(im->cols),
  64. };
  65. data->scale_factor_ = {
  66. resize_scale.second, resize_scale.first,
  67. };
  68. }
  69. std::pair<float, float> Resize::GenerateScale(const cv::Mat& im) {
  70. std::pair<float, float> resize_scale;
  71. int origin_w = im.cols;
  72. int origin_h = im.rows;
  73. if (keep_ratio_) {
  74. int im_size_max = std::max(origin_w, origin_h);
  75. int im_size_min = std::min(origin_w, origin_h);
  76. int target_size_max =
  77. *std::max_element(target_size_.begin(), target_size_.end());
  78. int target_size_min =
  79. *std::min_element(target_size_.begin(), target_size_.end());
  80. float scale_min =
  81. static_cast<float>(target_size_min) / static_cast<float>(im_size_min);
  82. float scale_max =
  83. static_cast<float>(target_size_max) / static_cast<float>(im_size_max);
  84. float scale_ratio = std::min(scale_min, scale_max);
  85. resize_scale = {scale_ratio, scale_ratio};
  86. } else {
  87. resize_scale.first =
  88. static_cast<float>(target_size_[1]) / static_cast<float>(origin_w);
  89. resize_scale.second =
  90. static_cast<float>(target_size_[0]) / static_cast<float>(origin_h);
  91. }
  92. return resize_scale;
  93. }
  94. void PadStride::Run(cv::Mat* im, ImageBlob* data) {
  95. if (stride_ <= 0) {
  96. return;
  97. }
  98. int rc = im->channels();
  99. int rh = im->rows;
  100. int rw = im->cols;
  101. int nh = (rh / stride_) * stride_ + (rh % stride_ != 0) * stride_;
  102. int nw = (rw / stride_) * stride_ + (rw % stride_ != 0) * stride_;
  103. cv::copyMakeBorder(
  104. *im, *im, 0, nh - rh, 0, nw - rw, cv::BORDER_CONSTANT, cv::Scalar(0));
  105. data->in_net_shape_ = {
  106. static_cast<float>(im->rows), static_cast<float>(im->cols),
  107. };
  108. }
  109. void TopDownEvalAffine::Run(cv::Mat* im, ImageBlob* data) {
  110. cv::resize(*im, *im, cv::Size(trainsize_[0], trainsize_[1]), 0, 0, interp_);
  111. // todo: Simd::ResizeBilinear();
  112. data->in_net_shape_ = {
  113. static_cast<float>(trainsize_[1]), static_cast<float>(trainsize_[0]),
  114. };
  115. }
  116. // Preprocessor op running order
  117. const std::vector<std::string> Preprocessor::RUN_ORDER = {"InitInfo",
  118. "TopDownEvalAffine",
  119. "Resize",
  120. "NormalizeImage",
  121. "PadStride",
  122. "Permute"};
  123. void Preprocessor::Run(cv::Mat* im, ImageBlob* data) {
  124. for (const auto& name : RUN_ORDER) {
  125. if (ops_.find(name) != ops_.end()) {
  126. ops_[name]->Run(im, data);
  127. }
  128. }
  129. }
  130. void CropImg(cv::Mat& img,
  131. cv::Mat& crop_img,
  132. std::vector<int>& area,
  133. std::vector<float>& center,
  134. std::vector<float>& scale,
  135. float expandratio) {
  136. int crop_x1 = std::max(0, area[0]);
  137. int crop_y1 = std::max(0, area[1]);
  138. int crop_x2 = std::min(img.cols - 1, area[2]);
  139. int crop_y2 = std::min(img.rows - 1, area[3]);
  140. int center_x = (crop_x1 + crop_x2) / 2.;
  141. int center_y = (crop_y1 + crop_y2) / 2.;
  142. int half_h = (crop_y2 - crop_y1) / 2.;
  143. int half_w = (crop_x2 - crop_x1) / 2.;
  144. if (half_h * 3 > half_w * 4) {
  145. half_w = static_cast<int>(half_h * 0.75);
  146. } else {
  147. half_h = static_cast<int>(half_w * 4 / 3);
  148. }
  149. crop_x1 =
  150. std::max(0, center_x - static_cast<int>(half_w * (1 + expandratio)));
  151. crop_y1 =
  152. std::max(0, center_y - static_cast<int>(half_h * (1 + expandratio)));
  153. crop_x2 = std::min(img.cols - 1,
  154. static_cast<int>(center_x + half_w * (1 + expandratio)));
  155. crop_y2 = std::min(img.rows - 1,
  156. static_cast<int>(center_y + half_h * (1 + expandratio)));
  157. crop_img =
  158. img(cv::Range(crop_y1, crop_y2 + 1), cv::Range(crop_x1, crop_x2 + 1));
  159. center.clear();
  160. center.emplace_back((crop_x1 + crop_x2) / 2);
  161. center.emplace_back((crop_y1 + crop_y2) / 2);
  162. scale.clear();
  163. scale.emplace_back((crop_x2 - crop_x1));
  164. scale.emplace_back((crop_y2 - crop_y1));
  165. }
  166. } // namespace PaddleDetection