transforms.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. namespace PaddleX {
  25. // Object for storing all preprocessed data
  26. class ImageBlob {
  27. public:
  28. // Original image height and width
  29. std::vector<int> ori_im_size_ = std::vector<int>(2);
  30. // Newest image height and width after process
  31. std::vector<int> new_im_size_ = std::vector<int>(2);
  32. // Image height and width before padding
  33. std::vector<int> im_size_before_padding_ = std::vector<int>(2);
  34. // Image height and width before resize
  35. std::vector<int> im_size_before_resize_ = std::vector<int>(2);
  36. // Reshape order
  37. std::vector<std::string> reshape_order_;
  38. // Resize scale
  39. float scale = 1.0;
  40. // Buffer for image data after preprocessing
  41. std::vector<float> im_data_;
  42. void clear() {
  43. ori_im_size_.clear();
  44. new_im_size_.clear();
  45. im_size_before_padding_.clear();
  46. im_size_before_resize_.clear();
  47. reshape_order_.clear();
  48. im_data_.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. class ResizeByLong : public Transform {
  85. public:
  86. virtual void Init(const YAML::Node& item) {
  87. long_size_ = item["long_size"].as<int>();
  88. };
  89. virtual bool Run(cv::Mat* im, ImageBlob* data);
  90. private:
  91. int long_size_;
  92. };
  93. class Resize : public Transform {
  94. public:
  95. virtual void Init(const YAML::Node& item) {
  96. if (item["target_size"].IsScalar()) {
  97. height_ = item["target_size"].as<int>();
  98. width_ = item["target_size"].as<int>();
  99. interp_ = item["interp"].as<std::string>();
  100. } else if (item["target_size"].IsSequence()) {
  101. std::vector<int> target_size = item["target_size"].as<std::vector<int>>();
  102. width_ = target_size[0];
  103. height_ = target_size[1];
  104. }
  105. if (height_ <= 0 || width_ <= 0) {
  106. std::cerr << "[Resize] target_size should greater than 0" << std::endl;
  107. exit(-1);
  108. }
  109. }
  110. virtual bool Run(cv::Mat* im, ImageBlob* data);
  111. private:
  112. int height_;
  113. int width_;
  114. std::string interp_;
  115. };
  116. class CenterCrop : public Transform {
  117. public:
  118. virtual void Init(const YAML::Node& item) {
  119. if (item["crop_size"].IsScalar()) {
  120. height_ = item["crop_size"].as<int>();
  121. width_ = item["crop_size"].as<int>();
  122. } else if (item["crop_size"].IsSequence()) {
  123. std::vector<int> crop_size = item["crop_size"].as<std::vector<int>>();
  124. width_ = crop_size[0];
  125. height_ = crop_size[1];
  126. }
  127. }
  128. virtual bool Run(cv::Mat* im, ImageBlob* data);
  129. private:
  130. int height_;
  131. int width_;
  132. };
  133. class Padding : public Transform {
  134. public:
  135. virtual void Init(const YAML::Node& item) {
  136. if (item["coarsest_stride"].IsDefined()) {
  137. coarsest_stride_ = item["coarsest_stride"].as<int>();
  138. if (coarsest_stride_ <= 1) {
  139. std::cerr << "[Padding] coarest_stride should greater than 0"
  140. << std::endl;
  141. exit(-1);
  142. }
  143. } else {
  144. if (item["target_size"].IsScalar()) {
  145. width_ = item["target_size"].as<int>();
  146. height_ = item["target_size"].as<int>();
  147. } else if (item["target_size"].IsSequence()) {
  148. width_ = item["target_size"].as<std::vector<int>>()[0];
  149. height_ = item["target_size"].as<std::vector<int>>()[1];
  150. }
  151. }
  152. if (item["im_padding_value"].IsDefined()) {
  153. value_ = item["im_padding_value"].as<std::vector<float>>();
  154. }
  155. }
  156. virtual bool Run(cv::Mat* im, ImageBlob* data);
  157. private:
  158. int coarsest_stride_ = -1;
  159. int width_ = 0;
  160. int height_ = 0;
  161. std::vector<float> value_;
  162. };
  163. class Transforms {
  164. public:
  165. void Init(const YAML::Node& node, bool to_rgb = true);
  166. std::shared_ptr<Transform> CreateTransform(const std::string& name);
  167. bool Run(cv::Mat* im, ImageBlob* data);
  168. private:
  169. std::vector<std::shared_ptr<Transform>> transforms_;
  170. bool to_rgb_ = true;
  171. };
  172. } // namespace PaddleX