transforms.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. double beta = -1.0 * (mean_[c] + min_val_[c] * alpha) / std_[c];
  61. alpha /= 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. if (item["keep_ratio"].IsDefined()) {
  150. keep_ratio_ = item["keep_ratio"].as<bool>();
  151. } else {
  152. keep_ratio_ = false;
  153. }
  154. height_ = item["height"].as<int>();
  155. width_ = item["width"].as<int>();
  156. if (height_ <= 0 || width_ <= 0) {
  157. std::cerr << "[Resize] target_size should greater than 0" << std::endl;
  158. exit(-1);
  159. }
  160. }
  161. virtual bool Run(cv::Mat* im);
  162. virtual bool ShapeInfer(const std::vector<int>& in_shape,
  163. std::vector<int>* out_shape);
  164. virtual std::string Name() { return "Resize"; }
  165. private:
  166. int height_;
  167. int width_;
  168. int interp_;
  169. bool use_scale_;
  170. bool keep_ratio_;
  171. };
  172. class BGR2RGB : public Transform {
  173. public:
  174. virtual void Init(const YAML::Node& item) {
  175. }
  176. virtual bool Run(cv::Mat* im);
  177. virtual bool ShapeInfer(const std::vector<int>& in_shape,
  178. std::vector<int>* out_shape);
  179. virtual std::string Name() { return "BGR2RGB"; }
  180. };
  181. class RGB2BGR : public Transform {
  182. public:
  183. virtual void Init(const YAML::Node& item) {
  184. }
  185. virtual bool Run(cv::Mat* im);
  186. virtual bool ShapeInfer(const std::vector<int>& in_shape,
  187. std::vector<int>* out_shape);
  188. virtual std::string Name() { return "RGB2BGR"; }
  189. };
  190. class Padding : public Transform {
  191. public:
  192. virtual void Init(const YAML::Node& item) {
  193. if (item["stride"].IsDefined()) {
  194. stride_ = item["stride"].as<int>();
  195. if (stride_ < 1) {
  196. std::cerr << "[Padding] coarest_stride should greater than 0"
  197. << std::endl;
  198. exit(-1);
  199. }
  200. }
  201. if (item["width"].IsDefined() && item["height"].IsDefined()) {
  202. width_ = item["width"].as<int>();
  203. height_ = item["height"].as<int>();
  204. }
  205. if (item["im_padding_value"].IsDefined()) {
  206. im_value_ = item["im_padding_value"].as<std::vector<float>>();
  207. } else {
  208. im_value_ = {0, 0, 0};
  209. }
  210. }
  211. virtual bool Run(cv::Mat* im);
  212. virtual bool ShapeInfer(const std::vector<int>& in_shape,
  213. std::vector<int>* out_shape);
  214. virtual std::string Name() { return "Padding"; }
  215. virtual void GeneralPadding(cv::Mat* im,
  216. const std::vector<float>& padding_val,
  217. int padding_w, int padding_h);
  218. virtual void MultichannelPadding(cv::Mat* im,
  219. const std::vector<float>& padding_val,
  220. int padding_w, int padding_h);
  221. virtual bool Run(cv::Mat* im, int padding_w, int padding_h);
  222. private:
  223. int stride_ = -1;
  224. int width_ = 0;
  225. int height_ = 0;
  226. std::vector<float> im_value_;
  227. };
  228. class CenterCrop : public Transform {
  229. public:
  230. virtual void Init(const YAML::Node& item) {
  231. height_ = item["width"].as<int>();
  232. width_ = item["height"].as<int>();
  233. }
  234. virtual bool Run(cv::Mat* im);
  235. virtual bool ShapeInfer(const std::vector<int>& in_shape,
  236. std::vector<int>* out_shape);
  237. virtual std::string Name() { return "CenterCrop"; }
  238. private:
  239. int height_;
  240. int width_;
  241. };
  242. class Clip : public Transform {
  243. public:
  244. virtual void Init(const YAML::Node& item) {
  245. min_val_ = item["min_val"].as<std::vector<float>>();
  246. max_val_ = item["max_val"].as<std::vector<float>>();
  247. }
  248. virtual bool Run(cv::Mat* im);
  249. virtual bool ShapeInfer(const std::vector<int>& in_shape,
  250. std::vector<int>* out_shape);
  251. virtual std::string Name() { return "Clip"; }
  252. private:
  253. std::vector<float> min_val_;
  254. std::vector<float> max_val_;
  255. };
  256. class Permute : public Transform {
  257. public:
  258. virtual void Init(const YAML::Node& item) {}
  259. virtual bool Run(cv::Mat* im);
  260. virtual bool ShapeInfer(const std::vector<int>& in_shape,
  261. std::vector<int>* out_shape);
  262. virtual std::string Name() { return "Permute"; }
  263. };
  264. class Convert : public Transform {
  265. public:
  266. virtual void Init(const YAML::Node& item) {
  267. dtype_ = item["dtype"].as<std::string>();
  268. }
  269. virtual bool Run(cv::Mat* im);
  270. virtual bool ShapeInfer(const std::vector<int>& in_shape,
  271. std::vector<int>* out_shape);
  272. virtual std::string Name() { return "Convert"; }
  273. private:
  274. std::string dtype_;
  275. };
  276. class OcrResize : public Transform {
  277. public:
  278. virtual void Init(const YAML::Node& item) {
  279. height_ = item["height"].as<int>();
  280. width_ = item["width"].as<int>();
  281. is_pad_ = item["is_pad"].as<bool>();
  282. fix_width_ = item["fix_width"].as<bool>();
  283. if (item["interp"].IsDefined()) {
  284. interp_ = item["interp"].as<int>();
  285. } else {
  286. interp_ = 1;
  287. }
  288. if (item["value"].IsDefined()) {
  289. std::vector<float> value = item["value"].as<std::vector<float>>();
  290. value_ = cv::Scalar(value[0], value[1], value[2]);
  291. } else {
  292. value_ = cv::Scalar(0, 0, 0);
  293. }
  294. }
  295. virtual bool Run(cv::Mat* im);
  296. virtual bool ShapeInfer(const std::vector<int>& in_shape,
  297. std::vector<int>* out_shape);
  298. virtual std::string Name() { return "OcrResize"; }
  299. private:
  300. int GeneralWidth(int w, int h);
  301. int height_;
  302. int width_;
  303. int interp_;
  304. bool is_pad_;
  305. bool fix_width_;
  306. cv::Scalar value_;
  307. };
  308. class OcrTrtResize : public Transform {
  309. public:
  310. virtual void Init(const YAML::Node& item) {
  311. height_ = item["height"].as<int>();
  312. width_ = item["width"].as<int>();
  313. if (item["interp"].IsDefined()) {
  314. interp_ = item["interp"].as<int>();
  315. } else {
  316. interp_ = 1;
  317. }
  318. }
  319. virtual bool Run(cv::Mat* im);
  320. virtual bool ShapeInfer(const std::vector<int>& in_shape,
  321. std::vector<int>* out_shape);
  322. virtual std::string Name() { return "OcrTrtResize"; }
  323. private:
  324. int height_;
  325. int width_;
  326. int interp_;
  327. };
  328. } // namespace PaddleDeploy