transforms.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 resize
  33. std::vector<std::vector<int>> im_size_before_resize_;
  34. // Reshape order
  35. std::vector<std::string> reshape_order_;
  36. // Resize scale
  37. float scale = 1.0;
  38. // Buffer for image data after preprocessing
  39. std::vector<float> im_data_;
  40. void clear() {
  41. im_size_before_resize_.clear();
  42. reshape_order_.clear();
  43. im_data_.clear();
  44. }
  45. };
  46. // Abstraction of preprocessing opration class
  47. class Transform {
  48. public:
  49. virtual void Init(const YAML::Node& item) = 0;
  50. virtual bool Run(cv::Mat* im, ImageBlob* data) = 0;
  51. };
  52. class Normalize : public Transform {
  53. public:
  54. virtual void Init(const YAML::Node& item) {
  55. mean_ = item["mean"].as<std::vector<float>>();
  56. std_ = item["std"].as<std::vector<float>>();
  57. }
  58. virtual bool Run(cv::Mat* im, ImageBlob* data);
  59. private:
  60. std::vector<float> mean_;
  61. std::vector<float> std_;
  62. };
  63. class ResizeByShort : public Transform {
  64. public:
  65. virtual void Init(const YAML::Node& item) {
  66. short_size_ = item["short_size"].as<int>();
  67. if (item["max_size"].IsDefined()) {
  68. max_size_ = item["max_size"].as<int>();
  69. } else {
  70. max_size_ = -1;
  71. }
  72. }
  73. virtual bool Run(cv::Mat* im, ImageBlob* data);
  74. private:
  75. float GenerateScale(const cv::Mat& im);
  76. int short_size_;
  77. int max_size_;
  78. };
  79. class ResizeByLong : public Transform {
  80. public:
  81. virtual void Init(const YAML::Node& item) {
  82. long_size_ = item["long_size"].as<int>();
  83. }
  84. virtual bool Run(cv::Mat* im, ImageBlob* data);
  85. private:
  86. int long_size_;
  87. };
  88. class Resize : public Transform {
  89. public:
  90. virtual void Init(const YAML::Node& item) {
  91. if (item["target_size"].IsScalar()) {
  92. height_ = item["target_size"].as<int>();
  93. width_ = item["target_size"].as<int>();
  94. interp_ = item["interp"].as<std::string>();
  95. } else if (item["target_size"].IsSequence()) {
  96. std::vector<int> target_size = item["target_size"].as<std::vector<int>>();
  97. width_ = target_size[0];
  98. height_ = target_size[1];
  99. }
  100. if (height_ <= 0 || width_ <= 0) {
  101. std::cerr << "[Resize] target_size should greater than 0" << std::endl;
  102. exit(-1);
  103. }
  104. }
  105. virtual bool Run(cv::Mat* im, ImageBlob* data);
  106. private:
  107. int height_;
  108. int width_;
  109. std::string interp_;
  110. };
  111. class CenterCrop : public Transform {
  112. public:
  113. virtual void Init(const YAML::Node& item) {
  114. if (item["crop_size"].IsScalar()) {
  115. height_ = item["crop_size"].as<int>();
  116. width_ = item["crop_size"].as<int>();
  117. } else if (item["crop_size"].IsSequence()) {
  118. std::vector<int> crop_size = item["crop_size"].as<std::vector<int>>();
  119. width_ = crop_size[0];
  120. height_ = crop_size[1];
  121. }
  122. }
  123. virtual bool Run(cv::Mat* im, ImageBlob* data);
  124. private:
  125. int height_;
  126. int width_;
  127. };
  128. class Padding : public Transform {
  129. public:
  130. virtual void Init(const YAML::Node& item) {
  131. if (item["coarsest_stride"].IsDefined()) {
  132. coarsest_stride_ = item["coarsest_stride"].as<int>();
  133. if (coarsest_stride_ < 1) {
  134. std::cerr << "[Padding] coarest_stride should greater than 0"
  135. << std::endl;
  136. exit(-1);
  137. }
  138. }
  139. if (item["target_size"].IsDefined()) {
  140. if (item["target_size"].IsScalar()) {
  141. width_ = item["target_size"].as<int>();
  142. height_ = item["target_size"].as<int>();
  143. } else if (item["target_size"].IsSequence()) {
  144. width_ = item["target_size"].as<std::vector<int>>()[0];
  145. height_ = item["target_size"].as<std::vector<int>>()[1];
  146. }
  147. }
  148. }
  149. virtual bool Run(cv::Mat* im, ImageBlob* data);
  150. private:
  151. int coarsest_stride_ = -1;
  152. int width_ = 0;
  153. int height_ = 0;
  154. };
  155. class Transforms {
  156. public:
  157. void Init(const YAML::Node& node, bool to_rgb = true);
  158. std::shared_ptr<Transform> CreateTransform(const std::string& name);
  159. bool Run(cv::Mat* im, ImageBlob* data);
  160. private:
  161. std::vector<std::shared_ptr<Transform>> transforms_;
  162. bool to_rgb_ = true;
  163. };
  164. } // namespace PaddleX