utils.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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__ = [
  15. "convert_points_to_boxes",
  16. "get_sub_regions_ocr_res"
  17. ]
  18. import numpy as np
  19. import copy
  20. def convert_points_to_boxes(dt_polys):
  21. if len(dt_polys) > 0:
  22. dt_polys_tmp = dt_polys.copy()
  23. dt_polys_tmp = np.array(dt_polys_tmp)
  24. boxes_left = np.min(dt_polys_tmp[:, :, 0], axis=1)
  25. boxes_right = np.max(dt_polys_tmp[:, :, 0], axis=1)
  26. boxes_top = np.min(dt_polys_tmp[:, :, 1], axis=1)
  27. boxes_bottom = np.max(dt_polys_tmp[:, :, 1], axis=1)
  28. dt_boxes = np.array([boxes_left, boxes_top, boxes_right, boxes_bottom])
  29. dt_boxes = dt_boxes.T
  30. else:
  31. dt_boxes = np.array([])
  32. return dt_boxes
  33. def get_overlap_boxes_idx(src_boxes, ref_boxes):
  34. '''get overlap boxes idx'''
  35. match_idx_list = []
  36. src_boxes_num = len(src_boxes)
  37. if src_boxes_num > 0 and len(ref_boxes) > 0:
  38. for rno in range(len(ref_boxes)):
  39. ref_box = ref_boxes[rno]
  40. x1 = np.maximum(ref_box[0], src_boxes[:, 0])
  41. y1 = np.maximum(ref_box[1], src_boxes[:, 1])
  42. x2 = np.minimum(ref_box[2], src_boxes[:, 2])
  43. y2 = np.minimum(ref_box[3], src_boxes[:, 3])
  44. pub_w = x2 - x1
  45. pub_h = y2 - y1
  46. match_idx = np.where((pub_w > 3) & (pub_h > 3))[0]
  47. match_idx_list.extend(match_idx)
  48. return match_idx_list
  49. def get_sub_regions_ocr_res(overall_ocr_res, object_boxes, flag_within=True):
  50. """
  51. :param flag_within: True (within the object regions), False (outside the object regions)
  52. :return:
  53. """
  54. sub_regions_ocr_res = copy.deepcopy(overall_ocr_res)
  55. sub_regions_ocr_res['input_img'] = overall_ocr_res['input_img']
  56. sub_regions_ocr_res['img_id'] = -1
  57. sub_regions_ocr_res['dt_polys'] = []
  58. sub_regions_ocr_res['rec_text'] = []
  59. sub_regions_ocr_res['rec_score'] = []
  60. sub_regions_ocr_res['dt_boxes'] = []
  61. overall_text_boxes = overall_ocr_res['dt_boxes']
  62. match_idx_list = get_overlap_boxes_idx(overall_text_boxes, object_boxes)
  63. match_idx_list = list(set(match_idx_list))
  64. for box_no in range(len(overall_text_boxes)):
  65. if flag_within:
  66. if box_no in match_idx_list:
  67. flag_match = True
  68. else:
  69. flag_match = False
  70. else:
  71. if box_no not in match_idx_list:
  72. flag_match = True
  73. else:
  74. flag_match = False
  75. if flag_match:
  76. sub_regions_ocr_res['dt_polys'].append(overall_ocr_res['dt_polys'][box_no])
  77. sub_regions_ocr_res['rec_text'].append(overall_ocr_res['rec_text'][box_no])
  78. sub_regions_ocr_res['rec_score'].append(overall_ocr_res['rec_score'][box_no])
  79. sub_regions_ocr_res['dt_boxes'].append(overall_ocr_res['dt_boxes'][box_no])
  80. return sub_regions_ocr_res