transforms.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. #pragma once
  15. #include <iostream>
  16. #include <string>
  17. #include <vector>
  18. #include <opencv2/core/core.hpp>
  19. #include <opencv2/highgui/highgui.hpp>
  20. #include <opencv2/imgproc/imgproc.hpp>
  21. #include "yaml-cpp/yaml.h"
  22. #include "model_deploy/common/include/output_struct.h"
  23. namespace PaddleDeploy {
  24. class Transform {
  25. public:
  26. virtual void Init(const YAML::Node& item) = 0;
  27. virtual bool ShapeInfer(const std::vector<int>& in_shape,
  28. std::vector<int>* out_shape) = 0;
  29. virtual std::string Name() = 0;
  30. virtual bool Run(cv::Mat* im) = 0;
  31. };
  32. class Normalize : public Transform {
  33. public:
  34. virtual void Init(const YAML::Node& item) {
  35. mean_ = item["mean"].as<std::vector<float>>();
  36. std_ = item["std"].as<std::vector<float>>();
  37. if (item["is_scale"].IsDefined()) {
  38. is_scale_ = item["is_scale"];
  39. } else {
  40. is_scale_ = true;
  41. }
  42. if (item["min_val"].IsDefined()) {
  43. min_val_ = item["min_val"].as<std::vector<float>>();
  44. } else {
  45. min_val_ = std::vector<float>(mean_.size(), 0.);
  46. }
  47. if (item["max_val"].IsDefined()) {
  48. max_val_ = item["max_val"].as<std::vector<float>>();
  49. } else {
  50. max_val_ = std::vector<float>(mean_.size(), 255.);
  51. }
  52. }
  53. virtual bool Run(cv::Mat* im);
  54. virtual bool ShapeInfer(const std::vector<int>& in_shape,
  55. std::vector<int>* out_shape);
  56. virtual std::string Name() { return "Normalize"; }
  57. private:
  58. bool is_scale_;
  59. std::vector<float> mean_;
  60. std::vector<float> std_;
  61. std::vector<float> min_val_;
  62. std::vector<float> max_val_;
  63. };
  64. class ResizeByShort : public Transform {
  65. public:
  66. virtual void Init(const YAML::Node& item) {
  67. target_size_ = item["target_size"].as<int>();
  68. if (item["interp"].IsDefined()) {
  69. interp_ = item["interp"].as<int>();
  70. } else {
  71. interp_ = 1;
  72. }
  73. if (item["use_scale"].IsDefined()) {
  74. use_scale_ = item["use_scale"].as<bool>();
  75. } else {
  76. use_scale_ = true;
  77. }
  78. if (item["max_size"].IsDefined()) {
  79. max_size_ = item["max_size"].as<int>();
  80. } else {
  81. max_size_ = -1;
  82. }
  83. }
  84. virtual bool Run(cv::Mat* im);
  85. virtual bool ShapeInfer(const std::vector<int>& in_shape,
  86. std::vector<int>* out_shape);
  87. virtual std::string Name() { return "ResizeByShort"; }
  88. private:
  89. double GenerateScale(const int origin_w, const int origin_h);
  90. int target_size_;
  91. int max_size_;
  92. int interp_;
  93. bool use_scale_;
  94. };
  95. class ResizeByLong : public Transform {
  96. public:
  97. virtual void Init(const YAML::Node& item) {
  98. target_size_ = item["target_size"].as<int>();
  99. if (item["interp"].IsDefined()) {
  100. interp_ = item["interp"].as<int>();
  101. } else {
  102. interp_ = 1;
  103. }
  104. if (item["max_size"].IsDefined()) {
  105. max_size_ = item["max_size"].as<int>();
  106. } else {
  107. max_size_ = -1;
  108. }
  109. if (item["stride"].IsDefined()) {
  110. stride_ = item["stride"].as<int>();
  111. } else {
  112. stride_ = 0;
  113. }
  114. }
  115. virtual bool Run(cv::Mat* im);
  116. virtual bool ShapeInfer(const std::vector<int>& in_shape,
  117. std::vector<int>* out_shape);
  118. virtual std::string Name() { return "ResizeByLong"; }
  119. private:
  120. double GenerateScale(const int origin_w, const int origin_h);
  121. int target_size_;
  122. int max_size_;
  123. int interp_;
  124. int stride_;
  125. };
  126. class Resize : public Transform {
  127. public:
  128. virtual void Init(const YAML::Node& item) {
  129. if (item["interp"].IsDefined()) {
  130. interp_ = item["interp"].as<int>();
  131. } else {
  132. interp_ = 1;
  133. }
  134. if (item["use_scale"].IsDefined()) {
  135. use_scale_ = item["use_scale"].as<bool>();
  136. } else {
  137. use_scale_ = true;
  138. }
  139. height_ = item["height"].as<int>();
  140. width_ = item["width"].as<int>();
  141. if (height_ <= 0 || width_ <= 0) {
  142. std::cerr << "[Resize] target_size should greater than 0" << std::endl;
  143. exit(-1);
  144. }
  145. }
  146. virtual bool Run(cv::Mat* im);
  147. virtual bool ShapeInfer(const std::vector<int>& in_shape,
  148. std::vector<int>* out_shape);
  149. virtual std::string Name() { return "Resize"; }
  150. private:
  151. int height_;
  152. int width_;
  153. int interp_;
  154. bool use_scale_;
  155. };
  156. class BGR2RGB : public Transform {
  157. public:
  158. virtual void Init(const YAML::Node& item) {
  159. }
  160. virtual bool Run(cv::Mat* im);
  161. virtual bool ShapeInfer(const std::vector<int>& in_shape,
  162. std::vector<int>* out_shape);
  163. virtual std::string Name() { return "BGR2RGB"; }
  164. };
  165. class RGB2BGR : public Transform {
  166. public:
  167. virtual void Init(const YAML::Node& item) {
  168. }
  169. virtual bool Run(cv::Mat* im);
  170. virtual bool ShapeInfer(const std::vector<int>& in_shape,
  171. std::vector<int>* out_shape);
  172. virtual std::string Name() { return "RGB2BGR"; }
  173. };
  174. class Padding : public Transform {
  175. public:
  176. virtual void Init(const YAML::Node& item) {
  177. if (item["stride"].IsDefined()) {
  178. stride_ = item["stride"].as<int>();
  179. if (stride_ < 1) {
  180. std::cerr << "[Padding] coarest_stride should greater than 0"
  181. << std::endl;
  182. exit(-1);
  183. }
  184. }
  185. if (item["width"].IsDefined() && item["height"].IsDefined()) {
  186. width_ = item["width"].as<int>();
  187. height_ = item["height"].as<int>();
  188. }
  189. if (item["im_padding_value"].IsDefined()) {
  190. im_value_ = item["im_padding_value"].as<std::vector<float>>();
  191. } else {
  192. im_value_ = {0, 0, 0};
  193. }
  194. }
  195. virtual bool Run(cv::Mat* im);
  196. virtual bool ShapeInfer(const std::vector<int>& in_shape,
  197. std::vector<int>* out_shape);
  198. virtual std::string Name() { return "Padding"; }
  199. virtual void GeneralPadding(cv::Mat* im,
  200. const std::vector<float>& padding_val,
  201. int padding_w, int padding_h);
  202. virtual void MultichannelPadding(cv::Mat* im,
  203. const std::vector<float>& padding_val,
  204. int padding_w, int padding_h);
  205. virtual bool Run(cv::Mat* im, int padding_w, int padding_h);
  206. private:
  207. int stride_ = -1;
  208. int width_ = 0;
  209. int height_ = 0;
  210. std::vector<float> im_value_;
  211. };
  212. class CenterCrop : public Transform {
  213. public:
  214. virtual void Init(const YAML::Node& item) {
  215. height_ = item["width"].as<int>();
  216. width_ = item["height"].as<int>();
  217. }
  218. virtual bool Run(cv::Mat* im);
  219. virtual bool ShapeInfer(const std::vector<int>& in_shape,
  220. std::vector<int>* out_shape);
  221. virtual std::string Name() { return "CenterCrop"; }
  222. private:
  223. int height_;
  224. int width_;
  225. };
  226. class Clip : public Transform {
  227. public:
  228. virtual void Init(const YAML::Node& item) {
  229. min_val_ = item["min_val"].as<std::vector<float>>();
  230. max_val_ = item["max_val"].as<std::vector<float>>();
  231. }
  232. virtual bool Run(cv::Mat* im);
  233. virtual bool ShapeInfer(const std::vector<int>& in_shape,
  234. std::vector<int>* out_shape);
  235. virtual std::string Name() { return "Clip"; }
  236. private:
  237. std::vector<float> min_val_;
  238. std::vector<float> max_val_;
  239. };
  240. class Permute : public Transform {
  241. public:
  242. virtual void Init(const YAML::Node& item) {}
  243. virtual bool Run(cv::Mat* im);
  244. virtual bool ShapeInfer(const std::vector<int>& in_shape,
  245. std::vector<int>* out_shape);
  246. virtual std::string Name() { return "Permute"; }
  247. };
  248. class Convert : public Transform {
  249. public:
  250. virtual void Init(const YAML::Node& item) {
  251. dtype_ = item["dtype"].as<std::string>();
  252. }
  253. virtual bool Run(cv::Mat* im);
  254. virtual bool ShapeInfer(const std::vector<int>& in_shape,
  255. std::vector<int>* out_shape);
  256. virtual std::string Name() { return "Convert"; }
  257. private:
  258. std::string dtype_;
  259. };
  260. class OcrResize : public Transform {
  261. public:
  262. virtual void Init(const YAML::Node& item) {
  263. height_ = item["height"].as<int>();
  264. width_ = item["width"].as<int>();
  265. is_pad_ = item["is_pad"].as<bool>();
  266. fix_width_ = item["fix_width"].as<bool>();
  267. if (item["interp"].IsDefined()) {
  268. interp_ = item["interp"].as<int>();
  269. } else {
  270. interp_ = 1;
  271. }
  272. if (item["value"].IsDefined()) {
  273. std::vector<float> value = item["value"].as<std::vector<float>>();
  274. value_ = cv::Scalar(value[0], value[1], value[2]);
  275. } else {
  276. value_ = cv::Scalar(0, 0, 0);
  277. }
  278. }
  279. virtual bool Run(cv::Mat* im);
  280. virtual bool ShapeInfer(const std::vector<int>& in_shape,
  281. std::vector<int>* out_shape);
  282. virtual std::string Name() { return "OcrResize"; }
  283. private:
  284. int GeneralWidth(int w, int h);
  285. int height_;
  286. int width_;
  287. int interp_;
  288. bool is_pad_;
  289. bool fix_width_;
  290. cv::Scalar value_;
  291. };
  292. class OcrTrtResize : public Transform {
  293. public:
  294. virtual void Init(const YAML::Node& item) {
  295. height_ = item["height"].as<int>();
  296. width_ = item["width"].as<int>();
  297. if (item["interp"].IsDefined()) {
  298. interp_ = item["interp"].as<int>();
  299. } else {
  300. interp_ = 1;
  301. }
  302. }
  303. virtual bool Run(cv::Mat* im);
  304. virtual bool ShapeInfer(const std::vector<int>& in_shape,
  305. std::vector<int>* out_shape);
  306. virtual std::string Name() { return "OcrTrtResize"; }
  307. private:
  308. int height_;
  309. int width_;
  310. int interp_;
  311. };
  312. } // namespace PaddleDeploy