transforms.h 10.0 KB

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