| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- __all__ = [
- "convert_points_to_boxes",
- "get_sub_regions_ocr_res"
- ]
- import numpy as np
- import copy
- def convert_points_to_boxes(dt_polys):
- if len(dt_polys) > 0:
- dt_polys_tmp = dt_polys.copy()
- dt_polys_tmp = np.array(dt_polys_tmp)
- boxes_left = np.min(dt_polys_tmp[:, :, 0], axis=1)
- boxes_right = np.max(dt_polys_tmp[:, :, 0], axis=1)
- boxes_top = np.min(dt_polys_tmp[:, :, 1], axis=1)
- boxes_bottom = np.max(dt_polys_tmp[:, :, 1], axis=1)
- dt_boxes = np.array([boxes_left, boxes_top, boxes_right, boxes_bottom])
- dt_boxes = dt_boxes.T
- else:
- dt_boxes = np.array([])
- return dt_boxes
- def get_overlap_boxes_idx(src_boxes, ref_boxes):
- '''get overlap boxes idx'''
- match_idx_list = []
- src_boxes_num = len(src_boxes)
- if src_boxes_num > 0 and len(ref_boxes) > 0:
- for rno in range(len(ref_boxes)):
- ref_box = ref_boxes[rno]
- x1 = np.maximum(ref_box[0], src_boxes[:, 0])
- y1 = np.maximum(ref_box[1], src_boxes[:, 1])
- x2 = np.minimum(ref_box[2], src_boxes[:, 2])
- y2 = np.minimum(ref_box[3], src_boxes[:, 3])
- pub_w = x2 - x1
- pub_h = y2 - y1
- match_idx = np.where((pub_w > 3) & (pub_h > 3))[0]
- match_idx_list.extend(match_idx)
- return match_idx_list
- def get_sub_regions_ocr_res(overall_ocr_res, object_boxes, flag_within=True):
- """
- :param flag_within: True (within the object regions), False (outside the object regions)
- :return:
- """
- sub_regions_ocr_res = copy.deepcopy(overall_ocr_res)
- sub_regions_ocr_res['input_img'] = overall_ocr_res['input_img']
- sub_regions_ocr_res['img_id'] = -1
- sub_regions_ocr_res['dt_polys'] = []
- sub_regions_ocr_res['rec_text'] = []
- sub_regions_ocr_res['rec_score'] = []
- sub_regions_ocr_res['dt_boxes'] = []
- overall_text_boxes = overall_ocr_res['dt_boxes']
- match_idx_list = get_overlap_boxes_idx(overall_text_boxes, object_boxes)
- match_idx_list = list(set(match_idx_list))
- for box_no in range(len(overall_text_boxes)):
- if flag_within:
- if box_no in match_idx_list:
- flag_match = True
- else:
- flag_match = False
- else:
- if box_no not in match_idx_list:
- flag_match = True
- else:
- flag_match = False
- if flag_match:
- sub_regions_ocr_res['dt_polys'].append(overall_ocr_res['dt_polys'][box_no])
- sub_regions_ocr_res['rec_text'].append(overall_ocr_res['rec_text'][box_no])
- sub_regions_ocr_res['rec_score'].append(overall_ocr_res['rec_score'][box_no])
- sub_regions_ocr_res['dt_boxes'].append(overall_ocr_res['dt_boxes'][box_no])
- return sub_regions_ocr_res
|