matcher.cc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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/ocr/ppocr/utils/ocr_utils.h"
  15. namespace ultra_infer {
  16. namespace vision {
  17. namespace ocr {
  18. std::vector<int> Xyxyxyxy2Xyxy(std::array<int, 8> &box) {
  19. int x_collect[4] = {box[0], box[2], box[4], box[6]};
  20. int y_collect[4] = {box[1], box[3], box[5], box[7]};
  21. int left = int(*std::min_element(x_collect, x_collect + 4));
  22. int right = int(*std::max_element(x_collect, x_collect + 4));
  23. int top = int(*std::min_element(y_collect, y_collect + 4));
  24. int bottom = int(*std::max_element(y_collect, y_collect + 4));
  25. std::vector<int> box1(4, 0);
  26. box1[0] = left;
  27. box1[1] = top;
  28. box1[2] = right;
  29. box1[3] = bottom;
  30. return box1;
  31. }
  32. float Dis(std::vector<int> &box1, std::vector<int> &box2) {
  33. float x1_1 = float(box1[0]);
  34. float y1_1 = float(box1[1]);
  35. float x2_1 = float(box1[2]);
  36. float y2_1 = float(box1[3]);
  37. float x1_2 = float(box2[0]);
  38. float y1_2 = float(box2[1]);
  39. float x2_2 = float(box2[2]);
  40. float y2_2 = float(box2[3]);
  41. float dis = std::abs(x1_2 - x1_1) + std::abs(y1_2 - y1_1) +
  42. std::abs(x2_2 - x2_1) + std::abs(y2_2 - y2_1);
  43. float dis_2 = std::abs(x1_2 - x1_1) + std::abs(y1_2 - y1_1);
  44. float dis_3 = std::abs(x2_2 - x2_1) + std::abs(y2_2 - y2_1);
  45. return dis + std::min(dis_2, dis_3);
  46. }
  47. float Iou(std::vector<int> &box1, std::vector<int> &box2) {
  48. int area1 = std::max(0, box1[2] - box1[0]) * std::max(0, box1[3] - box1[1]);
  49. int area2 = std::max(0, box2[2] - box2[0]) * std::max(0, box2[3] - box2[1]);
  50. // computing the sum_area
  51. int sum_area = area1 + area2;
  52. // find the each point of intersect rectangle
  53. int x1 = std::max(box1[0], box2[0]);
  54. int y1 = std::max(box1[1], box2[1]);
  55. int x2 = std::min(box1[2], box2[2]);
  56. int y2 = std::min(box1[3], box2[3]);
  57. // judge if there is an intersect
  58. if (y1 >= y2 || x1 >= x2) {
  59. return 0.0;
  60. } else {
  61. int intersect = (x2 - x1) * (y2 - y1);
  62. return intersect / (sum_area - intersect + 0.00000001);
  63. }
  64. }
  65. bool ComparisonDis(const std::vector<float> &dis1,
  66. const std::vector<float> &dis2) {
  67. if (dis1[1] < dis2[1]) {
  68. return true;
  69. } else if (dis1[1] == dis2[1]) {
  70. return dis1[0] < dis2[0];
  71. } else {
  72. return false;
  73. }
  74. }
  75. } // namespace ocr
  76. } // namespace vision
  77. } // namespace ultra_infer