def xianyu(test_folder, show=False, write_img=True): all_imgs = glob.glob(test_folder + "/**") for input_path_img in all_imgs: postfix = input_path_img.split(".")[-1].lower() if postfix not in ["jpeg", "jpg", "png"]: continue name = os.path.basename(input_path_img).split('.')[0] org = cv2.imread(input_path_img) img = utils.resize_by_height(org, resize_height=800) compo = detect_compo(img, show=show) text = ocr.ocr(org, resize_height=800, show=show) compo_merge, categories = merge.incorporate(img, compo, text, show=show) utils.draw_bounding_box_class( img, compo_merge, categories, output=pjoin(test_folder, str(name) + '_det.png'), ) utils.save_corners_json(pjoin(test_folder, str(name) + '_det.json'), compo_merge, categories)
def ocr(img, resize_height=800, output_path=None, show=False): start = time.clock() data = pyt.image_to_data(img) bboxes = [] # level|page_num|block_num|par_num|line_num|word_num|left|top|width|height|conf|text for d in data.split('\n')[1:]: d = d.split() conf = d[10] if int(conf) != -1: bboxes.append([ int(d[6]), int(d[7]), int(d[6]) + int(d[8]), int(d[7]) + int(d[9]) ]) bboxes = merge_text(bboxes) bboxes = resize_label(bboxes, img.shape[0], resize_height) resize_img = utils.resize_by_height(img, resize_height) utils.draw_bounding_box(resize_img, bboxes, name='ocr', color=(255, 6, 6), show=show) if output_path is not None: utils.save_corners_json(output_path + '.json', bboxes, 'Text') # print('OCR [%.3fs] %s' % (time.clock() - start, img_path)) return bboxes
def xianyu(input_path_img, output_path, num, show=False, write_img=False): start = time.clock() org = cv2.imread(input_path_img) img = utils.resize_by_height(org, resize_height=800) compo = detect_compo(img, show=show, output_path=output_path.replace( 'rico_xianyu_bg_ocr', 'rico_xianyu_bg_cv')) text = ocr.ocr(org, show=show) compo_merge, categories = merge.incorporate(img, compo, text, show=show) utils.draw_bounding_box_class(img, compo_merge, categories, show=show) utils.draw_bounding_box_class(img, compo_merge, categories, name='non-text', non_text=True, show=show) # compo_merge = merge.merge_intersected_compo(compo_merge) utils.draw_bounding_box(img, compo_merge, name='merged', show=show) utils.save_corners_json(output_path + '.json', compo_merge, categories) print('[%.3fs] %d %s' % (time.clock() - start, num, input_path_img))
def xianyu(input_img_root='E:\\Mulong\\Datasets\\rico\\combined', output_root='E:\\Mulong\\Result\\rico\\rico_xianyu\\rico_xianyu_bg_ocr', show=False, write_img=False): data = json.load(open('E:\\Mulong\\Datasets\\rico\\instances_test.json', 'r')) input_paths_img = [pjoin(input_img_root, img['file_name'].split('/')[-1]) for img in data['images']] input_paths_img = sorted(input_paths_img, key=lambda x: int(x.split('\\')[-1][:-4])) # sorted by index num = 0 start_index = 3762 end_index = 100000 for input_path_img in input_paths_img: index = input_path_img.split('\\')[-1][:-4] if int(index) < start_index: continue if int(index) > end_index: break start = time.clock() org = cv2.imread(input_path_img) img = utils.resize_by_height(org, resize_height=800) compo = detect_compo(img, show=show) text = ocr.ocr(org, show=show) compo_merge, categories = merge.incorporate(img, compo, text, show=show) utils.draw_bounding_box_class(img, compo_merge, categories) utils.save_corners_json(pjoin(output_root, str(index) + '.json'), compo_merge, categories) print('[%.3fs] %d %s' % (time.clock() - start, num, input_path_img)) num += 1
def xianyu(input_path_img, output_path, show=False, write_img=False): start = time.clock() org = cv2.imread(input_path_img) img = utils.resize_by_height(org, resize_height=800) compo = detect_compo(img, show=show) text = ocr.ocr(org, show=show) # utils.draw_bounding_box_class(img, (compo + text), list(np.full(len(compo), 'Compo')) + list(np.full(len(text), 'Text')), show=True) compo_merge, categories = merge.incorporate(img, compo, text, show=show) utils.draw_bounding_box_class(img, compo_merge, categories, output=output_path + '.png' if write_img else None)
def xianyu(input_path_img, output_path, show=False): start = time.clock() org = cv2.imread(input_path_img) img = utils.resize_by_height(org, resize_height=800) compo = detect_compo(img, show=show) text = ocr.ocr(org, show=show) compo_merge, categories = merge.incorporate(img, compo, text, show=show) output_path_img = output_path.split('.')[0] + '.png' output_path_json = output_path.split('.')[0] + '.json' utils.draw_bounding_box_class(img, compo_merge, categories, output=output_path_img) utils.save_corners_json(output_path_json, compo_merge, categories) print('[%.3fs] %s' % (time.clock() - start, input_path_img))