utility.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #pragma once
  15. #include <chrono>
  16. #include <iomanip>
  17. #include <iostream>
  18. #include <ostream>
  19. #include <stdlib.h>
  20. #include <vector>
  21. #include <algorithm>
  22. #include <cstring>
  23. #include <fstream>
  24. #include <numeric>
  25. #include "opencv2/core.hpp"
  26. #include "opencv2/imgcodecs.hpp"
  27. #include "opencv2/imgproc.hpp"
  28. namespace PaddleOCR {
  29. struct OCRPredictResult {
  30. std::vector<std::vector<int>> box;
  31. std::string text;
  32. float score = -1.0;
  33. float cls_score;
  34. int cls_label = -1;
  35. };
  36. struct StructurePredictResult {
  37. std::vector<float> box;
  38. std::vector<std::vector<int>> cell_box;
  39. std::string type;
  40. std::vector<OCRPredictResult> text_res;
  41. std::string html;
  42. float html_score = -1;
  43. float confidence;
  44. };
  45. class Utility {
  46. public:
  47. static std::vector<std::string> ReadDict(const std::string &path);
  48. static void VisualizeBboxes(const cv::Mat &srcimg,
  49. const std::vector<OCRPredictResult> &ocr_result,
  50. const std::string &save_path);
  51. static void VisualizeBboxes(const cv::Mat &srcimg,
  52. const StructurePredictResult &structure_result,
  53. const std::string &save_path);
  54. template <class ForwardIterator>
  55. inline static size_t argmax(ForwardIterator first, ForwardIterator last) {
  56. return std::distance(first, std::max_element(first, last));
  57. }
  58. static void GetAllFiles(const char *dir_name,
  59. std::vector<std::string> &all_inputs);
  60. static cv::Mat GetRotateCropImage(const cv::Mat &srcimage,
  61. std::vector<std::vector<int>> box);
  62. static std::vector<int> argsort(const std::vector<float> &array);
  63. static std::string basename(const std::string &filename);
  64. static bool PathExists(const std::string &path);
  65. static void CreateDir(const std::string &path);
  66. static void print_result(const std::vector<OCRPredictResult> &ocr_result);
  67. static cv::Mat crop_image(cv::Mat &img, const std::vector<int> &area);
  68. static cv::Mat crop_image(cv::Mat &img, const std::vector<float> &area);
  69. static void sorted_boxes(std::vector<OCRPredictResult> &ocr_result);
  70. static std::vector<int> xyxyxyxy2xyxy(std::vector<std::vector<int>> &box);
  71. static std::vector<int> xyxyxyxy2xyxy(std::vector<int> &box);
  72. static float fast_exp(float x);
  73. static std::vector<float>
  74. activation_function_softmax(std::vector<float> &src);
  75. static float iou(std::vector<int> &box1, std::vector<int> &box2);
  76. static float iou(std::vector<float> &box1, std::vector<float> &box2);
  77. private:
  78. static bool comparison_box(const OCRPredictResult &result1,
  79. const OCRPredictResult &result2) {
  80. if (result1.box[0][1] < result2.box[0][1]) {
  81. return true;
  82. } else if (result1.box[0][1] == result2.box[0][1]) {
  83. return result1.box[0][0] < result2.box[0][0];
  84. } else {
  85. return false;
  86. }
  87. }
  88. };
  89. } // namespace PaddleOCR