utils.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  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. __all__ = ["convert_points_to_boxes", "get_sub_regions_ocr_res"]
  15. import numpy as np
  16. import copy
  17. from ..ocr.result import OCRResult
  18. def convert_points_to_boxes(dt_polys: list) -> np.ndarray:
  19. """
  20. Converts a list of polygons to a numpy array of bounding boxes.
  21. Args:
  22. dt_polys (list): A list of polygons, where each polygon is represented
  23. as a list of (x, y) points.
  24. Returns:
  25. np.ndarray: A numpy array of bounding boxes, where each box is represented
  26. as [left, top, right, bottom].
  27. If the input list is empty, returns an empty numpy array.
  28. """
  29. if len(dt_polys) > 0:
  30. dt_polys_tmp = dt_polys.copy()
  31. dt_polys_tmp = np.array(dt_polys_tmp)
  32. boxes_left = np.min(dt_polys_tmp[:, :, 0], axis=1)
  33. boxes_right = np.max(dt_polys_tmp[:, :, 0], axis=1)
  34. boxes_top = np.min(dt_polys_tmp[:, :, 1], axis=1)
  35. boxes_bottom = np.max(dt_polys_tmp[:, :, 1], axis=1)
  36. dt_boxes = np.array([boxes_left, boxes_top, boxes_right, boxes_bottom])
  37. dt_boxes = dt_boxes.T
  38. else:
  39. dt_boxes = np.array([])
  40. return dt_boxes
  41. def get_overlap_boxes_idx(src_boxes: np.ndarray, ref_boxes: np.ndarray) -> list:
  42. """
  43. Get the indices of source boxes that overlap with reference boxes based on a specified threshold.
  44. Args:
  45. src_boxes (np.ndarray): A 2D numpy array of source bounding boxes.
  46. ref_boxes (np.ndarray): A 2D numpy array of reference bounding boxes.
  47. Returns:
  48. list: A list of indices of source boxes that overlap with any reference box.
  49. """
  50. match_idx_list = []
  51. src_boxes_num = len(src_boxes)
  52. if src_boxes_num > 0 and len(ref_boxes) > 0:
  53. for rno in range(len(ref_boxes)):
  54. ref_box = ref_boxes[rno]
  55. x1 = np.maximum(ref_box[0], src_boxes[:, 0])
  56. y1 = np.maximum(ref_box[1], src_boxes[:, 1])
  57. x2 = np.minimum(ref_box[2], src_boxes[:, 2])
  58. y2 = np.minimum(ref_box[3], src_boxes[:, 3])
  59. pub_w = x2 - x1
  60. pub_h = y2 - y1
  61. match_idx = np.where((pub_w > 3) & (pub_h > 3))[0]
  62. match_idx_list.extend(match_idx)
  63. return match_idx_list
  64. def get_sub_regions_ocr_res(
  65. overall_ocr_res: OCRResult, object_boxes: list, flag_within: bool = True
  66. ) -> OCRResult:
  67. """
  68. Filters OCR results to only include text boxes within specified object boxes based on a flag.
  69. Args:
  70. overall_ocr_res (OCRResult): The original OCR result containing all text boxes.
  71. object_boxes (list): A list of bounding boxes for the objects of interest.
  72. flag_within (bool): If True, only include text boxes within the object boxes. If False, exclude text boxes within the object boxes.
  73. Returns:
  74. OCRResult: A filtered OCR result containing only the relevant text boxes.
  75. """
  76. sub_regions_ocr_res = copy.deepcopy(overall_ocr_res)
  77. sub_regions_ocr_res["input_img"] = overall_ocr_res["input_img"]
  78. sub_regions_ocr_res["img_id"] = -1
  79. sub_regions_ocr_res["dt_polys"] = []
  80. sub_regions_ocr_res["rec_text"] = []
  81. sub_regions_ocr_res["rec_score"] = []
  82. sub_regions_ocr_res["dt_boxes"] = []
  83. overall_text_boxes = overall_ocr_res["dt_boxes"]
  84. match_idx_list = get_overlap_boxes_idx(overall_text_boxes, object_boxes)
  85. match_idx_list = list(set(match_idx_list))
  86. for box_no in range(len(overall_text_boxes)):
  87. if flag_within:
  88. if box_no in match_idx_list:
  89. flag_match = True
  90. else:
  91. flag_match = False
  92. else:
  93. if box_no not in match_idx_list:
  94. flag_match = True
  95. else:
  96. flag_match = False
  97. if flag_match:
  98. sub_regions_ocr_res["dt_polys"].append(overall_ocr_res["dt_polys"][box_no])
  99. sub_regions_ocr_res["rec_text"].append(overall_ocr_res["rec_text"][box_no])
  100. sub_regions_ocr_res["rec_score"].append(
  101. overall_ocr_res["rec_score"][box_no]
  102. )
  103. sub_regions_ocr_res["dt_boxes"].append(overall_ocr_res["dt_boxes"][box_no])
  104. return sub_regions_ocr_res