crnn_process.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #include "crnn_process.h" //NOLINT
  15. #include <algorithm>
  16. #include <memory>
  17. #include <string>
  18. const std::vector<int> rec_image_shape{3, 32, 320};
  19. cv::Mat CrnnResizeImg(cv::Mat img, float wh_ratio, int rec_image_height) {
  20. int imgC, imgH, imgW;
  21. imgC = rec_image_shape[0];
  22. imgH = rec_image_height;
  23. imgW = rec_image_shape[2];
  24. imgW = int(imgH * wh_ratio);
  25. float ratio = float(img.cols) / float(img.rows);
  26. int resize_w, resize_h;
  27. if (ceilf(imgH * ratio) > imgW)
  28. resize_w = imgW;
  29. else
  30. resize_w = int(ceilf(imgH * ratio));
  31. cv::Mat resize_img;
  32. cv::resize(img, resize_img, cv::Size(resize_w, imgH), 0.f, 0.f,
  33. cv::INTER_LINEAR);
  34. cv::copyMakeBorder(resize_img, resize_img, 0, 0, 0,
  35. int(imgW - resize_img.cols), cv::BORDER_CONSTANT,
  36. {127, 127, 127});
  37. return resize_img;
  38. }
  39. std::vector<std::string> ReadDict(std::string path) {
  40. std::ifstream in(path);
  41. std::string filename;
  42. std::string line;
  43. std::vector<std::string> m_vec;
  44. if (in) {
  45. while (getline(in, line)) {
  46. m_vec.push_back(line);
  47. }
  48. } else {
  49. std::cout << "no such file" << std::endl;
  50. }
  51. return m_vec;
  52. }
  53. cv::Mat GetRotateCropImage(cv::Mat srcimage,
  54. std::vector<std::vector<int>> box) {
  55. cv::Mat image;
  56. srcimage.copyTo(image);
  57. std::vector<std::vector<int>> points = box;
  58. int x_collect[4] = {box[0][0], box[1][0], box[2][0], box[3][0]};
  59. int y_collect[4] = {box[0][1], box[1][1], box[2][1], box[3][1]};
  60. int left = int(*std::min_element(x_collect, x_collect + 4));
  61. int right = int(*std::max_element(x_collect, x_collect + 4));
  62. int top = int(*std::min_element(y_collect, y_collect + 4));
  63. int bottom = int(*std::max_element(y_collect, y_collect + 4));
  64. cv::Mat img_crop;
  65. image(cv::Rect(left, top, right - left, bottom - top)).copyTo(img_crop);
  66. for (int i = 0; i < points.size(); i++) {
  67. points[i][0] -= left;
  68. points[i][1] -= top;
  69. }
  70. int img_crop_width =
  71. static_cast<int>(sqrt(pow(points[0][0] - points[1][0], 2) +
  72. pow(points[0][1] - points[1][1], 2)));
  73. int img_crop_height =
  74. static_cast<int>(sqrt(pow(points[0][0] - points[3][0], 2) +
  75. pow(points[0][1] - points[3][1], 2)));
  76. cv::Point2f pts_std[4];
  77. pts_std[0] = cv::Point2f(0., 0.);
  78. pts_std[1] = cv::Point2f(img_crop_width, 0.);
  79. pts_std[2] = cv::Point2f(img_crop_width, img_crop_height);
  80. pts_std[3] = cv::Point2f(0.f, img_crop_height);
  81. cv::Point2f pointsf[4];
  82. pointsf[0] = cv::Point2f(points[0][0], points[0][1]);
  83. pointsf[1] = cv::Point2f(points[1][0], points[1][1]);
  84. pointsf[2] = cv::Point2f(points[2][0], points[2][1]);
  85. pointsf[3] = cv::Point2f(points[3][0], points[3][1]);
  86. cv::Mat M = cv::getPerspectiveTransform(pointsf, pts_std);
  87. cv::Mat dst_img;
  88. cv::warpPerspective(img_crop, dst_img, M,
  89. cv::Size(img_crop_width, img_crop_height),
  90. cv::BORDER_REPLICATE);
  91. const float ratio = 1.5;
  92. if (static_cast<float>(dst_img.rows) >=
  93. static_cast<float>(dst_img.cols) * ratio) {
  94. cv::Mat srcCopy = cv::Mat(dst_img.rows, dst_img.cols, dst_img.depth());
  95. cv::transpose(dst_img, srcCopy);
  96. cv::flip(srcCopy, srcCopy, 0);
  97. return srcCopy;
  98. } else {
  99. return dst_img;
  100. }
  101. }