ocr.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright (c) 2022 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 "ultra_infer/vision/visualize/visualize.h"
  15. namespace ultra_infer {
  16. namespace vision {
  17. cv::Mat VisOcr(const cv::Mat &im, const OCRResult &ocr_result,
  18. const float score_threshold) {
  19. auto vis_im = im.clone();
  20. bool have_score = (ocr_result.boxes.size() == ocr_result.rec_scores.size());
  21. for (int n = 0; n < ocr_result.boxes.size(); n++) {
  22. if (have_score) {
  23. if (ocr_result.rec_scores[n] < score_threshold) {
  24. continue;
  25. }
  26. }
  27. cv::Point rook_points[4];
  28. for (int m = 0; m < 4; m++) {
  29. rook_points[m] = cv::Point(int(ocr_result.boxes[n][m * 2]),
  30. int(ocr_result.boxes[n][m * 2 + 1]));
  31. }
  32. const cv::Point *ppt[1] = {rook_points};
  33. int npt[] = {4};
  34. cv::polylines(vis_im, ppt, npt, 1, 1, CV_RGB(0, 255, 0), 2, 8, 0);
  35. }
  36. return vis_im;
  37. }
  38. cv::Mat VisCURVEOcr(const cv::Mat &im, const OCRCURVEResult &ocr_result,
  39. const float score_threshold) {
  40. auto vis_im = im.clone();
  41. bool have_score = (ocr_result.boxes.size() == ocr_result.rec_scores.size());
  42. for (int n = 0; n < ocr_result.boxes.size(); n++) {
  43. if (have_score) {
  44. if (ocr_result.rec_scores[n] < score_threshold) {
  45. continue;
  46. }
  47. }
  48. std::vector<cv::Point> rook_points;
  49. for (int m = 0; m < ocr_result.boxes[n].size() / 2; m++) {
  50. rook_points.push_back(cv::Point(int(ocr_result.boxes[n][m * 2]),
  51. int(ocr_result.boxes[n][m * 2 + 1])));
  52. }
  53. if (!rook_points.empty()) {
  54. cv::Point *ppt = &rook_points[0];
  55. int npt = static_cast<int>(rook_points.size());
  56. cv::polylines(vis_im, &ppt, &npt, 1, 1, CV_RGB(0, 255, 0), 2, 8, 0);
  57. }
  58. }
  59. return vis_im;
  60. }
  61. cv::Mat Visualize::VisOcr(const cv::Mat &im, const OCRResult &ocr_result) {
  62. FDWARNING
  63. << "DEPRECATED: ultra_infer::vision::Visualize::VisOcr is deprecated, "
  64. "please use ultra_infer::vision:VisOcr function instead."
  65. << std::endl;
  66. auto vis_im = im.clone();
  67. for (int n = 0; n < ocr_result.boxes.size(); n++) {
  68. cv::Point rook_points[4];
  69. for (int m = 0; m < 4; m++) {
  70. rook_points[m] = cv::Point(int(ocr_result.boxes[n][m * 2]),
  71. int(ocr_result.boxes[n][m * 2 + 1]));
  72. }
  73. const cv::Point *ppt[1] = {rook_points};
  74. int npt[] = {4};
  75. cv::polylines(vis_im, ppt, npt, 1, 1, CV_RGB(0, 255, 0), 2, 8, 0);
  76. }
  77. return vis_im;
  78. }
  79. cv::Mat Visualize::VisCURVEOcr(const cv::Mat &im,
  80. const OCRCURVEResult &ocr_result) {
  81. FDWARNING
  82. << "DEPRECATED: ultra_infer::vision::Visualize::VisOcr is deprecated, "
  83. "please use ultra_infer::vision:VisOcr function instead."
  84. << std::endl;
  85. auto vis_im = im.clone();
  86. for (int n = 0; n < ocr_result.boxes.size(); n++) {
  87. std::vector<cv::Point> rook_points;
  88. for (int m = 0; m < ocr_result.boxes[n].size() / 2; m++) {
  89. rook_points.push_back(cv::Point(int(ocr_result.boxes[n][m * 2]),
  90. int(ocr_result.boxes[n][m * 2 + 1])));
  91. }
  92. if (!rook_points.empty()) {
  93. cv::Point *ppt = &rook_points[0];
  94. int npt = static_cast<int>(rook_points.size());
  95. cv::polylines(vis_im, &ppt, &npt, 1, 1, CV_RGB(0, 255, 0), 2, 8, 0);
  96. }
  97. }
  98. return vis_im;
  99. }
  100. } // namespace vision
  101. } // namespace ultra_infer