utils.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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__ = ["get_sub_regions_ocr_res"]
  15. import numpy as np
  16. import copy
  17. from ..ocr.result import OCRResult
  18. def get_overlap_boxes_idx(src_boxes: np.ndarray, ref_boxes: np.ndarray) -> list:
  19. """
  20. Get the indices of source boxes that overlap with reference boxes based on a specified threshold.
  21. Args:
  22. src_boxes (np.ndarray): A 2D numpy array of source bounding boxes.
  23. ref_boxes (np.ndarray): A 2D numpy array of reference bounding boxes.
  24. Returns:
  25. list: A list of indices of source boxes that overlap with any reference box.
  26. """
  27. match_idx_list = []
  28. src_boxes_num = len(src_boxes)
  29. if src_boxes_num > 0 and len(ref_boxes) > 0:
  30. for rno in range(len(ref_boxes)):
  31. ref_box = ref_boxes[rno]
  32. x1 = np.maximum(ref_box[0], src_boxes[:, 0])
  33. y1 = np.maximum(ref_box[1], src_boxes[:, 1])
  34. x2 = np.minimum(ref_box[2], src_boxes[:, 2])
  35. y2 = np.minimum(ref_box[3], src_boxes[:, 3])
  36. pub_w = x2 - x1
  37. pub_h = y2 - y1
  38. match_idx = np.where((pub_w > 3) & (pub_h > 3))[0]
  39. match_idx_list.extend(match_idx)
  40. return match_idx_list
  41. def get_sub_regions_ocr_res(
  42. overall_ocr_res: OCRResult, object_boxes: list, flag_within: bool = True
  43. ) -> OCRResult:
  44. """
  45. Filters OCR results to only include text boxes within specified object boxes based on a flag.
  46. Args:
  47. overall_ocr_res (OCRResult): The original OCR result containing all text boxes.
  48. object_boxes (list): A list of bounding boxes for the objects of interest.
  49. flag_within (bool): If True, only include text boxes within the object boxes. If False, exclude text boxes within the object boxes.
  50. Returns:
  51. OCRResult: A filtered OCR result containing only the relevant text boxes.
  52. """
  53. sub_regions_ocr_res = {}
  54. sub_regions_ocr_res["rec_polys"] = []
  55. sub_regions_ocr_res["rec_texts"] = []
  56. sub_regions_ocr_res["rec_scores"] = []
  57. sub_regions_ocr_res["rec_boxes"] = []
  58. overall_text_boxes = overall_ocr_res["rec_boxes"]
  59. match_idx_list = get_overlap_boxes_idx(overall_text_boxes, object_boxes)
  60. match_idx_list = list(set(match_idx_list))
  61. for box_no in range(len(overall_text_boxes)):
  62. if flag_within:
  63. if box_no in match_idx_list:
  64. flag_match = True
  65. else:
  66. flag_match = False
  67. else:
  68. if box_no not in match_idx_list:
  69. flag_match = True
  70. else:
  71. flag_match = False
  72. if flag_match:
  73. sub_regions_ocr_res["rec_polys"].append(
  74. overall_ocr_res["rec_polys"][box_no]
  75. )
  76. sub_regions_ocr_res["rec_texts"].append(
  77. overall_ocr_res["rec_texts"][box_no]
  78. )
  79. sub_regions_ocr_res["rec_scores"].append(
  80. overall_ocr_res["rec_scores"][box_no]
  81. )
  82. sub_regions_ocr_res["rec_boxes"].append(
  83. overall_ocr_res["rec_boxes"][box_no]
  84. )
  85. return sub_regions_ocr_res