def approaching_horizontal_point2(bg, line1): """ """ center_point = (line1[0][0] + line1[1][0]) / 2 + 5, (line1[0][1] + line1[1][1]) / 2 pt1, pt2 = line1 original_pt1, original_pt2 = copy.deepcopy(pt1), copy.deepcopy(pt2) original_pt1 = original_pt1[0] + 2, original_pt1[1] original_pt2 = original_pt2[0] + 2, original_pt2[1] pt1, pt2 = original_pt1, center_point while True: pt1, pt2 = translation_line_first_side(pt1, pt2, direction='left') tmp_bg = np.zeros_like(bg) tmp_bg = draw_line_from_points([pt1, pt2], tmp_bg, line_pixel=1) tmp_bg = tmp_bg + bg write_image('tmp_bg.jpg', tmp_bg * 255) max_value = np.max(tmp_bg) if max_value > 1: location = np.where(tmp_bg > 1) first_location, last_location = _sort_location(location) first_point = first_location break pt1, pt2 = center_point, original_pt2 while True: pt1, pt2 = translation_line_second_side(pt1, pt2, direction='left') tmp_bg = np.zeros_like(bg) tmp_bg = draw_line_from_points([pt1, pt2], tmp_bg, line_pixel=1) tmp_bg = tmp_bg + bg write_image('tmp_bg.jpg', tmp_bg * 255) max_value = np.max(tmp_bg) if max_value > 1: location = np.where(tmp_bg > 1) first_location, last_location = _sort_location(location) second_point = first_location break pt1, pt2 = first_point, second_point while True: pt1, pt2 = translation_line(pt1, pt2, direction='right') tmp_bg = np.zeros_like(bg) tmp_bg = draw_line_from_points([pt1, pt2], tmp_bg, line_pixel=1) tmp_bg = tmp_bg + bg write_image('tmp_bg.jpg', tmp_bg * 255) max_value = np.max(tmp_bg) if max_value <= 1: return pt1, pt2
def __corrected_bbox_second_time(self, bg, bbox): """ corrected second time """ center_point = (bbox[0][0] + bbox[2][0]) / 2, (bbox[0][1] + bbox[2][1]) / 2 des_line = np.zeros_like(bg) bbox[0], bbox[1] = self.get_new_corrected_point(bg, bbox[0], bbox[1], direction='down') bbox[1], bbox[2] = self.get_new_corrected_point(bg, bbox[1], bbox[2], direction='left') bbox[2], bbox[3] = self.get_new_corrected_point(bg, bbox[2], bbox[3], direction='up') bbox[3], bbox[1] = self.get_new_corrected_point(bg, bbox[3], bbox[0], direction='right') des_line = draw_line_from_points([bbox[0], bbox[1]], des_line, line_pixel=1) des_line = draw_line_from_points([bbox[1], bbox[2]], des_line, line_pixel=1) des_line = draw_line_from_points([bbox[2], bbox[3]], des_line, line_pixel=1) des_line = draw_line_from_points([bbox[3], bbox[0]], des_line, line_pixel=1) write_image('des_line.jpg', des_line * 255)
def _corrected_bbox(bg, bbox): """ corrected bbox """ distance1 = get_distance(bbox[0], bbox[1]) distance2 = get_distance(bbox[1], bbox[2]) if distance2 == 0 or distance1 == 0: return None if distance1 > distance2: line1, line2 = [bbox[0], bbox[3]], [bbox[1], bbox[2]] ver_line1 = approaching_horizontal_point0(bg, line1) ver_line2 = approaching_horizontal_point2(bg, line2) hor_line1 = bbox[0], bbox[1] hor_line2 = bbox[2], bbox[3] tmp_bg1 = draw_line_from_points(ver_line1, bg, line_pixel=1) tmp_bg2 = draw_line_from_points(ver_line2, bg, line_pixel=1) tmp_bg3 = draw_line_from_points(hor_line1, bg, line_pixel=1) tmp_bg4 = draw_line_from_points(hor_line2, bg, line_pixel=1) bg = bg + tmp_bg1 + tmp_bg2 + tmp_bg3 + tmp_bg4 write_image('tmp_bg.jpg', bg * 255) pt1 = cross_point(ver_line1, hor_line1) pt2 = cross_point(ver_line1, hor_line2) pt3 = cross_point(ver_line2, hor_line1) pt4 = cross_point(ver_line2, hor_line2) else: line1, line2 = [bbox[0], bbox[1]], [bbox[2], bbox[3]] hor_line1 = approaching_vertical_point0(bg, line1) hor_line2 = approaching_vertical_point2(bg, line2) ver_line1 = bbox[0], bbox[3] ver_line2 = bbox[1], bbox[2] pt1 = cross_point(ver_line1, hor_line1) pt2 = cross_point(ver_line1, hor_line2) pt3 = cross_point(ver_line2, hor_line1) pt4 = cross_point(ver_line2, hor_line2) points = resort_points([pt1, pt2, pt3, pt4]) return points
def gen_bbox(self): """ :return: """ line_points = self._get_gap_lines() bg = np.zeros_like(self.original_textarea_pixel, dtype=np.int) for point in line_points: tmp_canvas = draw_line_from_points(point, bg) bg = bg + tmp_canvas # write_image('line.jpg', bg * 255) bg = self.format_textarea_pixel - bg bg[np.where(bg < 0)] = 0 # write_image('pix.jpg', bg * 255) text_area_domain = ContainerBboxGenerate._get_connected_domain(bg) bboxes = self._gen_bboxes_by_domain(text_area_domain) return bboxes
def get_new_corrected_point(bg, pt1, pt2, direction='down'): """ get new corrected points """ original_pt1, original_pt2 = copy.deepcopy(pt1), copy.deepcopy(pt2) is_move_points = False while True: last_pt1, last_pt2 = copy.deepcopy(pt1), copy.deepcopy(pt2) pt1, pt2 = translation_line(pt1, pt2, direction=direction) tmp_bg = np.zeros_like(bg) tmp_bg = draw_line_from_points([pt1, pt2], tmp_bg, line_pixel=1) tmp_bg = tmp_bg + bg max_value = np.max(tmp_bg) if max_value <= 1: is_move_points = True else: break write_image('tmp_bg.jpg', tmp_bg * 255) if is_move_points: pt1, pt2 = last_pt1, last_pt2 original_pt1, original_pt2 = copy.deepcopy(pt1), copy.deepcopy(pt2) """first side""" while True: last_pt1, last_pt2 = copy.deepcopy(original_pt1), copy.deepcopy( original_pt2) pt1, pt2 = translation_line_first_side(pt1, pt2, direction=direction) tmp_bg = np.zeros_like(bg) tmp_bg = draw_line_from_points([pt1, pt2], tmp_bg, line_pixel=1) tmp_bg = tmp_bg + bg max_value = np.max(tmp_bg) if max_value <= 1: is_move_points = True else: break write_image('tmp_bg.jpg', tmp_bg * 255) final_pt1, final_pt2 = None, None if is_move_points: final_pt1 = last_pt1 """second side""" while True: last_pt1, last_pt2 = copy.deepcopy(original_pt1), copy.deepcopy( original_pt2) pt1, pt2 = translation_line_second_side(pt1, pt2, direction=direction) tmp_bg = np.zeros_like(bg) tmp_bg = draw_line_from_points([pt1, pt2], tmp_bg, line_pixel=1) tmp_bg = tmp_bg + bg max_value = np.max(tmp_bg) if max_value <= 1: is_move_points = True else: break if is_move_points: final_pt2 = last_pt2 write_image('tmp_bg.jpg', tmp_bg * 255) if is_move_points: tmp_bg = draw_line_from_points([final_pt1, final_pt2], tmp_bg, line_pixel=1) tmp_bg = tmp_bg + bg write_image('tmp_bg.jpg', tmp_bg * 255) return final_pt1, final_pt2 return original_pt1, original_pt2