transforms.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // Copyright (c) 2020 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 <yaml-cpp/yaml.h>
  16. #include <memory>
  17. #include <string>
  18. #include <unordered_map>
  19. #include <utility>
  20. #include <vector>
  21. #include <opencv2/core/core.hpp>
  22. #include <opencv2/highgui/highgui.hpp>
  23. #include <opencv2/imgproc/imgproc.hpp>
  24. #include <inference_engine.hpp>
  25. using namespace InferenceEngine;
  26. namespace PaddleX {
  27. /*
  28. * @brief
  29. * This class represents object for storing all preprocessed data
  30. * */
  31. class ImageBlob {
  32. public:
  33. // Original image height and width
  34. //std::vector<int> ori_im_size_ = std::vector<int>(2);
  35. Blob::Ptr ori_im_size_;
  36. // Newest image height and width after process
  37. std::vector<int> new_im_size_ = std::vector<int>(2);
  38. // Image height and width before resize
  39. std::vector<std::vector<int>> im_size_before_resize_;
  40. // Reshape order
  41. std::vector<std::string> reshape_order_;
  42. // Resize scale
  43. float scale = 1.0;
  44. // Buffer for image data after preprocessing
  45. Blob::Ptr blob;
  46. void clear() {
  47. im_size_before_resize_.clear();
  48. reshape_order_.clear();
  49. }
  50. };
  51. // Abstraction of preprocessing opration class
  52. class Transform {
  53. public:
  54. virtual void Init(const YAML::Node& item) = 0;
  55. virtual bool Run(cv::Mat* im, ImageBlob* data) = 0;
  56. };
  57. class Normalize : public Transform {
  58. public:
  59. virtual void Init(const YAML::Node& item) {
  60. mean_ = item["mean"].as<std::vector<float>>();
  61. std_ = item["std"].as<std::vector<float>>();
  62. }
  63. virtual bool Run(cv::Mat* im, ImageBlob* data);
  64. private:
  65. std::vector<float> mean_;
  66. std::vector<float> std_;
  67. };
  68. class ResizeByShort : public Transform {
  69. public:
  70. virtual void Init(const YAML::Node& item) {
  71. short_size_ = item["short_size"].as<int>();
  72. if (item["max_size"].IsDefined()) {
  73. max_size_ = item["max_size"].as<int>();
  74. } else {
  75. max_size_ = -1;
  76. }
  77. };
  78. virtual bool Run(cv::Mat* im, ImageBlob* data);
  79. private:
  80. float GenerateScale(const cv::Mat& im);
  81. int short_size_;
  82. int max_size_;
  83. };
  84. /*
  85. * @brief
  86. * This class execute resize by long operation on image matrix. At first, it resizes
  87. * the long side of image matrix to specified length. Accordingly, the short side
  88. * will be resized in the same proportion.
  89. * */
  90. class ResizeByLong : public Transform {
  91. public:
  92. virtual void Init(const YAML::Node& item) {
  93. long_size_ = item["long_size"].as<int>();
  94. }
  95. virtual bool Run(cv::Mat* im, ImageBlob* data);
  96. private:
  97. int long_size_;
  98. };
  99. /*
  100. * @brief
  101. * This class execute resize operation on image matrix. It resizes width and height
  102. * to specified length.
  103. * */
  104. class Resize : public Transform {
  105. public:
  106. virtual void Init(const YAML::Node& item) {
  107. if (item["interp"].IsDefined()) {
  108. interp_ = item["interp"].as<std::string>();
  109. }
  110. if (item["target_size"].IsScalar()) {
  111. height_ = item["target_size"].as<int>();
  112. width_ = item["target_size"].as<int>();
  113. } else if (item["target_size"].IsSequence()) {
  114. std::vector<int> target_size = item["target_size"].as<std::vector<int>>();
  115. width_ = target_size[0];
  116. height_ = target_size[1];
  117. }
  118. if (height_ <= 0 || width_ <= 0) {
  119. std::cerr << "[Resize] target_size should greater than 0" << std::endl;
  120. exit(-1);
  121. }
  122. }
  123. virtual bool Run(cv::Mat* im, ImageBlob* data);
  124. private:
  125. int height_;
  126. int width_;
  127. std::string interp_;
  128. };
  129. class CenterCrop : public Transform {
  130. public:
  131. virtual void Init(const YAML::Node& item) {
  132. if (item["crop_size"].IsScalar()) {
  133. height_ = item["crop_size"].as<int>();
  134. width_ = item["crop_size"].as<int>();
  135. } else if (item["crop_size"].IsSequence()) {
  136. std::vector<int> crop_size = item["crop_size"].as<std::vector<int>>();
  137. width_ = crop_size[0];
  138. height_ = crop_size[1];
  139. }
  140. }
  141. virtual bool Run(cv::Mat* im, ImageBlob* data);
  142. private:
  143. int height_;
  144. int width_;
  145. };
  146. /*
  147. * @brief
  148. * This class execute padding operation on image matrix. It makes border on edge
  149. * of image matrix.
  150. * */
  151. class Padding : public Transform {
  152. public:
  153. virtual void Init(const YAML::Node& item) {
  154. if (item["coarsest_stride"].IsDefined()) {
  155. coarsest_stride_ = item["coarsest_stride"].as<int>();
  156. if (coarsest_stride_ < 1) {
  157. std::cerr << "[Padding] coarest_stride should greater than 0"
  158. << std::endl;
  159. exit(-1);
  160. }
  161. }
  162. if (item["target_size"].IsDefined()) {
  163. if (item["target_size"].IsScalar()) {
  164. width_ = item["target_size"].as<int>();
  165. height_ = item["target_size"].as<int>();
  166. } else if (item["target_size"].IsSequence()) {
  167. width_ = item["target_size"].as<std::vector<int>>()[0];
  168. height_ = item["target_size"].as<std::vector<int>>()[1];
  169. }
  170. }
  171. if (item["im_padding_value"].IsDefined()) {
  172. im_value_ = item["im_padding_value"].as<std::vector<float>>();
  173. }
  174. else {
  175. im_value_ = {0, 0, 0};
  176. }
  177. }
  178. virtual bool Run(cv::Mat* im, ImageBlob* data);
  179. private:
  180. int coarsest_stride_ = -1;
  181. int width_ = 0;
  182. int height_ = 0;
  183. std::vector<float> im_value_;
  184. };
  185. class Transforms {
  186. public:
  187. void Init(const YAML::Node& node, std::string type, bool to_rgb = true);
  188. std::shared_ptr<Transform> CreateTransform(const std::string& name);
  189. bool Run(cv::Mat* im, ImageBlob* data);
  190. private:
  191. std::vector<std::shared_ptr<Transform>> transforms_;
  192. bool to_rgb_ = true;
  193. std::string type_;
  194. };
  195. } // namespace PaddleX