From b4d168cae3bf02c4595c007b8937f8a5312ff5e9 Mon Sep 17 00:00:00 2001 From: cneud <952378+cneud@users.noreply.github.com> Date: Tue, 25 Mar 2025 23:54:31 +0100 Subject: [PATCH] pep8-e265: comment should start with `# ` --- src/eynollah/eynollah.py | 726 +++++++++++++-------------- src/eynollah/sbb_binarize.py | 4 +- src/eynollah/utils/__init__.py | 350 +++++++------ src/eynollah/utils/contour.py | 8 +- src/eynollah/utils/drop_capitals.py | 230 ++++----- src/eynollah/utils/marginals.py | 24 +- src/eynollah/utils/separate_lines.py | 146 +++--- src/eynollah/writer.py | 8 +- tests/base.py | 2 +- 9 files changed, 748 insertions(+), 750 deletions(-) diff --git a/src/eynollah/eynollah.py b/src/eynollah/eynollah.py index 535aee8..15e6a54 100644 --- a/src/eynollah/eynollah.py +++ b/src/eynollah/eynollah.py @@ -32,7 +32,7 @@ from scipy.signal import find_peaks from scipy.ndimage import gaussian_filter1d os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3" -#os.environ['CUDA_VISIBLE_DEVICES'] = '-1' +# os.environ['CUDA_VISIBLE_DEVICES'] = '-1' stderr = sys.stderr sys.stderr = open(os.devnull, "w") import tensorflow as tf @@ -105,14 +105,14 @@ from .writer import EynollahXmlWriter MIN_AREA_REGION = 0.000001 SLOPE_THRESHOLD = 0.13 -RATIO_OF_TWO_MODEL_THRESHOLD = 95.50 #98.45: +RATIO_OF_TWO_MODEL_THRESHOLD = 95.50 # 98.45: DPI_THRESHOLD = 298 MAX_SLOPE = 999 KERNEL = np.ones((5, 5), np.uint8) projection_dim = 64 patch_size = 1 -num_patches = 21 * 21 #14*14#28*28#14*14#28*28 +num_patches = 21 * 21 # 14*14#28*28#14*14#28*28 class Patches(layers.Layer): @@ -272,44 +272,44 @@ class Eynollah: self.model_dir_of_col_classifier = dir_models + "/eynollah-column-classifier_20210425" self.model_region_dir_p = dir_models + "/eynollah-main-regions-aug-scaling_20210425" self.model_region_dir_p2 = dir_models + "/eynollah-main-regions-aug-rotation_20210425" - #"/modelens_full_lay_1_3_031124" - #"/modelens_full_lay_13__3_19_241024" - #"/model_full_lay_13_241024" - #"/modelens_full_lay_13_17_231024" - #"/modelens_full_lay_1_2_221024" - #"/eynollah-full-regions-1column_20210425" + # "/modelens_full_lay_1_3_031124" + # "/modelens_full_lay_13__3_19_241024" + # "/model_full_lay_13_241024" + # "/modelens_full_lay_13_17_231024" + # "/modelens_full_lay_1_2_221024" + # "/eynollah-full-regions-1column_20210425" self.model_region_dir_fully_np = dir_models + "/modelens_full_lay_1__4_3_091124" - #self.model_region_dir_fully = dir_models + "/eynollah-full-regions-3+column_20210425" + # self.model_region_dir_fully = dir_models + "/eynollah-full-regions-3+column_20210425" self.model_page_dir = dir_models + "/eynollah-page-extraction_20210425" self.model_region_dir_p_ens = dir_models + "/eynollah-main-regions-ensembled_20210425" self.model_region_dir_p_ens_light = dir_models + "/eynollah-main-regions_20220314" self.model_region_dir_p_ens_light_only_images_extraction = dir_models + "/eynollah-main-regions_20231127_672_org_ens_11_13_16_17_18" self.model_reading_order_dir = dir_models + "/model_ens_reading_order_machine_based" - #"/modelens_12sp_elay_0_3_4__3_6_n" - #"/modelens_earlylayout_12spaltige_2_3_5_6_7_8" - #"/modelens_early12_sp_2_3_5_6_7_8_9_10_12_14_15_16_18" - #"/modelens_1_2_4_5_early_lay_1_2_spaltige" - #"/model_3_eraly_layout_no_patches_1_2_spaltige" + # "/modelens_12sp_elay_0_3_4__3_6_n" + # "/modelens_earlylayout_12spaltige_2_3_5_6_7_8" + # "/modelens_early12_sp_2_3_5_6_7_8_9_10_12_14_15_16_18" + # "/modelens_1_2_4_5_early_lay_1_2_spaltige" + # "/model_3_eraly_layout_no_patches_1_2_spaltige" self.model_region_dir_p_1_2_sp_np = dir_models + "/modelens_e_l_all_sp_0_1_2_3_4_171024" - ##self.model_region_dir_fully_new = dir_models + "/model_2_full_layout_new_trans" - #"/modelens_full_lay_1_3_031124" - #"/modelens_full_lay_13__3_19_241024" - #"/model_full_lay_13_241024" - #"/modelens_full_lay_13_17_231024" - #"/modelens_full_lay_1_2_221024" - #"/modelens_full_layout_24_till_28" - #"/model_2_full_layout_new_trans" + # self.model_region_dir_fully_new = dir_models + "/model_2_full_layout_new_trans" + # "/modelens_full_lay_1_3_031124" + # "/modelens_full_lay_13__3_19_241024" + # "/model_full_lay_13_241024" + # "/modelens_full_lay_13_17_231024" + # "/modelens_full_lay_1_2_221024" + # "/modelens_full_layout_24_till_28" + # "/model_2_full_layout_new_trans" self.model_region_dir_fully = dir_models + "/modelens_full_lay_1__4_3_091124" if self.textline_light: - #"/modelens_textline_1_4_16092024" - #"/model_textline_ens_3_4_5_6_artificial" - #"/modelens_textline_1_3_4_20240915" - #"/model_textline_ens_3_4_5_6_artificial" - #"/modelens_textline_9_12_13_14_15" - #"/eynollah-textline_light_20210425" + # "/modelens_textline_1_4_16092024" + # "/model_textline_ens_3_4_5_6_artificial" + # "/modelens_textline_1_3_4_20240915" + # "/model_textline_ens_3_4_5_6_artificial" + # "/modelens_textline_9_12_13_14_15" + # "/eynollah-textline_light_20210425" self.model_textline_dir = dir_models + "/modelens_textline_0_1__2_4_16092024" else: - #"/eynollah-textline_20210425" + # "/eynollah-textline_20210425" self.model_textline_dir = dir_models + "/modelens_textline_0_1__2_4_16092024" if self.ocr: self.model_ocr_dir = dir_models + "/trocr_model_ens_of_3_checkpoints_201124" @@ -343,7 +343,7 @@ class Eynollah: self.model_region = self.our_load_model(self.model_region_dir_p_ens) self.model_region_p2 = self.our_load_model(self.model_region_dir_p2) self.model_enhancement = self.our_load_model(self.model_dir_of_enhancement) - ###self.model_region_fl_new = self.our_load_model(self.model_region_dir_fully_new) + # self.model_region_fl_new = self.our_load_model(self.model_region_dir_fully_new) self.model_region_fl_np = self.our_load_model(self.model_region_dir_fully_np) self.model_region_fl = self.our_load_model(self.model_region_dir_fully) if self.reading_order_machine_based: @@ -351,7 +351,7 @@ class Eynollah: if self.ocr: self.model_ocr = VisionEncoderDecoderModel.from_pretrained(self.model_ocr_dir) self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") - #("microsoft/trocr-base-printed")#("microsoft/trocr-base-handwritten") + # ("microsoft/trocr-base-printed")# ("microsoft/trocr-base-handwritten") self.processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-handwritten") if self.tables: self.model_table = self.our_load_model(self.model_table_dir) @@ -550,7 +550,7 @@ class Eynollah: if label_p_pred[0][int(num_col - 1)] < 0.9 and img_w_new < width_early: img_new = np.copy(img) num_column_is_classified = False - #elif label_p_pred[0][int(num_col - 1)] < 0.8 and img_h_new >= 8000: + # elif label_p_pred[0][int(num_col - 1)] < 0.8 and img_h_new >= 8000: elif img_h_new >= 8000: img_new = np.copy(img) num_column_is_classified = False @@ -571,7 +571,7 @@ class Eynollah: if label_p_pred[0][int(num_col - 1)] < 0.9 and img_w_new < width_early: img_new = np.copy(img) num_column_is_classified = False - #elif label_p_pred[0][int(num_col - 1)] < 0.8 and img_h_new >= 8000: + # elif label_p_pred[0][int(num_col - 1)] < 0.8 and img_h_new >= 8000: elif img_h_new >= 8000: img_new = np.copy(img) num_column_is_classified = False @@ -826,9 +826,9 @@ class Eynollah: def start_new_session_and_model(self, model_dir): self.logger.debug("enter start_new_session_and_model (model_dir=%s)", model_dir) - #gpu_options = tf.compat.v1.GPUOptions(allow_growth=True) - #gpu_options = tf.compat.v1.GPUOptions(per_process_gpu_memory_fraction=7.7, allow_growth=True) - #session = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(gpu_options=gpu_options)) + # gpu_options = tf.compat.v1.GPUOptions(allow_growth=True) + # gpu_options = tf.compat.v1.GPUOptions(per_process_gpu_memory_fraction=7.7, allow_growth=True) + # session = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(gpu_options=gpu_options)) physical_devices = tf.config.list_physical_devices('GPU') try: for device in physical_devices: @@ -891,7 +891,7 @@ class Eynollah: width_mid = img_width_model - 2 * margin height_mid = img_height_model - 2 * margin img = img / 255. - #img = img.astype(np.float16) + # img = img.astype(np.float16) img_h = img.shape[0] img_w = img.shape[1] prediction_true = np.zeros((img_h, img_w, 3)) @@ -1049,7 +1049,7 @@ class Eynollah: img_patch[:] = 0 prediction_true = prediction_true.astype(np.uint8) - #del model + # del model gc.collect() return prediction_true @@ -1068,20 +1068,20 @@ class Eynollah: w_start = int(0.5 * w_diff) img_res = resize_image(img, h_n, w_n) - #label_res = resize_image(label, h_n, w_n) + # label_res = resize_image(label, h_n, w_n) img_scaled_padded = np.copy(img) - #label_scaled_padded = np.zeros(label.shape) + # label_scaled_padded = np.zeros(label.shape) img_scaled_padded[:, :, 0] = channel0_avg img_scaled_padded[:, :, 1] = channel1_avg img_scaled_padded[:, :, 2] = channel2_avg img_scaled_padded[h_start:h_start + h_n, w_start:w_start + w_n, :] = img_res[:, :, :] - #label_scaled_padded[h_start:h_start+h_n, w_start:w_start+w_n,:] = label_res[:,:,:] + # label_scaled_padded[h_start:h_start+h_n, w_start:w_start+w_n,:] = label_res[:,:,:] - return img_scaled_padded #, label_scaled_padded + return img_scaled_padded # , label_scaled_padded def do_prediction_new_concept_scatter_nd( self, patches, img, model, @@ -1103,10 +1103,10 @@ class Eynollah: seg = np.argmax(label_p_pred, axis=3)[0] if thresholding_for_artificial_class_in_light_version: - #seg_text = label_p_pred[0,:,:,1] - #seg_text[seg_text<0.2] =0 - #seg_text[seg_text>0] =1 - #seg[seg_text==1]=1 + # seg_text = label_p_pred[0,:,:,1] + # seg_text[seg_text<0.2] =0 + # seg_text[seg_text>0] =1 + # seg[seg_text==1]=1 seg_art = label_p_pred[0, :, :, 4] seg_art[seg_art < 0.2] = 0 @@ -1123,9 +1123,9 @@ class Eynollah: img = resize_image(img, img.shape[0], img_width_model) self.logger.debug("Patch size: %sx%s", img_height_model, img_width_model) - ##margin = int(marginal_of_patch_percent * img_height_model) - #width_mid = img_width_model - 2 * margin - #height_mid = img_height_model - 2 * margin + # margin = int(marginal_of_patch_percent * img_height_model) + # width_mid = img_width_model - 2 * margin + # height_mid = img_height_model - 2 * margin img = img / 255.0 img = img.astype(np.float16) img_h = img.shape[0] @@ -1203,10 +1203,10 @@ class Eynollah: seg = np.argmax(label_p_pred, axis=3)[0] if thresholding_for_artificial_class_in_light_version: - #seg_text = label_p_pred[0,:,:,1] - #seg_text[seg_text<0.2] =0 - #seg_text[seg_text>0] =1 - #seg[seg_text==1]=1 + # seg_text = label_p_pred[0,:,:,1] + # seg_text[seg_text<0.2] =0 + # seg_text[seg_text>0] =1 + # seg[seg_text==1]=1 seg_art = label_p_pred[0, :, :, 4] seg_art[seg_art < 0.2] = 0 @@ -1598,7 +1598,7 @@ class Eynollah: slope_deskew=slope_deskew, textline_light=self.textline_light, logger=self.logger, ), boxes, contours, contours_par, range(len(contours_par))) - #textline_polygons, boxes, text_regions, text_regions_par, box_coord, index_text_con, slopes = zip(*results) + # textline_polygons, boxes, text_regions, text_regions_par, box_coord, index_text_con, slopes = zip(*results) self.logger.debug("exit get_slopes_and_deskew_new_light") return tuple(zip(*results)) @@ -1616,7 +1616,7 @@ class Eynollah: logger=self.logger, plotter=self.plotter, ), boxes, contours, contours_par, range(len(contours_par))) - #textline_polygons, boxes, text_regions, text_regions_par, box_coord, index_text_con, slopes = zip(*results) + # textline_polygons, boxes, text_regions, text_regions_par, box_coord, index_text_con, slopes = zip(*results) self.logger.debug("exit get_slopes_and_deskew_new") return tuple(zip(*results)) @@ -1637,7 +1637,7 @@ class Eynollah: logger=self.logger, plotter=self.plotter, ), boxes, contours, contours_par, range(len(contours_par))) - #textline_polygons, boxes, text_regions, text_regions_par, box_coord, index_text_con, slopes = zip(*results) + # textline_polygons, boxes, text_regions, text_regions_par, box_coord, index_text_con, slopes = zip(*results) self.logger.debug("exit get_slopes_and_deskew_new_curved") return tuple(zip(*results)) @@ -1646,7 +1646,7 @@ class Eynollah: if not self.dir_in: self.model_textline, _ = self.start_new_session_and_model(self.model_textline_dir) - #img = img.astype(np.uint8) + # img = img.astype(np.uint8) img_org = np.copy(img) img_h = img_org.shape[0] img_w = img_org.shape[1] @@ -1656,10 +1656,10 @@ class Eynollah: use_patches, img, self.model_textline, marginal_of_patch_percent=0.15, n_batch_inference=3, thresholding_for_artificial_class_in_light_version=self.textline_light) - #if not self.textline_light: - #if num_col_classifier==1: - #prediction_textline_nopatch = self.do_prediction(False, img, self.model_textline) - #prediction_textline[:,:][prediction_textline_nopatch[:,:]==0] = 0 + # if not self.textline_light: + # if num_col_classifier==1: + # prediction_textline_nopatch = self.do_prediction(False, img, self.model_textline) + # prediction_textline[:,:][prediction_textline_nopatch[:,:]==0] = 0 prediction_textline = resize_image(prediction_textline, img_h, img_w) textline_mask_tot_ea_art = (prediction_textline[:, :] == 2) * 1 @@ -1667,7 +1667,7 @@ class Eynollah: old_art = np.copy(textline_mask_tot_ea_art) if not self.textline_light: textline_mask_tot_ea_art = textline_mask_tot_ea_art.astype('uint8') - #textline_mask_tot_ea_art = cv2.dilate(textline_mask_tot_ea_art, KERNEL, iterations=1) + # textline_mask_tot_ea_art = cv2.dilate(textline_mask_tot_ea_art, KERNEL, iterations=1) prediction_textline[:, :][textline_mask_tot_ea_art[:, :] == 1] = 2 textline_mask_tot_ea_lines = (prediction_textline[:, :] == 1) * 1 @@ -1780,15 +1780,15 @@ class Eynollah: text_regions_p_true[text_regions_p_true.shape[0] - 15:text_regions_p_true.shape[0], :] = 0 text_regions_p_true[:, text_regions_p_true.shape[1] - 15:text_regions_p_true.shape[1]] = 0 - ##polygons_of_images = return_contours_of_interested_region(text_regions_p_true, 2, 0.0001) + # polygons_of_images = return_contours_of_interested_region(text_regions_p_true, 2, 0.0001) polygons_of_images = return_contours_of_interested_region(text_regions_p_true, 2, 0.001) image_boundary_of_doc = np.zeros((text_regions_p_true.shape[0], text_regions_p_true.shape[1])) - ###image_boundary_of_doc[:6, :] = 1 - ###image_boundary_of_doc[text_regions_p_true.shape[0]-6:text_regions_p_true.shape[0], :] = 1 + # image_boundary_of_doc[:6, :] = 1 + # image_boundary_of_doc[text_regions_p_true.shape[0]-6:text_regions_p_true.shape[0], :] = 1 - ###image_boundary_of_doc[:, :6] = 1 - ###image_boundary_of_doc[:, text_regions_p_true.shape[1]-6:text_regions_p_true.shape[1]] = 1 + # image_boundary_of_doc[:, :6] = 1 + # image_boundary_of_doc[:, text_regions_p_true.shape[1]-6:text_regions_p_true.shape[1]] = 1 polygons_of_images_fin = [] for ploy_img_ind in polygons_of_images: @@ -1802,7 +1802,7 @@ class Eynollah: test_poly_image_intersected_area = test_poly_image_intersected_area.sum() if test_poly_image_intersected_area==0: - ##polygons_of_images_fin.append(ploy_img_ind) + # polygons_of_images_fin.append(ploy_img_ind) box = cv2.boundingRect(ploy_img_ind) _, page_coord_img = crop_image_inside_box(box, text_regions_p_true) @@ -1840,13 +1840,13 @@ class Eynollah: img_height_h = img_org.shape[0] img_width_h = img_org.shape[1] - #model_region, _ = self.start_new_session_and_model(self.model_region_dir_p_ens) - #print(num_col_classifier,'num_col_classifier') + # model_region, _ = self.start_new_session_and_model(self.model_region_dir_p_ens) + # print(num_col_classifier,'num_col_classifier') if num_col_classifier == 1: img_w_new = 1000 elif num_col_classifier == 2: - img_w_new = 1500 #1500 + img_w_new = 1500 # 1500 elif num_col_classifier == 3: img_w_new = 2000 elif num_col_classifier == 4: @@ -1859,26 +1859,26 @@ class Eynollah: img_resized = resize_image(img, img_h_new, img_w_new) t_bin = time.time() - #if (not self.input_binary) or self.full_layout: - #if self.input_binary: - #img_bin = np.copy(img_resized) - ###if (not self.input_binary and self.full_layout) or (not self.input_binary and num_col_classifier >= 30): - ###if not self.dir_in: - ###self.model_bin, _ = self.start_new_session_and_model(self.model_dir_of_binarization) - ###prediction_bin = self.do_prediction(True, img_resized, self.model_bin, n_batch_inference=5) - - ####print("inside bin ", time.time()-t_bin) - ###prediction_bin=prediction_bin[:,:,0] - ###prediction_bin = (prediction_bin[:,:]==0)*1 - ###prediction_bin = prediction_bin*255 - - ###prediction_bin =np.repeat(prediction_bin[:, :, np.newaxis], 3, axis=2) - - ###prediction_bin = prediction_bin.astype(np.uint16) - ####img= np.copy(prediction_bin) - ###img_bin = np.copy(prediction_bin) - ###else: - ###img_bin = np.copy(img_resized) + # if (not self.input_binary) or self.full_layout: + # if self.input_binary: + # img_bin = np.copy(img_resized) + # if (not self.input_binary and self.full_layout) or (not self.input_binary and num_col_classifier >= 30): + # if not self.dir_in: + # self.model_bin, _ = self.start_new_session_and_model(self.model_dir_of_binarization) + # prediction_bin = self.do_prediction(True, img_resized, self.model_bin, n_batch_inference=5) + + # print("inside bin ", time.time()-t_bin) + # prediction_bin=prediction_bin[:,:,0] + # prediction_bin = (prediction_bin[:,:]==0)*1 + # prediction_bin = prediction_bin*255 + + # prediction_bin =np.repeat(prediction_bin[:, :, np.newaxis], 3, axis=2) + + # prediction_bin = prediction_bin.astype(np.uint16) + # img= np.copy(prediction_bin) + # img_bin = np.copy(prediction_bin) + # else: + # img_bin = np.copy(img_resized) if self.ocr and not self.input_binary: if not self.dir_in: self.model_bin, _ = self.start_new_session_and_model(self.model_dir_of_binarization) @@ -1886,28 +1886,28 @@ class Eynollah: prediction_bin = 255 * (prediction_bin[:, :, 0] == 0) prediction_bin = np.repeat(prediction_bin[:, :, np.newaxis], 3, axis=2) prediction_bin = prediction_bin.astype(np.uint16) - #img= np.copy(prediction_bin) + # img= np.copy(prediction_bin) img_bin = np.copy(prediction_bin) else: img_bin = np.copy(img_resized) - #print("inside 1 ", time.time()-t_in) + # print("inside 1 ", time.time()-t_in) - ###textline_mask_tot_ea = self.run_textline(img_bin) + # textline_mask_tot_ea = self.run_textline(img_bin) self.logger.debug("detecting textlines on %s with %d colors", str(img_resized.shape), len(np.unique(img_resized))) textline_mask_tot_ea = self.run_textline(img_resized, num_col_classifier) textline_mask_tot_ea = resize_image(textline_mask_tot_ea, img_height_h, img_width_h) - #print(self.image_org.shape) - #cv2.imwrite('out_13.png', self.image_page_org_size) + # print(self.image_org.shape) + # cv2.imwrite('out_13.png', self.image_page_org_size) - #plt.imshwo(self.image_page_org_size) - #plt.show() + # plt.imshwo(self.image_page_org_size) + # plt.show() if not skip_layout_and_reading_order: - #print("inside 2 ", time.time()-t_in) + # print("inside 2 ", time.time()-t_in) if not self.dir_in: self.model_region_1_2, _ = self.start_new_session_and_model(self.model_region_dir_p_1_2_sp_np) - ##self.model_region, _ = self.start_new_session_and_model(self.model_region_dir_p_ens_light) + # self.model_region, _ = self.start_new_session_and_model(self.model_region_dir_p_ens_light) if num_col_classifier == 1 or num_col_classifier == 2: model_region, session_region = self.start_new_session_and_model(self.model_region_dir_p_1_2_sp_np) @@ -1934,10 +1934,10 @@ class Eynollah: prediction_regions_org = self.do_prediction_new_concept( True, img_resized, self.model_region_1_2, n_batch_inference=2, thresholding_for_some_classes_in_light_version=True) - ###prediction_regions_org = self.do_prediction(True, img_bin, self.model_region, n_batch_inference=3, thresholding_for_some_classes_in_light_version=True) - #print("inside 3 ", time.time()-t_in) - #plt.imshow(prediction_regions_org[:,:,0]) - #plt.show() + # prediction_regions_org = self.do_prediction(True, img_bin, self.model_region, n_batch_inference=3, thresholding_for_some_classes_in_light_version=True) + # print("inside 3 ", time.time()-t_in) + # plt.imshow(prediction_regions_org[:,:,0]) + # plt.show() prediction_regions_org = resize_image(prediction_regions_org, img_height_h, img_width_h) img_bin = resize_image(img_bin, img_height_h, img_width_h) @@ -1947,9 +1947,9 @@ class Eynollah: mask_texts_only = (prediction_regions_org[:, :] == 1) * 1 mask_texts_only = mask_texts_only.astype('uint8') - ##if num_col_classifier == 1 or num_col_classifier == 2: - ###mask_texts_only = cv2.erode(mask_texts_only, KERNEL, iterations=1) - ##mask_texts_only = cv2.dilate(mask_texts_only, KERNEL, iterations=1) + # if num_col_classifier == 1 or num_col_classifier == 2: + # mask_texts_only = cv2.erode(mask_texts_only, KERNEL, iterations=1) + # mask_texts_only = cv2.dilate(mask_texts_only, KERNEL, iterations=1) mask_texts_only = cv2.dilate(mask_texts_only, kernel=np.ones((2, 2), np.uint8), iterations=1) mask_images_only = (prediction_regions_org[:, :] == 2) * 1 @@ -1958,14 +1958,14 @@ class Eynollah: test_khat = np.zeros(prediction_regions_org.shape) test_khat = cv2.fillPoly(test_khat, pts=polygons_lines_xml, color=(1, 1, 1)) - #plt.imshow(test_khat[:,:]) - #plt.show() - #for jv in range(1): - #print(jv, hir_lines_xml[0][232][3]) - #test_khat = np.zeros(prediction_regions_org.shape) - #test_khat = cv2.fillPoly(test_khat, pts = [polygons_lines_xml[232]], color=(1,1,1)) - #plt.imshow(test_khat[:,:]) - #plt.show() + # plt.imshow(test_khat[:,:]) + # plt.show() + # for jv in range(1): + # print(jv, hir_lines_xml[0][232][3]) + # test_khat = np.zeros(prediction_regions_org.shape) + # test_khat = cv2.fillPoly(test_khat, pts = [polygons_lines_xml[232]], color=(1,1,1)) + # plt.imshow(test_khat[:,:]) + # plt.show() polygons_lines_xml = filter_contours_area_of_image( mask_lines_only, polygons_lines_xml, hir_lines_xml, max_area=1, min_area=0.00001) @@ -1973,12 +1973,12 @@ class Eynollah: test_khat = np.zeros(prediction_regions_org.shape) test_khat = cv2.fillPoly(test_khat, pts=polygons_lines_xml, color=(1, 1, 1)) - #plt.imshow(test_khat[:,:]) - #plt.show() - #sys.exit() + # plt.imshow(test_khat[:,:]) + # plt.show() + # sys.exit() polygons_of_only_texts = return_contours_of_interested_region(mask_texts_only, 1, 0.00001) - ##polygons_of_only_texts = self.dilate_textregions_contours(polygons_of_only_texts) + # polygons_of_only_texts = self.dilate_textregions_contours(polygons_of_only_texts) polygons_of_only_lines = return_contours_of_interested_region(mask_lines_only, 1, 0.00001) text_regions_p_true = np.zeros(prediction_regions_org.shape) @@ -1987,14 +1987,14 @@ class Eynollah: text_regions_p_true[:, :][mask_images_only[:, :] == 1] = 2 text_regions_p_true = cv2.fillPoly(text_regions_p_true, pts=polygons_of_only_texts, color=(1, 1, 1)) - #plt.imshow(textline_mask_tot_ea) - #plt.show() + # plt.imshow(textline_mask_tot_ea) + # plt.show() textline_mask_tot_ea[(text_regions_p_true == 0) | (text_regions_p_true == 4)] = 0 - #plt.imshow(textline_mask_tot_ea) - #plt.show() - #print("inside 4 ", time.time()-t_in) + # plt.imshow(textline_mask_tot_ea) + # plt.show() + # print("inside 4 ", time.time()-t_in) self.logger.debug("exit get_regions_light_v") return text_regions_p_true, erosion_hurts, polygons_lines_xml, textline_mask_tot_ea, img_bin else: @@ -2019,12 +2019,12 @@ class Eynollah: prediction_regions_org_y = self.do_prediction(True, img, self.model_region) prediction_regions_org_y = resize_image(prediction_regions_org_y, img_height_h, img_width_h) - #plt.imshow(prediction_regions_org_y[:,:,0]) - #plt.show() + # plt.imshow(prediction_regions_org_y[:,:,0]) + # plt.show() prediction_regions_org_y = prediction_regions_org_y[:, :, 0] mask_zeros_y = (prediction_regions_org_y[:, :] == 0) * 1 - ##img_only_regions_with_sep = ( (prediction_regions_org_y[:,:] != 3) & (prediction_regions_org_y[:,:] != 0) )*1 + # img_only_regions_with_sep = ( (prediction_regions_org_y[:,:] != 3) & (prediction_regions_org_y[:,:] != 0) )*1 img_only_regions_with_sep = (prediction_regions_org_y == 1).astype(np.uint8) try: img_only_regions = cv2.erode(img_only_regions_with_sep[:, :], KERNEL, iterations=20) @@ -2129,16 +2129,16 @@ class Eynollah: prediction_regions_org = resize_image(prediction_regions_org, img_height_h, img_width_h) prediction_regions_org = prediction_regions_org[:, :, 0] - #mask_lines_only=(prediction_regions_org[:,:]==3)*1 - #img = resize_image(img_org, int(img_org.shape[0]*1), int(img_org.shape[1]*1)) + # mask_lines_only=(prediction_regions_org[:,:]==3)*1 + # img = resize_image(img_org, int(img_org.shape[0]*1), int(img_org.shape[1]*1)) - #prediction_regions_org = self.do_prediction(True, img, self.model_region) + # prediction_regions_org = self.do_prediction(True, img, self.model_region) - #prediction_regions_org = resize_image(prediction_regions_org, img_height_h, img_width_h ) + # prediction_regions_org = resize_image(prediction_regions_org, img_height_h, img_width_h ) - #prediction_regions_org = prediction_regions_org[:,:,0] + # prediction_regions_org = prediction_regions_org[:,:,0] - #prediction_regions_org[(prediction_regions_org[:,:] == 1) & (mask_zeros_y[:,:] == 1)]=0 + # prediction_regions_org[(prediction_regions_org[:,:] == 1) & (mask_zeros_y[:,:] == 1)]=0 mask_lines_only = (prediction_regions_org == 3) * 1 mask_texts_only = (prediction_regions_org == 1) * 1 @@ -2285,7 +2285,7 @@ class Eynollah: args_contours = np.array(range(len(arg_text_con))) order_by_con_main = np.zeros(len(arg_text_con)) - ############################# head + # head arg_text_con_h = [] for ii in range(len(cx_text_only_h)): @@ -2550,7 +2550,7 @@ class Eynollah: color=(1, 1, 1)) table_pixels_masked_from_early_pre = only_recent_contour_image * table_prediction_early iou_in = 100. * table_pixels_masked_from_early_pre.sum() / only_recent_contour_image.sum() - #print(iou_in,'iou_in_in1') + # print(iou_in,'iou_in_in1') if iou_in > 30: layout_org = cv2.fillPoly(layout_org, pts=[contours_sep[ji]], color=3 * (pixel_table,)) @@ -2567,7 +2567,7 @@ class Eynollah: table_pixels_masked_from_early_pre = only_recent_contour_image * table_prediction_early iou_in = 100. * table_pixels_masked_from_early_pre.sum() / only_recent_contour_image.sum() - #print(iou_in,'iou_in') + # print(iou_in,'iou_in') if iou_in > 30: layout_org = cv2.fillPoly(layout_org, pts=[contours[i]], color=3 * (pixel_table,)) else: @@ -2830,8 +2830,8 @@ class Eynollah: prediction_table[:, :, 0][prediction_table_full_updown_erode[:, :] == 1] = 1 prediction_table = prediction_table.astype(np.int16) - #prediction_table_erode = cv2.erode(prediction_table[:,:,0], self.kernel, iterations=6) - #prediction_table_erode = cv2.dilate(prediction_table_erode, self.kernel, iterations=6) + # prediction_table_erode = cv2.erode(prediction_table[:,:,0], self.kernel, iterations=6) + # prediction_table_erode = cv2.dilate(prediction_table_erode, self.kernel, iterations=6) prediction_table_erode = cv2.erode(prediction_table[:, :, 0], KERNEL, iterations=20) prediction_table_erode = cv2.dilate(prediction_table_erode, KERNEL, iterations=20) @@ -2841,8 +2841,8 @@ class Eynollah: self, text_regions_p_1, textline_mask_tot_ea, num_col_classifier, num_column_is_classified, erosion_hurts, img_bin_light): - #print(text_regions_p_1.shape, 'text_regions_p_1 shape run graphics') - #print(erosion_hurts, 'erosion_hurts') + # print(text_regions_p_1.shape, 'text_regions_p_1 shape run graphics') + # print(erosion_hurts, 'erosion_hurts') t_in_gr = time.time() img_g = self.imread(grayscale=True, uint8=True) @@ -2853,7 +2853,7 @@ class Eynollah: img_g3[:, :, 2] = img_g[:, :] image_page, page_coord, cont_page = self.extract_page() - #print("inside graphics 1 ", time.time() - t_in_gr) + # print("inside graphics 1 ", time.time() - t_in_gr) if self.tables: table_prediction = self.get_tables_from_model(image_page, num_col_classifier) else: @@ -2874,16 +2874,16 @@ class Eynollah: img_only_regions_with_sep = ((text_regions_p_1[:, :] != 3) & (text_regions_p_1[:, :] != 0)) * 1 img_only_regions_with_sep = img_only_regions_with_sep.astype(np.uint8) - #print("inside graphics 2 ", time.time() - t_in_gr) + # print("inside graphics 2 ", time.time() - t_in_gr) if erosion_hurts: img_only_regions = np.copy(img_only_regions_with_sep[:, :]) else: img_only_regions = cv2.erode(img_only_regions_with_sep[:, :], KERNEL, iterations=6) - ##print(img_only_regions.shape,'img_only_regions') - ##plt.imshow(img_only_regions[:,:]) - ##plt.show() - ##num_col, _ = find_num_col(img_only_regions, num_col_classifier, self.tables, multiplier=6.0) + # print(img_only_regions.shape,'img_only_regions') + # plt.imshow(img_only_regions[:,:]) + # plt.show() + # num_col, _ = find_num_col(img_only_regions, num_col_classifier, self.tables, multiplier=6.0) try: num_col, _ = find_num_col(img_only_regions, num_col_classifier, self.tables, multiplier=6.0) num_col = num_col + 1 @@ -2892,13 +2892,13 @@ class Eynollah: except Exception as why: self.logger.error(why) num_col = None - #print("inside graphics 3 ", time.time() - t_in_gr) + # print("inside graphics 3 ", time.time() - t_in_gr) return (num_col, num_col_classifier, img_only_regions, page_coord, image_page, mask_images, mask_lines, text_regions_p_1, cont_page, table_prediction, textline_mask_tot_ea, img_bin_light) def run_graphics_and_columns_without_layout(self, textline_mask_tot_ea, img_bin_light): - #print(text_regions_p_1.shape, 'text_regions_p_1 shape run graphics') - #print(erosion_hurts, 'erosion_hurts') + # print(text_regions_p_1.shape, 'text_regions_p_1 shape run graphics') + # print(erosion_hurts, 'erosion_hurts') t_in_gr = time.time() img_g = self.imread(grayscale=True, uint8=True) @@ -2909,7 +2909,7 @@ class Eynollah: img_g3[:, :, 2] = img_g[:, :] image_page, page_coord, cont_page = self.extract_page() - #print("inside graphics 1 ", time.time() - t_in_gr) + # print("inside graphics 1 ", time.time() - t_in_gr) textline_mask_tot_ea = textline_mask_tot_ea[page_coord[0]: page_coord[1], page_coord[2]: page_coord[3]] img_bin_light = img_bin_light[page_coord[0]: page_coord[1], page_coord[2]: page_coord[3]] @@ -2972,7 +2972,7 @@ class Eynollah: scale = 1 if is_image_enhanced: if self.allow_enhancement: - #img_res = img_res.astype(np.uint8) + # img_res = img_res.astype(np.uint8) self.get_image_and_scales(img_org, img_res, scale) if self.plotter: self.plotter.save_enhanced_image(img_res) @@ -2987,13 +2987,13 @@ class Eynollah: img_org, img_res, is_image_enhanced = self.resize_image_with_column_classifier(is_image_enhanced, img_bin) self.get_image_and_scales_after_enhancing(img_org, img_res) - #print("enhancement in ", time.time()-t_in) + # print("enhancement in ", time.time()-t_in) return img_res, is_image_enhanced, num_col_classifier, num_column_is_classified def run_textline(self, image_page, num_col_classifier=None): - scaler_h_textline = 1 #1.3 # 1.2#1.2 - scaler_w_textline = 1 #1.3 # 0.9#1 - #print(image_page.shape) + scaler_h_textline = 1 # 1.3 # 1.2 # 1.2 + scaler_w_textline = 1 # 1.3 # 0.9 # 1 + # print(image_page.shape) textline_mask_tot_ea, _ = self.textline_contours(image_page, True, scaler_h_textline, scaler_w_textline, num_col_classifier) if self.textline_light: @@ -3004,7 +3004,7 @@ class Eynollah: return textline_mask_tot_ea def run_deskew(self, textline_mask_tot_ea): - #print(textline_mask_tot_ea.shape, 'textline_mask_tot_ea deskew') + # print(textline_mask_tot_ea.shape, 'textline_mask_tot_ea deskew') slope_deskew = return_deskew_slop(cv2.erode(textline_mask_tot_ea, KERNEL, iterations=2), 2, 30, True, map=self.executor.map, logger=self.logger, plotter=self.plotter) slope_first = 0 @@ -3059,7 +3059,7 @@ class Eynollah: regions_without_separators_d[table_prediction_n[:, :] == 1] = 1 regions_without_separators = (text_regions_p[:, :] == 1) * 1 # ( (text_regions_p[:,:]==1) | (text_regions_p[:,:]==2) )*1 #self.return_regions_without_separators_new(text_regions_p[:,:,0],img_only_regions) - #print(time.time()-t_0_box,'time box in 1') + # print(time.time()-t_0_box,'time box in 1') if self.tables: regions_without_separators[table_prediction == 1] = 1 if np.abs(slope_deskew) < SLOPE_THRESHOLD: @@ -3076,7 +3076,7 @@ class Eynollah: _, _, matrix_of_lines_ch_d, splitter_y_new_d, _ = find_number_of_columns_in_document( np.repeat(text_regions_p_1_n[:, :, np.newaxis], 3, axis=2), num_col_classifier, self.tables, pixel_lines) - #print(time.time()-t_0_box,'time box in 2') + # print(time.time()-t_0_box,'time box in 2') self.logger.info("num_col_classifier: %s", num_col_classifier) if num_col_classifier >= 3: @@ -3086,7 +3086,7 @@ class Eynollah: else: regions_without_separators_d = regions_without_separators_d.astype(np.uint8) regions_without_separators_d = cv2.erode(regions_without_separators_d[:, :], KERNEL, iterations=6) - #print(time.time()-t_0_box,'time box in 3') + # print(time.time()-t_0_box,'time box in 3') t1 = time.time() if np.abs(slope_deskew) < SLOPE_THRESHOLD: boxes, peaks_neg_tot_tables = return_boxes_of_images_by_order_of_reading_new( @@ -3094,7 +3094,7 @@ class Eynollah: num_col_classifier, erosion_hurts, self.tables, self.right2left) boxes_d = None self.logger.debug("len(boxes): %s", len(boxes)) - #print(time.time()-t_0_box,'time box in 3.1') + # print(time.time()-t_0_box,'time box in 3.1') if self.tables: if self.light_version: @@ -3106,10 +3106,10 @@ class Eynollah: img_revised_tab2 = self.add_tables_heuristic_to_layout( text_regions_p_tables, boxes, 0, splitter_y_new, peaks_neg_tot_tables, text_regions_p_tables, num_col_classifier, 0.000005, pixel_line) - #print(time.time()-t_0_box,'time box in 3.2') + # print(time.time()-t_0_box,'time box in 3.2') img_revised_tab2, contoures_tables = self.check_iou_of_bounding_box_and_contour_for_tables( img_revised_tab2, table_prediction, 10, num_col_classifier) - #print(time.time()-t_0_box,'time box in 3.3') + # print(time.time()-t_0_box,'time box in 3.3') else: boxes_d, peaks_neg_tot_tables_d = return_boxes_of_images_by_order_of_reading_new( splitter_y_new_d, regions_without_separators_d, matrix_of_lines_ch_d, @@ -3139,7 +3139,7 @@ class Eynollah: img_revised_tab2_d_rotated = img_revised_tab2_d_rotated.astype(np.int8) img_revised_tab2_d_rotated = resize_image(img_revised_tab2_d_rotated, text_regions_p.shape[0], text_regions_p.shape[1]) - #print(time.time()-t_0_box,'time box in 4') + # print(time.time()-t_0_box,'time box in 4') self.logger.info("detecting boxes took %.1fs", time.time() - t1) if self.tables: @@ -3159,7 +3159,7 @@ class Eynollah: text_regions_p[:, :][img_revised_tab[:, :] == 10] = 10 else: img_revised_tab = text_regions_p[:, :] - #img_revised_tab = text_regions_p[:, :] + # img_revised_tab = text_regions_p[:, :] if self.light_version: polygons_of_images = return_contours_of_interested_region(text_regions_p, 2) else: @@ -3178,7 +3178,7 @@ class Eynollah: pixel_img = 10 contours_tables = return_contours_of_interested_region(text_regions_p, pixel_img, min_area_mar) - #print(time.time()-t_0_box,'time box in 5') + # print(time.time()-t_0_box,'time box in 5') self.logger.debug('exit run_boxes_no_full_layout') return (polygons_of_images, img_revised_tab, text_regions_p_1_n, textline_mask_tot_d, regions_without_separators_d, boxes, boxes_d, @@ -3214,7 +3214,7 @@ class Eynollah: textline_mask_tot_d = None regions_without_separators_d = None # regions_without_separators = ( text_regions_p[:,:]==1 | text_regions_p[:,:]==2 )*1 - #self.return_regions_without_separators_new(text_regions_p[:,:,0],img_only_regions) + # self.return_regions_without_separators_new(text_regions_p[:,:,0],img_only_regions) regions_without_separators = (text_regions_p[:, :] == 1) * 1 regions_without_separators[table_prediction == 1] = 1 @@ -3239,7 +3239,7 @@ class Eynollah: regions_without_separators_d = None # regions_without_separators = ( text_regions_p[:,:]==1 | text_regions_p[:,:]==2 )*1 - #self.return_regions_without_separators_new(text_regions_p[:,:,0],img_only_regions) + # self.return_regions_without_separators_new(text_regions_p[:,:,0],img_only_regions) regions_without_separators = (text_regions_p[:, :] == 1) * 1 regions_without_separators[table_prediction == 1] = 1 @@ -3311,11 +3311,11 @@ class Eynollah: img_revised_tab[:, :][img_revised_tab[:, :] == 10] = 0 img_revised_tab[:, :][img_revised_tab2_d_rotated[:, :, 0] == 10] = 10 - ##img_revised_tab=img_revised_tab2[:,:,0] - #img_revised_tab=text_regions_p[:,:] + # img_revised_tab=img_revised_tab2[:,:,0] + # img_revised_tab=text_regions_p[:,:] text_regions_p[:, :][text_regions_p[:, :] == 10] = 0 text_regions_p[:, :][img_revised_tab[:, :] == 10] = 10 - #img_revised_tab[img_revised_tab2[:,:,0]==10] =10 + # img_revised_tab[img_revised_tab2[:,:,0]==10] =10 pixel_img = 4 min_area_mar = 0.00001 @@ -3338,21 +3338,21 @@ class Eynollah: text_regions_p[:, :][text_regions_p[:, :] == 4] = 8 image_page = image_page.astype(np.uint8) - #print("full inside 1", time.time()- t_full0) + # print("full inside 1", time.time()- t_full0) regions_fully, regions_fully_only_drop = self.extract_text_regions_new( img_bin_light if self.light_version else image_page, False, cols=num_col_classifier) - #print("full inside 2", time.time()- t_full0) + # print("full inside 2", time.time()- t_full0) # 6 is the separators lable in old full layout model # 4 is the drop capital class in old full layout model # in the new full layout drop capital is 3 and separators are 5 text_regions_p[:, :][regions_fully[:, :, 0] == 5] = 6 - ###regions_fully[:, :, 0][regions_fully_only_drop[:, :, 0] == 3] = 4 + # regions_fully[:, :, 0][regions_fully_only_drop[:, :, 0] == 3] = 4 - #text_regions_p[:,:][regions_fully[:,:,0]==6]=6 - ##regions_fully_only_drop = put_drop_out_from_only_drop_model(regions_fully_only_drop, text_regions_p) - ##regions_fully[:, :, 0][regions_fully_only_drop[:, :, 0] == 4] = 4 + # text_regions_p[:,:][regions_fully[:,:,0]==6]=6 + # regions_fully_only_drop = put_drop_out_from_only_drop_model(regions_fully_only_drop, text_regions_p) + # regions_fully[:, :, 0][regions_fully_only_drop[:, :, 0] == 4] = 4 drop_capital_label_in_full_layout_model = 3 drops = (regions_fully[:, :, 0] == drop_capital_label_in_full_layout_model) * 1 @@ -3365,20 +3365,20 @@ class Eynollah: regions_fully = putt_bb_of_drop_capitals_of_model_in_patches_in_layout( regions_fully, drop_capital_label_in_full_layout_model, text_regions_p) - ##regions_fully_np, _ = self.extract_text_regions(image_page, False, cols=num_col_classifier) - ##if num_col_classifier > 2: - ##regions_fully_np[:, :, 0][regions_fully_np[:, :, 0] == 4] = 0 - ##else: - ##regions_fully_np = filter_small_drop_capitals_from_no_patch_layout(regions_fully_np, text_regions_p) + # regions_fully_np, _ = self.extract_text_regions(image_page, False, cols=num_col_classifier) + # if num_col_classifier > 2: + # regions_fully_np[:, :, 0][regions_fully_np[:, :, 0] == 4] = 0 + # else: + # regions_fully_np = filter_small_drop_capitals_from_no_patch_layout(regions_fully_np, text_regions_p) - ###regions_fully = boosting_headers_by_longshot_region_segmentation(regions_fully, regions_fully_np, img_only_regions) + # regions_fully = boosting_headers_by_longshot_region_segmentation(regions_fully, regions_fully_np, img_only_regions) # plt.imshow(regions_fully[:,:,0]) # plt.show() text_regions_p[:, :][regions_fully[:, :, 0] == drop_capital_label_in_full_layout_model] = 4 - ####text_regions_p[:, :][regions_fully_np[:, :, 0] == 4] = 4 - #plt.imshow(text_regions_p) - #plt.show() - ####if not self.tables: + # text_regions_p[:, :][regions_fully_np[:, :, 0] == 4] = 4 + # plt.imshow(text_regions_p) + # plt.show() + # if not self.tables: if np.abs(slope_deskew) >= SLOPE_THRESHOLD: _, textline_mask_tot_d, text_regions_p_1_n, regions_fully_n = rotation_not_90_func_full_layout( image_page, textline_mask_tot, text_regions_p, regions_fully, slope_deskew) @@ -3398,7 +3398,7 @@ class Eynollah: polygons_of_images = return_contours_of_interested_region(img_revised_tab, 5) self.logger.debug('exit run_boxes_full_layout') - #print("full inside 3", time.time()- t_full0) + # print("full inside 3", time.time()- t_full0) return (polygons_of_images, img_revised_tab, text_regions_p_1_n, textline_mask_tot_d, regions_without_separators_d, regions_fully, regions_without_separators, polygons_of_marginals, contours_tables) @@ -3441,14 +3441,14 @@ class Eynollah: cv2.fillPoly(img, pts=[co_text_all[i]], color=(1,)) labels_con[:, :, i] = img - height1 = 672 #448 - width1 = 448 #224 + height1 = 672 # 448 + width1 = 448 # 224 - height2 = 672 #448 - width2 = 448 #224 + height2 = 672 # 448 + width2 = 448 # 224 - height3 = 672 #448 - width3 = 448 #224 + height3 = 672 # 448 + width3 = 448 # 224 labels_con = resize_image(labels_con.astype(np.uint8), height1, width1).astype(bool) img_header_and_sep = resize_image(img_header_and_sep, height1, width1) @@ -3461,7 +3461,7 @@ class Eynollah: input_1 = np.zeros((inference_bs, height1, width1, 3)) ordered = [list(range(len(co_text_all)))] index_update = 0 - #print(labels_con.shape[2],"number of regions for reading order") + # print(labels_con.shape[2],"number of regions for reading order") while index_update >= 0: ij_list = ordered.pop(index_update) i = ij_list.pop(0) @@ -3534,16 +3534,16 @@ class Eynollah: first_4_sorted = peaks_sort_4[argsort_sorted] y_4_sorted = sum_smoothed[peaks_real][arg_sort4[argsort_sorted]] - #print(first_4_sorted,'first_4_sorted') + # print(first_4_sorted,'first_4_sorted') arg_sortnew = np.argsort(y_4_sorted) peaks_final = np.sort(first_4_sorted[arg_sortnew][2:]) - #plt.figure(ind_tot) - #plt.imshow(textline_image) - #plt.plot([peaks_final[0], peaks_final[0]], [0, height-1]) - #plt.plot([peaks_final[1], peaks_final[1]], [0, height-1]) - #plt.savefig('./'+str(ind_tot)+'.png') + # plt.figure(ind_tot) + # plt.imshow(textline_image) + # plt.plot([peaks_final[0], peaks_final[0]], [0, height-1]) + # plt.plot([peaks_final[1], peaks_final[1]], [0, height-1]) + # plt.savefig('./'+str(ind_tot)+'.png') return peaks_final[0], peaks_final[1] else: @@ -3562,18 +3562,18 @@ class Eynollah: peaks_real, _ = find_peaks(sum_smoothed, height=0) if len(peaks_real) > 70: - #print(len(peaks_real), 'len(peaks_real)') + # print(len(peaks_real), 'len(peaks_real)') peaks_real = peaks_real[(peaks_real < width2) & (peaks_real > width1)] arg_max = np.argmax(sum_smoothed[peaks_real]) peaks_final = peaks_real[arg_max] - #plt.figure(ind_tot) - #plt.imshow(textline_image) - #plt.plot([peaks_final, peaks_final], [0, height-1]) - ##plt.plot([peaks_final[1], peaks_final[1]], [0, height-1]) - #plt.savefig('./'+str(ind_tot)+'.png') + # plt.figure(ind_tot) + # plt.imshow(textline_image) + # plt.plot([peaks_final, peaks_final], [0, height-1]) + # plt.plot([peaks_final[1], peaks_final[1]], [0, height-1]) + # plt.savefig('./'+str(ind_tot)+'.png') return peaks_final else: @@ -3591,7 +3591,7 @@ class Eynollah: first_4_sorted = peaks_sort_4[argsort_sorted] y_4_sorted = sum_smoothed[peaks_real][arg_sort4[argsort_sorted]] - #print(first_4_sorted,'first_4_sorted') + # print(first_4_sorted,'first_4_sorted') arg_sortnew = np.argsort(y_4_sorted) peaks_final = np.sort(first_4_sorted[arg_sortnew][3:]) @@ -3616,11 +3616,11 @@ class Eynollah: peak_end = self.return_start_and_end_of_common_text_of_textline_ocr_new_splitted( peaks_real, sum_smoothed, mid - 2, width2) - #plt.figure(ind_tot) - #plt.imshow(textline_image) - #plt.plot([peak_start, peak_start], [0, height-1]) - #plt.plot([peak_end, peak_end], [0, height-1]) - #plt.savefig('./'+str(ind_tot)+'.png') + # plt.figure(ind_tot) + # plt.imshow(textline_image) + # plt.plot([peak_start, peak_start], [0, height-1]) + # plt.plot([peak_end, peak_end], [0, height-1]) + # plt.savefig('./'+str(ind_tot)+'.png') return peak_start, peak_end else: @@ -3634,46 +3634,46 @@ class Eynollah: generated_ids = model_ocr.generate(pixel_values.to(device)) generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] else: - #width = np.shape(textline_image)[1] - #height = np.shape(textline_image)[0] - #common_window = int(0.3*width) - #width1 = int ( width/2. - common_window ) - #width2 = int ( width/2. + common_window ) + # width = np.shape(textline_image)[1] + # height = np.shape(textline_image)[0] + # common_window = int(0.3*width) + # width1 = int ( width/2. - common_window ) + # width2 = int ( width/2. + common_window ) split_point = self.return_start_and_end_of_common_text_of_textline_ocr_without_common_section( textline_image, ind_tot) if split_point: image1 = textline_image[:, :split_point, :] # image.crop((0, 0, width2, height)) - image2 = textline_image[:, split_point:, :] #image.crop((width1, 0, width, height)) + image2 = textline_image[:, split_point:, :] # image.crop((width1, 0, width, height)) - #pixel_values1 = processor(image1, return_tensors="pt").pixel_values - #pixel_values2 = processor(image2, return_tensors="pt").pixel_values + # pixel_values1 = processor(image1, return_tensors="pt").pixel_values + # pixel_values2 = processor(image2, return_tensors="pt").pixel_values pixel_values_merged = processor([image1, image2], return_tensors="pt").pixel_values generated_ids_merged = model_ocr.generate(pixel_values_merged.to(device)) generated_text_merged = processor.batch_decode(generated_ids_merged, skip_special_tokens=True) - #print(generated_text_merged,'generated_text_merged') + # print(generated_text_merged,'generated_text_merged') - #generated_ids1 = model_ocr.generate(pixel_values1.to(device)) - #generated_ids2 = model_ocr.generate(pixel_values2.to(device)) + # generated_ids1 = model_ocr.generate(pixel_values1.to(device)) + # generated_ids2 = model_ocr.generate(pixel_values2.to(device)) - #generated_text1 = processor.batch_decode(generated_ids1, skip_special_tokens=True)[0] - #generated_text2 = processor.batch_decode(generated_ids2, skip_special_tokens=True)[0] + # generated_text1 = processor.batch_decode(generated_ids1, skip_special_tokens=True)[0] + # generated_text2 = processor.batch_decode(generated_ids2, skip_special_tokens=True)[0] - #generated_text = generated_text1 + ' ' + generated_text2 + # generated_text = generated_text1 + ' ' + generated_text2 generated_text = generated_text_merged[0] + ' ' + generated_text_merged[1] - #print(generated_text1,'generated_text1') - #print(generated_text2, 'generated_text2') - #print('########################################') + # print(generated_text1,'generated_text1') + # print(generated_text2, 'generated_text2') + # print('##########') else: pixel_values = processor(textline_image, return_tensors="pt").pixel_values generated_ids = model_ocr.generate(pixel_values.to(device)) generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] - #print(generated_text,'generated_text') - #print('########################################') + # print(generated_text,'generated_text') + # print('##########') return generated_text def return_ocr_of_textline( @@ -3684,17 +3684,17 @@ class Eynollah: generated_ids = model_ocr.generate(pixel_values.to(device)) generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] else: - #width = np.shape(textline_image)[1] - #height = np.shape(textline_image)[0] - #common_window = int(0.3*width) - #width1 = int ( width/2. - common_window ) - #width2 = int ( width/2. + common_window ) + # width = np.shape(textline_image)[1] + # height = np.shape(textline_image)[0] + # common_window = int(0.3*width) + # width1 = int ( width/2. - common_window ) + # width2 = int ( width/2. + common_window ) try: width1, width2 = self.return_start_and_end_of_common_text_of_textline_ocr_new(textline_image, ind_tot) image1 = textline_image[:, :width2, :] # image.crop((0, 0, width2, height)) - image2 = textline_image[:, width1:, :] #image.crop((width1, 0, width, height)) + image2 = textline_image[:, width1:, :] # image.crop((width1, 0, width, height)) pixel_values1 = processor(image1, return_tensors="pt").pixel_values pixel_values2 = processor(image2, return_tensors="pt").pixel_values @@ -3704,9 +3704,9 @@ class Eynollah: generated_text1 = processor.batch_decode(generated_ids1, skip_special_tokens=True)[0] generated_text2 = processor.batch_decode(generated_ids2, skip_special_tokens=True)[0] - #print(generated_text1,'generated_text1') - #print(generated_text2, 'generated_text2') - #print('########################################') + # print(generated_text1,'generated_text1') + # print(generated_text2, 'generated_text2') + # print('##########') match = sq(None, generated_text1, generated_text2).find_longest_match( 0, len(generated_text1), 0, len(generated_text2)) @@ -3752,7 +3752,7 @@ class Eynollah: return x_differential_new def dilate_textregions_contours_textline_version(self, all_found_textline_polygons): - #print(all_found_textline_polygons) + # print(all_found_textline_polygons) for j in range(len(all_found_textline_polygons)): for ij in range(len(all_found_textline_polygons[j])): con_ind = all_found_textline_polygons[j][ij] @@ -3788,7 +3788,7 @@ class Eynollah: dilation_m1 = 8 if dilation_m1 < 6: dilation_m1 = 6 - #print(dilation_m1, 'dilation_m1') + # print(dilation_m1, 'dilation_m1') dilation_m1 = 6 dilation_m2 = int(dilation_m1 / 2.) + 1 @@ -3830,7 +3830,7 @@ class Eynollah: results = [cv2.pointPolygonTest(con_ind, (con_scaled[ind, 0, 0], con_scaled[ind, 0, 1]), False) for ind in range(len(con_scaled[:, 0, 1]))] results = np.array(results) - #print(results,'results') + # print(results,'results') results[results == 0] = 1 diff_result = np.diff(results) @@ -3841,7 +3841,7 @@ class Eynollah: if results[0] == 1: con_scaled[:indices_m2[0] + 1, 0, 1] = con_ind[:indices_m2[0] + 1, 0, 1] con_scaled[:indices_m2[0] + 1, 0, 0] = con_ind[:indices_m2[0] + 1, 0, 0] - #indices_2 = indices_2[1:] + # indices_2 = indices_2[1:] indices_m2 = indices_m2[1:] if len(indices_2) > len(indices_m2): @@ -3858,10 +3858,10 @@ class Eynollah: return all_found_textline_polygons def dilate_textregions_contours(self, all_found_textline_polygons): - #print(all_found_textline_polygons) + # print(all_found_textline_polygons) for j in range(len(all_found_textline_polygons)): con_ind = all_found_textline_polygons[j] - #print(len(con_ind[:,0,0]),'con_ind[:,0,0]') + # print(len(con_ind[:,0,0]),'con_ind[:,0,0]') area = cv2.contourArea(con_ind) con_ind = con_ind.astype(np.float) @@ -3894,7 +3894,7 @@ class Eynollah: dilation_m1 = 8 if dilation_m1 < 6: dilation_m1 = 6 - #print(dilation_m1, 'dilation_m1') + # print(dilation_m1, 'dilation_m1') dilation_m1 = 6 dilation_m2 = int(dilation_m1 / 2.) + 1 @@ -3934,7 +3934,7 @@ class Eynollah: results = [cv2.pointPolygonTest(con_ind, (con_scaled[ind, 0, 0], con_scaled[ind, 0, 1]), False) for ind in range(len(con_scaled[:, 0, 1]))] results = np.array(results) - #print(results,'results') + # print(results,'results') results[results == 0] = 1 diff_result = np.diff(results) @@ -3944,7 +3944,7 @@ class Eynollah: if results[0] == 1: con_scaled[:indices_m2[0] + 1, 0, 1] = con_ind[:indices_m2[0] + 1, 0, 1] con_scaled[:indices_m2[0] + 1, 0, 0] = con_ind[:indices_m2[0] + 1, 0, 0] - #indices_2 = indices_2[1:] + # indices_2 = indices_2[1:] indices_m2 = indices_m2[1:] if len(indices_2) > len(indices_m2): @@ -3997,7 +3997,7 @@ class Eynollah: dilation_m1 = 12 if dilation_m1 < 4: dilation_m1 = 4 - #print(dilation_m1, 'dilation_m1') + # print(dilation_m1, 'dilation_m1') dilation_m2 = int(dilation_m1 / 2.) + 1 for i in range(len(x_differential)): @@ -4075,7 +4075,7 @@ class Eynollah: contours_index_small = [ind for ind in range(len(contours)) if areas_ratio[ind] < 1e-3] contours_index_big = [ind for ind in range(len(contours)) if areas_ratio[ind] >= 1e-3] - #contours_> = [contours[ind] for ind in contours_index_big] + # contours_> = [contours[ind] for ind in contours_index_big] indexes_to_be_removed = [] for ind_small in contours_index_small: results = [cv2.pointPolygonTest(contours[ind], (cx_main[ind_small], cy_main[ind_small]), False) @@ -4112,7 +4112,7 @@ class Eynollah: ind_textline_inside_tr = list(range(len(contours[jj]))) index_textline_inside_textregion = index_textline_inside_textregion + ind_textline_inside_tr - #ind_ins = [0] * len(contours[jj]) + jj + # ind_ins = [0] * len(contours[jj]) + jj ind_ins = np.zeros(len(contours[jj])) + jj list_ind_ins = list(ind_ins) indexes_of_textline_tot = indexes_of_textline_tot + list_ind_ins @@ -4142,10 +4142,10 @@ class Eynollah: for ind in args_with_bigger_area] results = np.array(results) if np.any(results == 1): - #print(indexes_of_textline_tot[ij], index_textline_inside_textregion[ij]) + # print(indexes_of_textline_tot[ij], index_textline_inside_textregion[ij]) textregion_index_to_del.append(int(indexes_of_textline_tot[ij])) textline_in_textregion_index_to_del.append(int(index_textline_inside_textregion[ij])) - #contours[int(indexes_of_textline_tot[ij])].pop(int(index_textline_inside_textregion[ij])) + # contours[int(indexes_of_textline_tot[ij])].pop(int(index_textline_inside_textregion[ij])) textregion_index_to_del = np.array(textregion_index_to_del) textline_in_textregion_index_to_del = np.array(textline_in_textregion_index_to_del) @@ -4161,28 +4161,28 @@ class Eynollah: def filter_contours_without_textline_inside( self, contours, text_con_org, contours_textline, contours_only_text_parent_d_ordered): - ###contours_txtline_of_all_textregions = [] - ###for jj in range(len(contours_textline)): - ###contours_txtline_of_all_textregions = contours_txtline_of_all_textregions + contours_textline[jj] - - ###M_main_textline = [cv2.moments(contours_txtline_of_all_textregions[j]) - ### for j in range(len(contours_txtline_of_all_textregions))] - ###cx_main_textline = [(M_main_textline[j]["m10"] / (M_main_textline[j]["m00"] + 1e-32)) - ### for j in range(len(M_main_textline))] - ###cy_main_textline = [(M_main_textline[j]["m01"] / (M_main_textline[j]["m00"] + 1e-32)) - ### for j in range(len(M_main_textline))] - - ###M_main = [cv2.moments(contours[j]) for j in range(len(contours))] - ###cx_main = [(M_main[j]["m10"] / (M_main[j]["m00"] + 1e-32)) for j in range(len(M_main))] - ###cy_main = [(M_main[j]["m01"] / (M_main[j]["m00"] + 1e-32)) for j in range(len(M_main))] - - ###contours_with_textline = [] - ###for ind_tr, con_tr in enumerate(contours): - ###results = [cv2.pointPolygonTest(con_tr, (cx_main_textline[index_textline_con], cy_main_textline[index_textline_con]), False) - ### for index_textline_con in range(len(contours_txtline_of_all_textregions)) ] - ###results = np.array(results) - ###if np.any(results==1): - ###contours_with_textline.append(con_tr) + # contours_txtline_of_all_textregions = [] + # for jj in range(len(contours_textline)): + # contours_txtline_of_all_textregions = contours_txtline_of_all_textregions + contours_textline[jj] + + # M_main_textline = [cv2.moments(contours_txtline_of_all_textregions[j]) + # for j in range(len(contours_txtline_of_all_textregions))] + # cx_main_textline = [(M_main_textline[j]["m10"] / (M_main_textline[j]["m00"] + 1e-32)) + # for j in range(len(M_main_textline))] + # cy_main_textline = [(M_main_textline[j]["m01"] / (M_main_textline[j]["m00"] + 1e-32)) + # for j in range(len(M_main_textline))] + + # M_main = [cv2.moments(contours[j]) for j in range(len(contours))] + # cx_main = [(M_main[j]["m10"] / (M_main[j]["m00"] + 1e-32)) for j in range(len(M_main))] + # cy_main = [(M_main[j]["m01"] / (M_main[j]["m00"] + 1e-32)) for j in range(len(M_main))] + + # contours_with_textline = [] + # for ind_tr, con_tr in enumerate(contours): + # results = [cv2.pointPolygonTest(con_tr, (cx_main_textline[index_textline_con], cy_main_textline[index_textline_con]), False) + # for index_textline_con in range(len(contours_txtline_of_all_textregions)) ] + # results = np.array(results) + # if np.any(results==1): + # contours_with_textline.append(con_tr) textregion_index_to_del = [] for index_textregion, textlines_textregion in enumerate(contours_textline): @@ -4362,7 +4362,7 @@ class Eynollah: t0 = time.time() if self.dir_in: self.reset_file_name_dir(os.path.join(self.dir_in, img_name)) - #print("text region early -11 in %.1fs", time.time() - t0) + # print("text region early -11 in %.1fs", time.time() - t0) if os.path.exists(self.writer.output_filename): if self.overwrite: @@ -4399,7 +4399,7 @@ class Eynollah: page_coord, image_page, textline_mask_tot_ea, img_bin_light, cont_page = \ self.run_graphics_and_columns_without_layout(textline_mask_tot_ea, img_bin_light) - ##all_found_textline_polygons =self.scale_contours_new(textline_mask_tot_ea) + # all_found_textline_polygons =self.scale_contours_new(textline_mask_tot_ea) cnt_clean_rot_raw, hir_on_cnt_clean_rot = return_contours_of_image(textline_mask_tot_ea) all_found_textline_polygons = filter_contours_area_of_image( @@ -4435,12 +4435,12 @@ class Eynollah: else: return pcgts - #print("text region early -1 in %.1fs", time.time() - t0) + # print("text region early -1 in %.1fs", time.time() - t0) t1 = time.time() if self.light_version: text_regions_p_1, erosion_hurts, polygons_lines_xml, textline_mask_tot_ea, img_bin_light = \ self.get_regions_light_v(img_res, is_image_enhanced, num_col_classifier) - #print("text region early -2 in %.1fs", time.time() - t0) + # print("text region early -2 in %.1fs", time.time() - t0) if num_col_classifier == 1 or num_col_classifier == 2: if num_col_classifier == 1: @@ -4454,17 +4454,17 @@ class Eynollah: slope_deskew, slope_first = self.run_deskew(textline_mask_tot_ea_deskew) else: slope_deskew, slope_first = self.run_deskew(textline_mask_tot_ea) - #print("text region early -2,5 in %.1fs", time.time() - t0) - #self.logger.info("Textregion detection took %.1fs ", time.time() - t1t) + # print("text region early -2,5 in %.1fs", time.time() - t0) + # self.logger.info("Textregion detection took %.1fs ", time.time() - t1t) num_col, num_col_classifier, img_only_regions, page_coord, image_page, mask_images, mask_lines, \ text_regions_p_1, cont_page, table_prediction, textline_mask_tot_ea, img_bin_light = \ self.run_graphics_and_columns_light(text_regions_p_1, textline_mask_tot_ea, num_col_classifier, num_column_is_classified, erosion_hurts, img_bin_light) - #self.logger.info("run graphics %.1fs ", time.time() - t1t) - #print("text region early -3 in %.1fs", time.time() - t0) + # self.logger.info("run graphics %.1fs ", time.time() - t1t) + # print("text region early -3 in %.1fs", time.time() - t0) textline_mask_tot_ea_org = np.copy(textline_mask_tot_ea) - #print("text region early -4 in %.1fs", time.time() - t0) + # print("text region early -4 in %.1fs", time.time() - t0) else: text_regions_p_1, erosion_hurts, polygons_lines_xml = \ self.get_regions_from_xy_2models(img_res, is_image_enhanced, @@ -4477,9 +4477,9 @@ class Eynollah: self.run_graphics_and_columns(text_regions_p_1, num_col_classifier, num_column_is_classified, erosion_hurts) self.logger.info("Graphics detection took %.1fs ", time.time() - t1) - #self.logger.info('cont_page %s', cont_page) - #plt.imshow(table_prediction) - #plt.show() + # self.logger.info('cont_page %s', cont_page) + # plt.imshow(table_prediction) + # plt.show() if not num_col: self.logger.info("No columns detected, outputting an empty PAGE-XML") @@ -4494,7 +4494,7 @@ class Eynollah: else: return pcgts - #print("text region early in %.1fs", time.time() - t0) + # print("text region early in %.1fs", time.time() - t0) t1 = time.time() if not self.light_version: textline_mask_tot_ea = self.run_textline(image_page) @@ -4532,22 +4532,22 @@ class Eynollah: image_page_rotated = resize_image(image_page_rotated, org_h_l_m, org_w_l_m) self.logger.info("detection of marginals took %.1fs", time.time() - t1) - #print("text region early 2 marginal in %.1fs", time.time() - t0) - ## birdan sora chock chakir + # print("text region early 2 marginal in %.1fs", time.time() - t0) + # birdan sora chock chakir t1 = time.time() if not self.full_layout: polygons_of_images, img_revised_tab, text_regions_p_1_n, textline_mask_tot_d, regions_without_separators_d, \ boxes, boxes_d, polygons_of_marginals, contours_tables = \ self.run_boxes_no_full_layout(image_page, textline_mask_tot, text_regions_p, slope_deskew, num_col_classifier, table_prediction, erosion_hurts) - ###polygons_of_marginals = self.dilate_textregions_contours(polygons_of_marginals) + # polygons_of_marginals = self.dilate_textregions_contours(polygons_of_marginals) else: polygons_of_images, img_revised_tab, text_regions_p_1_n, textline_mask_tot_d, regions_without_separators_d, \ regions_fully, regions_without_separators, polygons_of_marginals, contours_tables = \ self.run_boxes_full_layout(image_page, textline_mask_tot, text_regions_p, slope_deskew, num_col_classifier, img_only_regions, table_prediction, erosion_hurts, img_bin_light if self.light_version else None) - ###polygons_of_marginals = self.dilate_textregions_contours(polygons_of_marginals) + # polygons_of_marginals = self.dilate_textregions_contours(polygons_of_marginals) if self.light_version: drop_label_in_full_layout = 4 textline_mask_tot_ea_org[img_revised_tab == drop_label_in_full_layout] = 0 @@ -4556,14 +4556,14 @@ class Eynollah: if np.abs(slope_deskew) >= SLOPE_THRESHOLD: text_only_d = (text_regions_p_1_n[:, :] == 1) * 1 - #print("text region early 2 in %.1fs", time.time() - t0) - ###min_con_area = 0.000005 + # print("text region early 2 in %.1fs", time.time() - t0) + # min_con_area = 0.000005 contours_only_text, hir_on_text = return_contours_of_image(text_only) contours_only_text_parent = return_parent_contours(contours_only_text, hir_on_text) if len(contours_only_text_parent) > 0: areas_cnt_text = np.array([cv2.contourArea(c) for c in contours_only_text_parent]) areas_cnt_text = areas_cnt_text / float(text_only.shape[0] * text_only.shape[1]) - #self.logger.info('areas_cnt_text %s', areas_cnt_text) + # self.logger.info('areas_cnt_text %s', areas_cnt_text) contours_biggest = contours_only_text_parent[np.argmax(areas_cnt_text)] contours_only_text_parent = [c for jz, c in enumerate(contours_only_text_parent) if areas_cnt_text[jz] > MIN_AREA_REGION] @@ -4573,13 +4573,13 @@ class Eynollah: contours_only_text_parent = self.return_list_of_contours_with_desired_order( contours_only_text_parent, index_con_parents) - ##try: - ##contours_only_text_parent = \ - ##list(np.array(contours_only_text_parent,dtype=object)[index_con_parents]) - ##except: - ##contours_only_text_parent = \ - ##list(np.array(contours_only_text_parent,dtype=np.int32)[index_con_parents]) - ##areas_cnt_text_parent = list(np.array(areas_cnt_text_parent)[index_con_parents]) + # try: + # contours_only_text_parent = \ + # list(np.array(contours_only_text_parent,dtype=object)[index_con_parents]) + # except: + # contours_only_text_parent = \ + # list(np.array(contours_only_text_parent,dtype=np.int32)[index_con_parents]) + # areas_cnt_text_parent = list(np.array(areas_cnt_text_parent)[index_con_parents]) areas_cnt_text_parent = self.return_list_of_contours_with_desired_order( areas_cnt_text_parent, index_con_parents) @@ -4598,13 +4598,13 @@ class Eynollah: index_con_parents_d = np.argsort(areas_cnt_text_d) contours_only_text_parent_d = self.return_list_of_contours_with_desired_order( contours_only_text_parent_d, index_con_parents_d) - #try: - #contours_only_text_parent_d = \ - #list(np.array(contours_only_text_parent_d,dtype=object)[index_con_parents_d]) - #except: - #contours_only_text_parent_d = \ - #list(np.array(contours_only_text_parent_d,dtype=np.int32)[index_con_parents_d]) - #areas_cnt_text_d = list(np.array(areas_cnt_text_d)[index_con_parents_d]) + # try: + # contours_only_text_parent_d = \ + # list(np.array(contours_only_text_parent_d,dtype=object)[index_con_parents_d]) + # except: + # contours_only_text_parent_d = \ + # list(np.array(contours_only_text_parent_d,dtype=np.int32)[index_con_parents_d]) + # areas_cnt_text_d = list(np.array(areas_cnt_text_d)[index_con_parents_d]) areas_cnt_text_d = self.return_list_of_contours_with_desired_order( areas_cnt_text_d, index_con_parents_d) @@ -4662,7 +4662,7 @@ class Eynollah: else: contours_only_text_parent_d_ordered = [] contours_only_text_parent_d = [] - #contours_only_text_parent = [] + # contours_only_text_parent = [] if not len(contours_only_text_parent): # stop early empty_marginals = [[]] * len(polygons_of_marginals) @@ -4685,25 +4685,25 @@ class Eynollah: else: return pcgts - #print("text region early 3 in %.1fs", time.time() - t0) + # print("text region early 3 in %.1fs", time.time() - t0) if self.light_version: contours_only_text_parent = self.dilate_textregions_contours( contours_only_text_parent) contours_only_text_parent = self.filter_contours_inside_a_bigger_one( contours_only_text_parent, text_only, marginal_cnts=polygons_of_marginals) - #print("text region early 3.5 in %.1fs", time.time() - t0) + # print("text region early 3.5 in %.1fs", time.time() - t0) txt_con_org = get_textregion_contours_in_org_image_light( contours_only_text_parent, self.image, slope_first, map=self.executor.map) - #txt_con_org = self.dilate_textregions_contours(txt_con_org) - #contours_only_text_parent = self.dilate_textregions_contours(contours_only_text_parent) + # txt_con_org = self.dilate_textregions_contours(txt_con_org) + # contours_only_text_parent = self.dilate_textregions_contours(contours_only_text_parent) else: txt_con_org = get_textregion_contours_in_org_image( contours_only_text_parent, self.image, slope_first) - #print("text region early 4 in %.1fs", time.time() - t0) + # print("text region early 4 in %.1fs", time.time() - t0) boxes_text, _ = get_text_region_boxes_by_given_contours(contours_only_text_parent) boxes_marginals, _ = get_text_region_boxes_by_given_contours(polygons_of_marginals) - #print("text region early 5 in %.1fs", time.time() - t0) - ## birdan sora chock chakir + # print("text region early 5 in %.1fs", time.time() - t0) + # birdan sora chock chakir if not self.curved_line: if self.light_version: if self.textline_light: @@ -4716,14 +4716,14 @@ class Eynollah: polygons_of_marginals, polygons_of_marginals, textline_mask_tot_ea_org, image_page_rotated, boxes_marginals, slope_deskew) - #slopes, all_found_textline_polygons, boxes_text, txt_con_org, contours_only_text_parent, index_by_text_par_con = \ + # slopes, all_found_textline_polygons, boxes_text, txt_con_org, contours_only_text_parent, index_by_text_par_con = \ # self.delete_regions_without_textlines(slopes, all_found_textline_polygons, # boxes_text, txt_con_org, contours_only_text_parent, index_by_text_par_con) - #slopes_marginals, all_found_textline_polygons_marginals, boxes_marginals, polygons_of_marginals, polygons_of_marginals, _ = \ + # slopes_marginals, all_found_textline_polygons_marginals, boxes_marginals, polygons_of_marginals, polygons_of_marginals, _ = \ # self.delete_regions_without_textlines(slopes_marginals, all_found_textline_polygons_marginals, # boxes_marginals, polygons_of_marginals, polygons_of_marginals, np.array(range(len(polygons_of_marginals)))) - #all_found_textline_polygons = self.dilate_textlines(all_found_textline_polygons) - #####all_found_textline_polygons = self.dilate_textline_contours(all_found_textline_polygons) + # all_found_textline_polygons = self.dilate_textlines(all_found_textline_polygons) + # all_found_textline_polygons = self.dilate_textline_contours(all_found_textline_polygons) all_found_textline_polygons = self.dilate_textregions_contours_textline_version( all_found_textline_polygons) all_found_textline_polygons = self.filter_contours_inside_a_bigger_one( @@ -4744,7 +4744,7 @@ class Eynollah: all_box_coord_marginals, _, slopes_marginals = self.get_slopes_and_deskew_new_light( polygons_of_marginals, polygons_of_marginals, textline_mask_tot_ea, image_page_rotated, boxes_marginals, slope_deskew) - #all_found_textline_polygons = self.filter_contours_inside_a_bigger_one( + # all_found_textline_polygons = self.filter_contours_inside_a_bigger_one( # all_found_textline_polygons, textline_mask_tot_ea_org, type_contour="textline") else: textline_mask_tot_ea = cv2.erode(textline_mask_tot_ea, kernel=KERNEL, iterations=1) @@ -4774,19 +4774,19 @@ class Eynollah: all_found_textline_polygons_marginals = small_textlines_to_parent_adherence2( all_found_textline_polygons_marginals, textline_mask_tot_ea, num_col_classifier) - #print("text region early 6 in %.1fs", time.time() - t0) + # print("text region early 6 in %.1fs", time.time() - t0) if self.full_layout: if np.abs(slope_deskew) >= SLOPE_THRESHOLD: contours_only_text_parent_d_ordered = self.return_list_of_contours_with_desired_order( contours_only_text_parent_d_ordered, index_by_text_par_con) - #try: - #contours_only_text_parent_d_ordered = \ - #list(np.array(contours_only_text_parent_d_ordered, dtype=np.int32)[index_by_text_par_con]) - #except: - #contours_only_text_parent_d_ordered = \ - #list(np.array(contours_only_text_parent_d_ordered, dtype=object)[index_by_text_par_con]) + # try: + # contours_only_text_parent_d_ordered = \ + # list(np.array(contours_only_text_parent_d_ordered, dtype=np.int32)[index_by_text_par_con]) + # except: + # contours_only_text_parent_d_ordered = \ + # list(np.array(contours_only_text_parent_d_ordered, dtype=object)[index_by_text_par_con]) else: - #takes long timee + # takes long timee contours_only_text_parent_d_ordered = None if self.light_version: fun = check_any_text_region_in_model_one_is_main_or_header_light @@ -4878,7 +4878,7 @@ class Eynollah: all_found_textline_polygons_marginals, all_box_coord_marginals, slopes, slopes_h, slopes_marginals, cont_page, polygons_lines_xml, ocr_all_textlines) self.logger.info("Job done in %.1fs", time.time() - t0) - #print("Job done in %.1fs", time.time() - t0) + # print("Job done in %.1fs", time.time() - t0) if self.dir_in: self.writer.write_pagexml(pcgts) continue @@ -4897,12 +4897,12 @@ class Eynollah: else: contours_only_text_parent_d_ordered = self.return_list_of_contours_with_desired_order( contours_only_text_parent_d_ordered, index_by_text_par_con) - #try: - #contours_only_text_parent_d_ordered = \ - #list(np.array(contours_only_text_parent_d_ordered, dtype=object)[index_by_text_par_con]) - #except: - #contours_only_text_parent_d_ordered = \ - #list(np.array(contours_only_text_parent_d_ordered, dtype=np.int32)[index_by_text_par_con]) + # try: + # contours_only_text_parent_d_ordered = \ + # list(np.array(contours_only_text_parent_d_ordered, dtype=object)[index_by_text_par_con]) + # except: + # contours_only_text_parent_d_ordered = \ + # list(np.array(contours_only_text_parent_d_ordered, dtype=np.int32)[index_by_text_par_con]) order_text_new, id_of_texts_tot = self.do_order_of_regions( contours_only_text_parent_d_ordered, contours_only_text_parent_h, boxes_d, textline_mask_tot_d) @@ -4918,7 +4918,7 @@ class Eynollah: model_ocr.to(device) ind_tot = 0 - #cv2.imwrite('./img_out.png', image_page) + # cv2.imwrite('./img_out.png', image_page) ocr_all_textlines = [] for indexing, ind_poly_first in enumerate(all_found_textline_polygons): ocr_textline_in_textregion = [] @@ -4926,14 +4926,14 @@ class Eynollah: if not (self.textline_light or self.curved_line): ind_poly = copy.deepcopy(ind_poly) box_ind = all_box_coord[indexing] - #print(ind_poly,np.shape(ind_poly), 'ind_poly') - #print(box_ind) + # print(ind_poly,np.shape(ind_poly), 'ind_poly') + # print(box_ind) ind_poly = self.return_textline_contour_with_added_box_coordinate(ind_poly, box_ind) - #print(ind_poly_copy) + # print(ind_poly_copy) ind_poly[ind_poly < 0] = 0 x, y, w, h = cv2.boundingRect(ind_poly) - #print(ind_poly_copy, np.shape(ind_poly_copy)) - #print(x, y, w, h, h/float(w),'ratio') + # print(ind_poly_copy, np.shape(ind_poly_copy)) + # print(x, y, w, h, h/float(w),'ratio') h2w_ratio = h / float(w) mask_poly = np.zeros(image_page.shape) if not self.light_version: @@ -4949,7 +4949,7 @@ class Eynollah: img_poly_on_img[:, :, 2][mask_poly[:, :, 0] == 0] = 255 img_croped = img_poly_on_img[y:y + h, x:x + w, :] - #cv2.imwrite('./extracted_lines/'+str(ind_tot)+'.jpg', img_croped) + # cv2.imwrite('./extracted_lines/'+str(ind_tot)+'.jpg', img_croped) text_ocr = self.return_ocr_of_textline_without_common_section(img_croped, model_ocr, processor, device, w, h2w_ratio, ind_tot) @@ -4959,23 +4959,23 @@ class Eynollah: else: ocr_all_textlines = None - #print(ocr_all_textlines) + # print(ocr_all_textlines) self.logger.info("detection of reading order took %.1fs", time.time() - t_order) pcgts = self.writer.build_pagexml_no_full_layout( txt_con_org, page_coord, order_text_new, id_of_texts_tot, all_found_textline_polygons, all_box_coord, polygons_of_images, polygons_of_marginals, all_found_textline_polygons_marginals, all_box_coord_marginals, slopes, slopes_marginals, cont_page, polygons_lines_xml, contours_tables, ocr_all_textlines) - #print("Job done in %.1fs" % (time.time() - t0)) + # print("Job done in %.1fs" % (time.time() - t0)) self.logger.info("Job done in %.1fs", time.time() - t0) if not self.dir_in: return pcgts - #print("text region early 7 in %.1fs", time.time() - t0) + # print("text region early 7 in %.1fs", time.time() - t0) if self.dir_in: self.writer.write_pagexml(pcgts) self.logger.info("Job done in %.1fs", time.time() - t0) - #print("Job done in %.1fs" % (time.time() - t0)) + # print("Job done in %.1fs" % (time.time() - t0)) if self.dir_in: self.logger.info("All jobs done in %.1fs", time.time() - t0_tot) @@ -5009,7 +5009,7 @@ class Eynollah_ocr: self.model_ocr.to(self.device) else: - self.model_ocr_dir = dir_models + "/model_3_new_ocrcnn" #"/model_0_ocr_cnnrnn"#"/model_23_ocr_cnnrnn" + self.model_ocr_dir = dir_models + "/model_3_new_ocrcnn" # "/model_0_ocr_cnnrnn"#"/model_23_ocr_cnnrnn" model_ocr = load_model(self.model_ocr_dir, compile=False) self.prediction_model = tf.keras.models.Model( @@ -5042,10 +5042,10 @@ class Eynollah_ocr: # The outputs are in the first element of the tuple. # Additionally, the first element is actually a list, # therefore we take the first element of that list as well. - #print(decoded,'decoded') + # print(decoded,'decoded') decoded = decoded[0][0][:, :max_len] - #print(decoded, decoded.shape,'decoded') + # print(decoded, decoded.shape,'decoded') output = [] for d in decoded: @@ -5121,7 +5121,7 @@ class Eynollah_ocr: split_point = self.return_start_and_end_of_common_text_of_textline_ocr_without_common_section(textline_image) if split_point: image1 = textline_image[:, :split_point, :] # image.crop((0, 0, width2, height)) - image2 = textline_image[:, split_point:, :] #image.crop((width1, 0, width, height)) + image2 = textline_image[:, split_point:, :] # image.crop((width1, 0, width, height)) return [image1, image2] else: return None @@ -5152,7 +5152,7 @@ class Eynollah_ocr: out_file_ocr = os.path.join(self.dir_out, file_name + '.xml') img = cv2.imread(dir_img) - ##file_name = Path(dir_xmls).stem + # file_name = Path(dir_xmls).stem tree1 = ET.parse(dir_xml, parser=ET.XMLParser(encoding="utf-8")) root1 = tree1.getroot() alltags = [elem.tag for elem in root1.iter()] @@ -5195,7 +5195,7 @@ class Eynollah_ocr: cropped_lines_meging_indexing.append(0) else: splited_images = self.return_textlines_split_if_needed(img_crop) - #print(splited_images) + # print(splited_images) if splited_images: cropped_lines.append(splited_images[0]) cropped_lines_meging_indexing.append(1) @@ -5230,11 +5230,11 @@ class Eynollah_ocr: range(len(cropped_lines_meging_indexing))] extracted_texts_merged = [ind for ind in extracted_texts_merged if ind is not None] - #print(extracted_texts_merged, len(extracted_texts_merged)) + # print(extracted_texts_merged, len(extracted_texts_merged)) unique_cropped_lines_region_indexer = np.unique(cropped_lines_region_indexer) - #print(len(unique_cropped_lines_region_indexer), 'unique_cropped_lines_region_indexer') + # print(len(unique_cropped_lines_region_indexer), 'unique_cropped_lines_region_indexer') text_by_textregion = [] for ind in unique_cropped_lines_region_indexer: extracted_texts_merged_un = np.array(extracted_texts_merged)[ @@ -5242,9 +5242,9 @@ class Eynollah_ocr: text_by_textregion.append(" ".join(extracted_texts_merged_un)) - #print(len(text_by_textregion) , indexer_text_region, "text_by_textregion") + # print(len(text_by_textregion) , indexer_text_region, "text_by_textregion") - #print(time.time() - t0 ,'elapsed time') + # print(time.time() - t0 ,'elapsed time') indexer = 0 indexer_textregion = 0 @@ -5266,11 +5266,11 @@ class Eynollah_ocr: ET.register_namespace("", name_space) tree1.write(out_file_ocr, xml_declaration=True, method='xml', encoding="utf8", default_namespace=None) - #print("Job done in %.1fs", time.time() - t0) + # print("Job done in %.1fs", time.time() - t0) else: max_len = 512 padding_token = 299 - image_width = 512 #max_len * 4 + image_width = 512 # max_len * 4 image_height = 32 b_s = 8 @@ -5430,4 +5430,4 @@ class Eynollah_ocr: ET.register_namespace("", name_space) tree1.write(out_file_ocr, xml_declaration=True, method='xml', encoding="utf8", default_namespace=None) - #print("Job done in %.1fs", time.time() - t0) + # print("Job done in %.1fs", time.time() - t0) diff --git a/src/eynollah/sbb_binarize.py b/src/eynollah/sbb_binarize.py index d4424cd..c215e50 100644 --- a/src/eynollah/sbb_binarize.py +++ b/src/eynollah/sbb_binarize.py @@ -180,7 +180,7 @@ class SbbBinarizer: seg = np.argmax(label_p_pred, axis=3) - #print(seg.shape, len(seg), len(list_i_s)) + # print(seg.shape, len(seg), len(list_i_s)) indexer_inside_batch = 0 for i_batch, j_batch in zip(list_i_s, list_j_s): @@ -253,7 +253,7 @@ class SbbBinarizer: seg = np.argmax(label_p_pred, axis=3) - #print(seg.shape, len(seg), len(list_i_s)) + # print(seg.shape, len(seg), len(list_i_s)) indexer_inside_batch = 0 for i_batch, j_batch in zip(list_i_s, list_j_s): diff --git a/src/eynollah/utils/__init__.py b/src/eynollah/utils/__init__.py index 6644cfd..ca4f49c 100644 --- a/src/eynollah/utils/__init__.py +++ b/src/eynollah/utils/__init__.py @@ -19,7 +19,7 @@ def return_x_start_end_mothers_childs_and_type_of_reading_order( x_min_hor_some, x_max_hor_some, cy_hor_some, peak_points, cy_hor_diff): x_start = [] x_end = [] - kind = [] #if covers 2 and more than 2 columns set it to 1 otherwise 0 + kind = [] # if covers 2 and more than 2 columns set it to 1 otherwise 0 len_sep = [] y_sep = [] y_diff = [] @@ -40,7 +40,7 @@ def return_x_start_end_mothers_childs_and_type_of_reading_order( if (max_end - min_start) == (len(peak_points) - 1): new_main_sep_y.append(indexer) - #print((max_end-min_start),len(peak_points),'(max_end-min_start)') + # print((max_end-min_start),len(peak_points),'(max_end-min_start)') y_sep.append(cy_hor_some[i]) y_diff.append(cy_hor_diff[i]) x_end.append(max_end) @@ -67,7 +67,7 @@ def return_x_start_end_mothers_childs_and_type_of_reading_order( x_s_unified = [] x_e_unified = [] if len(all_args_uniq) > 0: - #print('burda') + # print('burda') if type(all_args_uniq[0]) is list: for dd in range(len(all_args_uniq)): if len(all_args_uniq[dd]) == 2: @@ -75,14 +75,14 @@ def return_x_start_end_mothers_childs_and_type_of_reading_order( x_e_same_hor = np.array(x_end_returned)[all_args_uniq[dd]] y_sep_same_hor = np.array(y_sep_returned)[all_args_uniq[dd]] y_diff_same_hor = np.array(y_diff_returned)[all_args_uniq[dd]] - #print('burda2') + # print('burda2') if (x_s_same_hor[0] == x_e_same_hor[1] - 1 or x_s_same_hor[1] == x_e_same_hor[0] - 1 and x_s_same_hor[0] != x_s_same_hor[1] and x_e_same_hor[0] != x_e_same_hor[1]): - #print('burda3') + # print('burda3') for arg_in in all_args_uniq[dd]: - #print(arg_in,'arg_in') + # print(arg_in,'arg_in') args_to_be_unified.append(arg_in) y_selected = np.min(y_sep_same_hor) y_diff_selected = np.max(y_diff_same_hor) @@ -93,15 +93,15 @@ def return_x_start_end_mothers_childs_and_type_of_reading_order( x_e_unified.append(x_e_selected) y_unified.append(y_selected) y_diff_unified.append(y_diff_selected) - #print(x_s_same_hor,'x_s_same_hor') - #print(x_e_same_hor[:]-1,'x_e_same_hor') - #print('#############################') - #print(x_s_unified,'y_selected') - #print(x_e_unified,'x_s_selected') - #print(y_unified,'x_e_same_hor') + # print(x_s_same_hor,'x_s_same_hor') + # print(x_e_same_hor[:]-1,'x_e_same_hor') + # print('#############################') + # print(x_s_unified,'y_selected') + # print(x_e_unified,'x_s_selected') + # print(y_unified,'x_e_same_hor') args_lines_not_unified = list(set(range(len(y_sep_returned))) - set(args_to_be_unified)) - #print(args_lines_not_unified,'args_lines_not_unified') + # print(args_lines_not_unified,'args_lines_not_unified') x_start_returned_not_unified = list(np.array(x_start_returned)[args_lines_not_unified]) x_end_returned_not_unified = list(np.array(x_end_returned)[args_lines_not_unified]) @@ -114,22 +114,22 @@ def return_x_start_end_mothers_childs_and_type_of_reading_order( x_start_returned_not_unified.append(x_s_unified[dv]) x_end_returned_not_unified.append(x_e_unified[dv]) - #print(y_sep_returned,'y_sep_returned') - #print(x_start_returned,'x_start_returned') - #print(x_end_returned,'x_end_returned') + # print(y_sep_returned,'y_sep_returned') + # print(x_start_returned,'x_start_returned') + # print(x_end_returned,'x_end_returned') x_start_returned = np.array(x_start_returned_not_unified, dtype=int) x_end_returned = np.array(x_end_returned_not_unified, dtype=int) y_sep_returned = np.array(y_sep_returned_not_unified, dtype=int) y_diff_returned = np.array(y_diff_returned_not_unified, dtype=int) - #print(y_sep_returned,'y_sep_returned2') - #print(x_start_returned,'x_start_returned2') - #print(x_end_returned,'x_end_returned2') - #print(new_main_sep_y,'new_main_sep_y') + # print(y_sep_returned,'y_sep_returned2') + # print(x_start_returned,'x_start_returned2') + # print(x_end_returned,'x_end_returned2') + # print(new_main_sep_y,'new_main_sep_y') - #print(x_start,'x_start') - #print(x_end,'x_end') + # print(x_start,'x_start') + # print(x_end,'x_end') if len(new_main_sep_y) > 0: min_ys = np.min(y_sep) @@ -151,9 +151,9 @@ def return_x_start_end_mothers_childs_and_type_of_reading_order( y_min_new = y_mains_sorted[argm] y_max_new = y_mains_sorted[argm + 1] - #print(y_min_new,'y_min_new') - #print(y_max_new,'y_max_new') - #print(y_sep[new_main_sep_y[0]],y_sep,'yseps') + # print(y_min_new,'y_min_new') + # print(y_max_new,'y_max_new') + # print(y_sep[new_main_sep_y[0]],y_sep,'yseps') x_start = np.array(x_start) x_end = np.array(x_end) kind = np.array(kind) @@ -166,11 +166,11 @@ def return_x_start_end_mothers_childs_and_type_of_reading_order( y_sep = y_sep[(y_sep > y_min_new) & (y_sep < y_max_new)] elif (y_min_new in y_mains_sep_ohne_grenzen and y_max_new not in y_mains_sep_ohne_grenzen): - #print('burda') + # print('burda') x_start = x_start[(y_sep > y_min_new) & (y_sep <= y_max_new)] - #print('burda1') + # print('burda1') x_end = x_end[(y_sep > y_min_new) & (y_sep <= y_max_new)] - #print('burda2') + # print('burda2') kind = kind[(y_sep > y_min_new) & (y_sep <= y_max_new)] y_sep = y_sep[(y_sep > y_min_new) & (y_sep <= y_max_new)] elif (y_min_new not in y_mains_sep_ohne_grenzen and @@ -184,9 +184,9 @@ def return_x_start_end_mothers_childs_and_type_of_reading_order( x_end = x_end[(y_sep >= y_min_new) & (y_sep <= y_max_new)] kind = kind[(y_sep >= y_min_new) & (y_sep <= y_max_new)] y_sep = y_sep[(y_sep >= y_min_new) & (y_sep <= y_max_new)] - #print(x_start,'x_start') - #print(x_end,'x_end') - #print(len_sep) + # print(x_start,'x_start') + # print(x_end,'x_end') + # print(len_sep) deleted = [] for i in range(len(x_start) - 1): @@ -194,11 +194,11 @@ def return_x_start_end_mothers_childs_and_type_of_reading_order( for j in range(i + 1, len(x_start)): if nodes_i == set(range(x_start[j], x_end[j] + 1)): deleted.append(j) - #print(np.unique(deleted)) + # print(np.unique(deleted)) remained_sep_indexes = set(range(len(x_start))) - set(np.unique(deleted)) - #print(remained_sep_indexes,'remained_sep_indexes') - mother = [] #if it has mother + # print(remained_sep_indexes,'remained_sep_indexes') + mother = [] # if it has mother child = [] for index_i in remained_sep_indexes: have_mother = 0 @@ -213,9 +213,9 @@ def return_x_start_end_mothers_childs_and_type_of_reading_order( mother.append(have_mother) child.append(have_child) - #print(mother,'mother') - #print(len(remained_sep_indexes)) - #print(len(remained_sep_indexes),len(x_start),len(x_end),len(y_sep),'lens') + # print(mother,'mother') + # print(len(remained_sep_indexes)) + # print(len(remained_sep_indexes),len(x_start),len(x_end),len(y_sep),'lens') y_lines_without_mother = [] x_start_without_mother = [] x_end_without_mother = [] @@ -226,23 +226,23 @@ def return_x_start_end_mothers_childs_and_type_of_reading_order( mother = np.array(mother) child = np.array(child) - #print(mother,'mother') - #print(child,'child') + # print(mother,'mother') + # print(child,'child') remained_sep_indexes = np.array(list(remained_sep_indexes)) x_start = np.array(x_start) x_end = np.array(x_end) y_sep = np.array(y_sep) if len(remained_sep_indexes) > 1: - #print(np.array(remained_sep_indexes),'np.array(remained_sep_indexes)') - #print(np.array(mother),'mother') - ##remained_sep_indexes_without_mother = remained_sep_indexes[mother==0] - ##remained_sep_indexes_with_child_without_mother = remained_sep_indexes[mother==0 & child==1] + # print(np.array(remained_sep_indexes),'np.array(remained_sep_indexes)') + # print(np.array(mother),'mother') + # remained_sep_indexes_without_mother = remained_sep_indexes[mother==0] + # remained_sep_indexes_with_child_without_mother = remained_sep_indexes[mother==0 & child==1] remained_sep_indexes_without_mother = np.array(list(remained_sep_indexes))[np.array(mother) == 0] remained_sep_indexes_with_child_without_mother = np.array(list(remained_sep_indexes))[ (np.array(mother) == 0) & (np.array(child) == 1)] - #print(remained_sep_indexes_without_mother,'remained_sep_indexes_without_mother') - #print(remained_sep_indexes_without_mother,'remained_sep_indexes_without_mother') + # print(remained_sep_indexes_without_mother,'remained_sep_indexes_without_mother') + # print(remained_sep_indexes_without_mother,'remained_sep_indexes_without_mother') x_end_with_child_without_mother = x_end[remained_sep_indexes_with_child_without_mother] x_start_with_child_without_mother = x_start[remained_sep_indexes_with_child_without_mother] @@ -269,19 +269,19 @@ def return_x_start_end_mothers_childs_and_type_of_reading_order( reading_orther_type = 1 else: reading_orther_type = 0 - #print(reading_orther_type,'javab') - #print(y_lines_with_child_without_mother,'y_lines_with_child_without_mother') - #print(x_start_with_child_without_mother,'x_start_with_child_without_mother') - #print(x_end_with_child_without_mother,'x_end_with_hild_without_mother') + # print(reading_orther_type,'javab') + # print(y_lines_with_child_without_mother,'y_lines_with_child_without_mother') + # print(x_start_with_child_without_mother,'x_start_with_child_without_mother') + # print(x_end_with_child_without_mother,'x_end_with_hild_without_mother') len_sep_with_child = len(child[child == 1]) - #print(len_sep_with_child,'len_sep_with_child') + # print(len_sep_with_child,'len_sep_with_child') there_is_sep_with_child = 0 if len_sep_with_child >= 1: there_is_sep_with_child = 1 - #print(all_args_uniq,'all_args_uniq') - #print(args_to_be_unified,'args_to_be_unified') + # print(all_args_uniq,'all_args_uniq') + # print(args_to_be_unified,'args_to_be_unified') return (reading_orther_type, x_start_returned, @@ -355,7 +355,7 @@ def boosting_headers_by_longshot_region_segmentation(textregion_pre_p, textregio textregion_pre_p_org = np.copy(textregion_pre_p) # 4 is drop capitals headers_in_longshot = textregion_pre_np[:, :, 0] == 2 - #headers_in_longshot = ((textregion_pre_np[:,:,0]==2) | + # headers_in_longshot = ((textregion_pre_np[:,:,0]==2) | # (textregion_pre_np[:,:,0]==1)) textregion_pre_p[:, :, 0][headers_in_longshot & (textregion_pre_p[:, :, 0] != 4)] = 2 @@ -379,8 +379,8 @@ def find_num_col_deskew(regions_without_separators, sigma_, multiplier=3.8): def find_num_col(regions_without_separators, num_col_classifier, tables, multiplier=3.8): regions_without_separators_0 = regions_without_separators.sum(axis=0) - ##plt.plot(regions_without_separators_0) - ##plt.show() + # plt.plot(regions_without_separators_0) + # plt.show() sigma_ = 35 # 70#35 meda_n_updown = regions_without_separators_0[len(regions_without_separators_0):: -1] first_nonzero = next((i for i, x in enumerate(regions_without_separators_0) if x), 0) @@ -479,7 +479,7 @@ def find_num_col(regions_without_separators, num_col_classifier, tables, multipl num_col = 1 peaks_neg_fin = [] - ##print(len(peaks_neg_fin)) + # print(len(peaks_neg_fin)) diff_peaks = np.abs(np.diff(peaks_neg_fin)) cut_off = 400 @@ -514,7 +514,7 @@ def find_num_col(regions_without_separators, num_col_classifier, tables, multipl p_u_quarter = len(y) - p_quarter - ##print(num_col,'early') + # print(num_col,'early') if num_col == 3: if ((peaks_neg_true[0] > p_g_u and peaks_neg_true[1] > p_g_u) or @@ -564,15 +564,15 @@ def find_num_col(regions_without_separators, num_col_classifier, tables, multipl # plt.plot(peaks_neg_true,z[peaks_neg_true],'*') # plt.plot([0,len(y)], [grenze,grenze]) # plt.show() - ##print(len(peaks_neg_true)) + # print(len(peaks_neg_true)) return len(peaks_neg_true), peaks_neg_true def find_num_col_only_image(regions_without_separators, multiplier=3.8): regions_without_separators_0 = regions_without_separators[:, :].sum(axis=0) - ##plt.plot(regions_without_separators_0) - ##plt.show() + # plt.plot(regions_without_separators_0) + # plt.show() sigma_ = 15 meda_n_updown = regions_without_separators_0[len(regions_without_separators_0):: -1] @@ -767,8 +767,8 @@ def find_num_col_only_image(regions_without_separators, multiplier=3.8): def find_num_col_by_vertical_lines(regions_without_separators, multiplier=3.8): regions_without_separators_0 = regions_without_separators[:, :, 0].sum(axis=0) - ##plt.plot(regions_without_separators_0) - ##plt.show() + # plt.plot(regions_without_separators_0) + # plt.show() sigma_ = 35 # 70#35 z = gaussian_filter1d(regions_without_separators_0, sigma_) @@ -862,7 +862,7 @@ def putt_bb_of_drop_capitals_of_model_in_patches_in_layout(layout_in_patch, drop layout_in_patch[box0][layout_in_patch[box0] == drop_capital_label] = drop_capital_label layout_in_patch[box0][layout_in_patch[box0] == 0] = drop_capital_label layout_in_patch[box0][layout_in_patch[box0] == 4] = drop_capital_label # images - #layout_in_patch[box0][layout_in_patch[box0] == drop_capital_label] = 1#drop_capital_label + # layout_in_patch[box0][layout_in_patch[box0] == drop_capital_label] = 1#drop_capital_label return layout_in_patch @@ -920,7 +920,7 @@ def check_any_text_region_in_model_one_is_main_or_header( slopes_main.append(slopes[ii]) all_found_textline_polygons_main.append(all_found_textline_polygons[ii]) - #print(all_pixels,pixels_main,pixels_header) + # print(all_pixels,pixels_main,pixels_header) return (regions_model_1, contours_only_text_parent_main, @@ -941,7 +941,7 @@ def check_any_text_region_in_model_one_is_main_or_header_light( all_box_coord, all_found_textline_polygons, slopes, contours_only_text_parent_d_ordered): - ### to make it faster + # to make it faster h_o = regions_model_1.shape[0] w_o = regions_model_1.shape[1] zoom = 3 @@ -953,7 +953,6 @@ def check_any_text_region_in_model_one_is_main_or_header_light( interpolation=cv2.INTER_NEAREST) contours_only_text_parent = [(i / zoom).astype(int) for i in contours_only_text_parent] - ### cx_main, cy_main, x_min_main, x_max_main, y_min_main, y_max_main, y_corr_x_min_from_argmin = \ find_new_features_of_contours(contours_only_text_parent) @@ -1002,16 +1001,15 @@ def check_any_text_region_in_model_one_is_main_or_header_light( slopes_main.append(slopes[ii]) all_found_textline_polygons_main.append(all_found_textline_polygons[ii]) - #print(all_pixels,pixels_main,pixels_header) + # print(all_pixels,pixels_main,pixels_header) - ### to make it faster + # to make it faster regions_model_1 = cv2.resize(regions_model_1, (w_o, h_o), interpolation=cv2.INTER_NEAREST) # regions_model_full = cv2.resize(img, (regions_model_full.shape[1] // zoom, # regions_model_full.shape[0] // zoom), # interpolation=cv2.INTER_NEAREST) contours_only_text_parent_head = [(i * zoom).astype(int) for i in contours_only_text_parent_head] contours_only_text_parent_main = [(i * zoom).astype(int) for i in contours_only_text_parent_main] - ### return (regions_model_1, contours_only_text_parent_main, @@ -1042,11 +1040,11 @@ def small_textlines_to_parent_adherence2(textlines_con, textline_iamge, num_col) textlines_tot.append(np.array(textlines_con[m1][nn], dtype=np.int32)) textlines_tot_org_form.append(textlines_con[m1][nn]) - ##img_text_all=np.zeros((textline_iamge.shape[0],textline_iamge.shape[1])) - ##img_text_all=cv2.fillPoly(img_text_all, pts =textlines_tot , color=(1,1,1)) + # img_text_all=np.zeros((textline_iamge.shape[0],textline_iamge.shape[1])) + # img_text_all=cv2.fillPoly(img_text_all, pts =textlines_tot , color=(1,1,1)) - ##plt.imshow(img_text_all) - ##plt.show() + # plt.imshow(img_text_all) + # plt.show() areas_cnt_text = np.array([cv2.contourArea(textlines_tot[j]) for j in range(len(textlines_tot))]) areas_cnt_text = areas_cnt_text / float(textline_iamge.shape[0] * textline_iamge.shape[1]) @@ -1152,8 +1150,8 @@ def small_textlines_to_parent_adherence2(textlines_con, textline_iamge, num_col) def order_of_regions(textline_mask, contours_main, contours_header, y_ref): - ##plt.imshow(textline_mask) - ##plt.show() + # plt.imshow(textline_mask) + # plt.show() """ print(len(contours_main),'contours_main') mada_n=textline_mask.sum(axis=1) @@ -1164,8 +1162,8 @@ def order_of_regions(textline_mask, contours_main, contours_header, y_ref): x=np.arange(len(y)) peaks_real, _ = find_peaks(gaussian_filter1d(y, 3), height=0) - ##plt.imshow(textline_mask[:,:]) - ##plt.show() + # plt.imshow(textline_mask[:,:]) + # plt.show() sigma_gaus=8 z= gaussian_filter1d(y_help, sigma_gaus) @@ -1200,8 +1198,8 @@ def order_of_regions(textline_mask, contours_main, contours_header, y_ref): peaks_neg = peaks_neg - 20 - 20 peaks = peaks - 20 - ##plt.plot(z) - ##plt.show() + # plt.plot(z) + # plt.show() if contours_main is not None: areas_main = np.array([cv2.contourArea(contours_main[j]) for j in range(len(contours_main))]) M_main = [cv2.moments(contours_main[j]) for j in range(len(contours_main))] @@ -1279,7 +1277,7 @@ def order_of_regions(textline_mask, contours_main, contours_header, y_ref): final_types.append(int(ind_in_type[j])) final_index_type.append(int(ind_ind_type[j])) - ##matrix_of_orders[:len_main,4]=final_indexers_sorted[:] + # matrix_of_orders[:len_main,4]=final_indexers_sorted[:] # This fix is applied if the sum of the lengths of contours and contours_h # does not match final_indexers_sorted. However, this is not the optimal solution.. @@ -1297,7 +1295,7 @@ def order_of_regions(textline_mask, contours_main, contours_header, y_ref): def combine_hor_lines_and_delete_cross_points_and_get_lines_features_back_new( img_p_in_ver, img_in_hor, num_col_classifier): - #img_p_in_ver = cv2.erode(img_p_in_ver, self.kernel, iterations=2) + # img_p_in_ver = cv2.erode(img_p_in_ver, self.kernel, iterations=2) img_p_in_ver = img_p_in_ver.astype(np.uint8) img_p_in_ver = np.repeat(img_p_in_ver[:, :, np.newaxis], 3, axis=2) imgray = cv2.cvtColor(img_p_in_ver, cv2.COLOR_BGR2GRAY) @@ -1334,7 +1332,7 @@ def combine_hor_lines_and_delete_cross_points_and_get_lines_features_back_new( if len_lines_bigger_than_x_width_smaller_than_acolumn_width_per_column < 10: args_hor = np.arange(len(slope_lines_hor)) all_args_uniq = contours_in_same_horizon(cy_main_hor) - #print(all_args_uniq,'all_args_uniq') + # print(all_args_uniq,'all_args_uniq') if len(all_args_uniq) > 0: if type(all_args_uniq[0]) is list: special_separators = [] @@ -1346,8 +1344,8 @@ def combine_hor_lines_and_delete_cross_points_and_get_lines_features_back_new( some_x_min = x_min_main_hor[all_args_uniq[dd]] some_x_max = x_max_main_hor[all_args_uniq[dd]] - #img_in=np.zeros(separators_closeup_n[:,:,2].shape) - #print(img_p_in_ver.shape[1],some_x_max-some_x_min,'xdiff') + # img_in=np.zeros(separators_closeup_n[:,:,2].shape) + # print(img_p_in_ver.shape[1],some_x_max-some_x_min,'xdiff') diff_x_some = some_x_max - some_x_min for jv in range(len(some_args)): img_p_in = cv2.fillPoly(img_in_hor, pts=[contours_lines_hor[some_args[jv]]], color=(1, 1, 1)) @@ -1535,9 +1533,9 @@ def find_number_of_columns_in_document(region_pre_p, num_col_classifier, tables, matrix_of_lines_ch[:len(cy_main_hor), 0] = args_hor matrix_of_lines_ch[len(cy_main_hor):, 0] = args_ver matrix_of_lines_ch[len(cy_main_hor):, 1] = cx_main_ver - matrix_of_lines_ch[:len(cy_main_hor), 2] = x_min_main_hor + 50 #x_min_main_hor+150 + matrix_of_lines_ch[:len(cy_main_hor), 2] = x_min_main_hor + 50 # x_min_main_hor+150 matrix_of_lines_ch[len(cy_main_hor):, 2] = x_min_main_ver - matrix_of_lines_ch[:len(cy_main_hor), 3] = x_max_main_hor - 50 #x_max_main_hor-150 + matrix_of_lines_ch[:len(cy_main_hor), 3] = x_max_main_hor - 50 # x_max_main_hor-150 matrix_of_lines_ch[len(cy_main_hor):, 3] = x_max_main_ver matrix_of_lines_ch[:len(cy_main_hor), 4] = dist_x_hor matrix_of_lines_ch[len(cy_main_hor):, 4] = dist_x_ver @@ -1563,7 +1561,7 @@ def find_number_of_columns_in_document(region_pre_p, num_col_classifier, tables, matrix_l_n[matrix_of_lines_ch.shape[0]:, 4] = dist_x_head matrix_l_n[matrix_of_lines_ch.shape[0]:, 5] = y_min_main_head - 3 - 8 matrix_l_n[matrix_of_lines_ch.shape[0]:, 6] = y_min_main_head - 5 - 8 - matrix_l_n[matrix_of_lines_ch.shape[0]:, 7] = y_max_main_head #y_min_main_head+1-8 + matrix_l_n[matrix_of_lines_ch.shape[0]:, 7] = y_max_main_head # y_min_main_head+1-8 matrix_l_n[matrix_of_lines_ch.shape[0]:, 8] = 4 matrix_of_lines_ch = np.copy(matrix_l_n) @@ -1624,13 +1622,13 @@ def return_boxes_of_images_by_order_of_reading_new( boxes = [] peaks_neg_tot_tables = [] for i in range(len(splitter_y_new) - 1): - #print(splitter_y_new[i],splitter_y_new[i+1]) + # print(splitter_y_new[i],splitter_y_new[i+1]) matrix_new = matrix_of_lines_ch[:, :][(matrix_of_lines_ch[:, 6] > splitter_y_new[i]) & (matrix_of_lines_ch[:, 7] < splitter_y_new[i + 1])] - #print(len( matrix_new[:,9][matrix_new[:,9]==1] )) - #print(matrix_new[:,8][matrix_new[:,9]==1],'gaddaaa') + # print(len( matrix_new[:,9][matrix_new[:,9]==1] )) + # print(matrix_new[:,8][matrix_new[:,9]==1],'gaddaaa') # check to see is there any vertical separator to find holes. - #if (len(matrix_new[:,9][matrix_new[:,9]==1]) > 0 and + # if (len(matrix_new[:,9][matrix_new[:,9]==1]) > 0 and # np.max(matrix_new[:,8][matrix_new[:,9]==1]) >= # 0.1 * (np.abs(splitter_y_new[i+1]-splitter_y_new[i]))): if True: @@ -1649,26 +1647,26 @@ def return_boxes_of_images_by_order_of_reading_new( try: peaks_neg_fin_org = np.copy(peaks_neg_fin) if (len(peaks_neg_fin) + 1) < num_col_classifier or num_col_classifier == 6: - #print('burda') + # print('burda') if len(peaks_neg_fin) == 0: num_col, peaks_neg_fin = find_num_col( regions_without_separators[int(splitter_y_new[i]):int(splitter_y_new[i + 1]), :], num_col_classifier, tables, multiplier=3.) peaks_neg_fin_early = [0] - #print(peaks_neg_fin,'peaks_neg_fin') + # print(peaks_neg_fin,'peaks_neg_fin') for p_n in peaks_neg_fin: peaks_neg_fin_early.append(p_n) peaks_neg_fin_early.append(regions_without_separators.shape[1] - 1) - #print(peaks_neg_fin_early,'burda2') + # print(peaks_neg_fin_early,'burda2') peaks_neg_fin_rev = [] for i_n in range(len(peaks_neg_fin_early) - 1): - #print(i_n,'i_n') - #plt.plot(regions_without_separators[int(splitter_y_new[i]): + # print(i_n,'i_n') + # plt.plot(regions_without_separators[int(splitter_y_new[i]): # int(splitter_y_new[i+1]), # peaks_neg_fin_early[i_n]: # peaks_neg_fin_early[i_n+1]].sum(axis=0) ) - #plt.show() + # plt.show() try: num_col, peaks_neg_fin1 = find_num_col( regions_without_separators[int(splitter_y_new[i]):int(splitter_y_new[i + 1]), @@ -1692,7 +1690,7 @@ def return_boxes_of_images_by_order_of_reading_new( if i_n != (len(peaks_neg_fin_early) - 2): peaks_neg_fin_rev.append(peaks_neg_fin_early[i_n + 1]) - #print(peaks_neg_fin,'peaks_neg_fin') + # print(peaks_neg_fin,'peaks_neg_fin') peaks_neg_fin_rev = peaks_neg_fin_rev + peaks_neg_fin if len(peaks_neg_fin_rev) >= len(peaks_neg_fin_org): @@ -1702,10 +1700,10 @@ def return_boxes_of_images_by_order_of_reading_new( peaks_neg_fin = list(np.copy(peaks_neg_fin_org)) num_col = len(peaks_neg_fin) - #print(peaks_neg_fin,'peaks_neg_fin') + # print(peaks_neg_fin,'peaks_neg_fin') except: pass - #num_col, peaks_neg_fin = find_num_col( + # num_col, peaks_neg_fin = find_num_col( # regions_without_separators[int(splitter_y_new[i]):int(splitter_y_new[i+1]),:], # multiplier=7.0) x_min_hor_some = matrix_new[:, 2][(matrix_new[:, 9] == 0)] @@ -1738,11 +1736,11 @@ def return_boxes_of_images_by_order_of_reading_new( (len(y_lines_without_mother) >= 2 or there_is_sep_with_child == 1))): try: y_grenze = int(splitter_y_new[i]) + 300 - #check if there is a big separator in this y_mains_sep_ohne_grenzen + # check if there is a big separator in this y_mains_sep_ohne_grenzen args_early_ys = np.arange(len(y_type_2)) - #print(args_early_ys,'args_early_ys') - #print(int(splitter_y_new[i]),int(splitter_y_new[i+1])) + # print(args_early_ys,'args_early_ys') + # print(int(splitter_y_new[i]),int(splitter_y_new[i+1])) x_starting_up = x_starting[(y_type_2 > int(splitter_y_new[i])) & (y_type_2 <= y_grenze)] @@ -1761,21 +1759,21 @@ def return_boxes_of_images_by_order_of_reading_new( (x_ending_up == (len(peaks_neg_tot) - 1))] args_main_to_deleted = args_up[(x_starting_up == 0) & (x_ending_up == (len(peaks_neg_tot) - 1))] - #print(y_main_separator_up,y_diff_main_separator_up,args_main_to_deleted,'fffffjammmm') + # print(y_main_separator_up,y_diff_main_separator_up,args_main_to_deleted,'fffffjammmm') if len(y_diff_main_separator_up) > 0: args_to_be_kept = np.array(list(set(args_early_ys) - set(args_main_to_deleted))) - #print(args_to_be_kept,'args_to_be_kept') + # print(args_to_be_kept,'args_to_be_kept') boxes.append([0, peaks_neg_tot[len(peaks_neg_tot) - 1], int(splitter_y_new[i]), int(np.max(y_diff_main_separator_up))]) splitter_y_new[i] = [np.max(y_diff_main_separator_up)][0] - #print(splitter_y_new[i],'splitter_y_new[i]') + # print(splitter_y_new[i],'splitter_y_new[i]') y_type_2 = y_type_2[args_to_be_kept] x_starting = x_starting[args_to_be_kept] x_ending = x_ending[args_to_be_kept] y_diff_type_2 = y_diff_type_2[args_to_be_kept] - #print('galdiha') + # print('galdiha') y_grenze = int(splitter_y_new[i]) + 200 args_early_ys2 = np.arange(len(y_type_2)) y_type_2_up = y_type_2[(y_type_2 > int(splitter_y_new[i])) & @@ -1788,20 +1786,20 @@ def return_boxes_of_images_by_order_of_reading_new( (y_type_2 <= y_grenze)] args_up2 = args_early_ys2[(y_type_2 > int(splitter_y_new[i])) & (y_type_2 <= y_grenze)] - #print(y_type_2_up,x_starting_up,x_ending_up,'didid') + # print(y_type_2_up,x_starting_up,x_ending_up,'didid') nodes_in = [] for ij in range(len(x_starting_up)): nodes_in = nodes_in + list(range(x_starting_up[ij], x_ending_up[ij])) nodes_in = np.unique(nodes_in) - #print(nodes_in,'nodes_in') + # print(nodes_in,'nodes_in') if set(nodes_in) == set(range(len(peaks_neg_tot) - 1)): pass elif set(nodes_in) == set(range(1, len(peaks_neg_tot) - 1)): pass else: - #print('burdaydikh') + # print('burdaydikh') args_to_be_kept2 = np.array(list(set(args_early_ys2) - set(args_up2))) if len(args_to_be_kept2) > 0: @@ -1811,28 +1809,28 @@ def return_boxes_of_images_by_order_of_reading_new( y_diff_type_2 = y_diff_type_2[args_to_be_kept2] else: pass - #print('burdaydikh2') + # print('burdaydikh2') elif len(y_diff_main_separator_up) == 0: nodes_in = [] for ij in range(len(x_starting_up)): nodes_in = nodes_in + list(range(x_starting_up[ij], x_ending_up[ij])) nodes_in = np.unique(nodes_in) - #print(nodes_in,'nodes_in2') - #print(np.array(range(len(peaks_neg_tot)-1)),'np.array(range(len(peaks_neg_tot)-1))') + # print(nodes_in,'nodes_in2') + # print(np.array(range(len(peaks_neg_tot)-1)),'np.array(range(len(peaks_neg_tot)-1))') if set(nodes_in) == set(range(len(peaks_neg_tot) - 1)): pass elif set(nodes_in) == set(range(1, len(peaks_neg_tot) - 1)): pass else: - #print('burdaydikh') - #print(args_early_ys,'args_early_ys') - #print(args_up,'args_up') + # print('burdaydikh') + # print(args_early_ys,'args_early_ys') + # print(args_up,'args_up') args_to_be_kept2 = np.array(list(set(args_early_ys) - set(args_up))) - #print(args_to_be_kept2,'args_to_be_kept2') - #print(len(y_type_2),len(x_starting),len(x_ending),len(y_diff_type_2)) + # print(args_to_be_kept2,'args_to_be_kept2') + # print(len(y_type_2),len(x_starting),len(x_ending),len(y_diff_type_2)) if len(args_to_be_kept2) > 0: y_type_2 = y_type_2[args_to_be_kept2] x_starting = x_starting[args_to_be_kept2] @@ -1840,9 +1838,9 @@ def return_boxes_of_images_by_order_of_reading_new( y_diff_type_2 = y_diff_type_2[args_to_be_kept2] else: pass - #print('burdaydikh2') + # print('burdaydikh2') - #int(splitter_y_new[i]) + # int(splitter_y_new[i]) y_lines_by_order = [] x_start_by_order = [] x_end_by_order = [] @@ -1853,7 +1851,7 @@ def return_boxes_of_images_by_order_of_reading_new( x_start_by_order.append(0) x_end_by_order.append(len(peaks_neg_tot) - 2) else: - #print(x_start_without_mother,x_end_without_mother,peaks_neg_tot,'dodo') + # print(x_start_without_mother,x_end_without_mother,peaks_neg_tot,'dodo') columns_covered_by_mothers = [] for dj in range(len(x_start_without_mother)): columns_covered_by_mothers = columns_covered_by_mothers + \ @@ -1864,40 +1862,40 @@ def return_boxes_of_images_by_order_of_reading_new( all_columns = np.arange(len(peaks_neg_tot) - 1) columns_not_covered = list(set(all_columns) - set(columns_covered_by_mothers)) y_type_2 = np.append(y_type_2, [int(splitter_y_new[i])] * ( - len(columns_not_covered) + len(x_start_without_mother))) - ##y_lines_by_order = np.append(y_lines_by_order, [int(splitter_y_new[i])] * len(columns_not_covered)) - ##x_start_by_order = np.append(x_start_by_order, [0] * len(columns_not_covered)) + len(columns_not_covered) + len(x_start_without_mother))) + # y_lines_by_order = np.append(y_lines_by_order, [int(splitter_y_new[i])] * len(columns_not_covered)) + # x_start_by_order = np.append(x_start_by_order, [0] * len(columns_not_covered)) x_starting = np.append(x_starting, columns_not_covered) x_starting = np.append(x_starting, x_start_without_mother) x_ending = np.append(x_ending, np.array(columns_not_covered) + 1) x_ending = np.append(x_ending, x_end_without_mother) ind_args = np.arange(len(y_type_2)) - #ind_args=np.array(ind_args) - #print(ind_args,'ind_args') + # ind_args=np.array(ind_args) + # print(ind_args,'ind_args') for column in range(len(peaks_neg_tot) - 1): - #print(column,'column') + # print(column,'column') ind_args_in_col = ind_args[x_starting == column] - #print('babali2') - #print(ind_args_in_col,'ind_args_in_col') + # print('babali2') + # print(ind_args_in_col,'ind_args_in_col') ind_args_in_col = np.array(ind_args_in_col) - #print(len(y_type_2)) + # print(len(y_type_2)) y_column = y_type_2[ind_args_in_col] x_start_column = x_starting[ind_args_in_col] x_end_column = x_ending[ind_args_in_col] - #print('babali3') + # print('babali3') ind_args_col_sorted = np.argsort(y_column) y_col_sort = y_column[ind_args_col_sorted] x_start_column_sort = x_start_column[ind_args_col_sorted] x_end_column_sort = x_end_column[ind_args_col_sorted] - #print('babali4') + # print('babali4') for ii in range(len(y_col_sort)): - #print('babali5') + # print('babali5') y_lines_by_order.append(y_col_sort[ii]) x_start_by_order.append(x_start_column_sort[ii]) x_end_by_order.append(x_end_column_sort[ii] - 1) else: - #print(x_start_without_mother,x_end_without_mother,peaks_neg_tot,'dodo') + # print(x_start_without_mother,x_end_without_mother,peaks_neg_tot,'dodo') columns_covered_by_mothers = [] for dj in range(len(x_start_without_mother)): columns_covered_by_mothers = columns_covered_by_mothers + \ @@ -1908,9 +1906,9 @@ def return_boxes_of_images_by_order_of_reading_new( all_columns = np.arange(len(peaks_neg_tot) - 1) columns_not_covered = list(set(all_columns) - set(columns_covered_by_mothers)) y_type_2 = np.append(y_type_2, [int(splitter_y_new[i])] * ( - len(columns_not_covered) + len(x_start_without_mother))) - ##y_lines_by_order = np.append(y_lines_by_order, [int(splitter_y_new[i])] * len(columns_not_covered)) - ##x_start_by_order = np.append(x_start_by_order, [0] * len(columns_not_covered)) + len(columns_not_covered) + len(x_start_without_mother))) + # y_lines_by_order = np.append(y_lines_by_order, [int(splitter_y_new[i])] * len(columns_not_covered)) + # x_start_by_order = np.append(x_start_by_order, [0] * len(columns_not_covered)) x_starting = np.append(x_starting, columns_not_covered) x_starting = np.append(x_starting, x_start_without_mother) x_ending = np.append(x_ending, np.array(columns_not_covered) + 1) @@ -1926,7 +1924,7 @@ def return_boxes_of_images_by_order_of_reading_new( all_columns = np.arange(len(peaks_neg_tot) - 1) columns_not_covered_child_no_mother = list( set(all_columns) - set(columns_covered_by_with_child_no_mothers)) - #indexes_to_be_spanned=[] + # indexes_to_be_spanned=[] for i_s in range(len(x_end_with_child_without_mother)): columns_not_covered_child_no_mother.append(x_start_with_child_without_mother[i_s]) columns_not_covered_child_no_mother = np.sort(columns_not_covered_child_no_mother) @@ -1936,7 +1934,7 @@ def return_boxes_of_images_by_order_of_reading_new( for i_s_nc in columns_not_covered_child_no_mother: if i_s_nc in x_start_with_child_without_mother: x_end_biggest_column = \ - x_end_with_child_without_mother[x_start_with_child_without_mother == i_s_nc][0] + x_end_with_child_without_mother[x_start_with_child_without_mother == i_s_nc][0] args_all_biggest_lines = ind_args[(x_starting == i_s_nc) & (x_ending == x_end_biggest_column)] y_column_nc = y_type_2[args_all_biggest_lines] @@ -2020,40 +2018,40 @@ def return_boxes_of_images_by_order_of_reading_new( ind_args_between = np.arange(len(x_ending_all_between_nm_wc)) for column in range(i_s_nc, x_end_biggest_column): ind_args_in_col = ind_args_between[x_starting_all_between_nm_wc == column] - #print('babali2') - #print(ind_args_in_col,'ind_args_in_col') + # print('babali2') + # print(ind_args_in_col,'ind_args_in_col') ind_args_in_col = np.array(ind_args_in_col) - #print(len(y_type_2)) + # print(len(y_type_2)) y_column = y_all_between_nm_wc[ind_args_in_col] x_start_column = x_starting_all_between_nm_wc[ind_args_in_col] x_end_column = x_ending_all_between_nm_wc[ind_args_in_col] - #print('babali3') + # print('babali3') ind_args_col_sorted = np.argsort(y_column) y_col_sort = y_column[ind_args_col_sorted] x_start_column_sort = x_start_column[ind_args_col_sorted] x_end_column_sort = x_end_column[ind_args_col_sorted] - #print('babali4') + # print('babali4') for ii in range(len(y_col_sort)): - #print('babali5') + # print('babali5') y_lines_by_order.append(y_col_sort[ii]) x_start_by_order.append(x_start_column_sort[ii]) x_end_by_order.append(x_end_column_sort[ii] - 1) else: - #print(column,'column') + # print(column,'column') ind_args_in_col = ind_args[x_starting == i_s_nc] - #print('babali2') - #print(ind_args_in_col,'ind_args_in_col') + # print('babali2') + # print(ind_args_in_col,'ind_args_in_col') ind_args_in_col = np.array(ind_args_in_col) - #print(len(y_type_2)) + # print(len(y_type_2)) y_column = y_type_2[ind_args_in_col] x_start_column = x_starting[ind_args_in_col] x_end_column = x_ending[ind_args_in_col] - #print('babali3') + # print('babali3') ind_args_col_sorted = np.argsort(y_column) y_col_sort = y_column[ind_args_col_sorted] x_start_column_sort = x_start_column[ind_args_col_sorted] x_end_column_sort = x_end_column[ind_args_col_sorted] - #print('babali4') + # print('babali4') for ii in range(len(y_col_sort)): y_lines_by_order.append(y_col_sort[ii]) x_start_by_order.append(x_start_column_sort[ii]) @@ -2064,27 +2062,27 @@ def return_boxes_of_images_by_order_of_reading_new( x_start_copy = list(x_start_by_order) x_end_copy = list(x_end_by_order) - #print(y_copy,'y_copy') + # print(y_copy,'y_copy') y_itself = y_copy.pop(il) x_start_itself = x_start_copy.pop(il) x_end_itself = x_end_copy.pop(il) - #print(y_copy,'y_copy2') + # print(y_copy,'y_copy2') for column in range(x_start_itself, x_end_itself + 1): - #print(column,'cols') + # print(column,'cols') y_in_cols = [] for yic in range(len(y_copy)): - #print('burda') + # print('burda') if (y_copy[yic] > y_itself and x_start_copy[yic] <= column <= x_end_copy[yic]): y_in_cols.append(y_copy[yic]) - #print('burda2') - #print(y_in_cols,'y_in_cols') + # print('burda2') + # print(y_in_cols,'y_in_cols') if len(y_in_cols) > 0: y_down = np.min(y_in_cols) else: y_down = [int(splitter_y_new[i + 1])][0] - #print(y_itself,'y_itself') + # print(y_itself,'y_itself') boxes.append([peaks_neg_tot[column], peaks_neg_tot[column + 1], y_itself, @@ -2110,8 +2108,8 @@ def return_boxes_of_images_by_order_of_reading_new( columns_not_covered = list(set(all_columns) - set(columns_covered_by_lines_covered_more_than_2col)) y_type_2 = np.append(y_type_2, [int(splitter_y_new[i])] * (len(columns_not_covered) + 1)) - ##y_lines_by_order = np.append(y_lines_by_order, [int(splitter_y_new[i])] * len(columns_not_covered)) - ##x_start_by_order = np.append(x_start_by_order, [0] * len(columns_not_covered)) + # y_lines_by_order = np.append(y_lines_by_order, [int(splitter_y_new[i])] * len(columns_not_covered)) + # x_start_by_order = np.append(x_start_by_order, [0] * len(columns_not_covered)) x_starting = np.append(x_starting, columns_not_covered) x_ending = np.append(x_ending, np.array(columns_not_covered) + 1) if len(new_main_sep_y) > 0: @@ -2124,18 +2122,18 @@ def return_boxes_of_images_by_order_of_reading_new( all_columns = np.arange(len(peaks_neg_tot) - 1) columns_not_covered = list(set(all_columns)) y_type_2 = np.append(y_type_2, [int(splitter_y_new[i])] * len(columns_not_covered)) - ##y_lines_by_order = np.append(y_lines_by_order, [int(splitter_y_new[i])] * len(columns_not_covered)) - ##x_start_by_order = np.append(x_start_by_order, [0] * len(columns_not_covered)) + # y_lines_by_order = np.append(y_lines_by_order, [int(splitter_y_new[i])] * len(columns_not_covered)) + # x_start_by_order = np.append(x_start_by_order, [0] * len(columns_not_covered)) x_starting = np.append(x_starting, columns_not_covered) x_ending = np.append(x_ending, np.array(columns_not_covered) + 1) ind_args = np.array(range(len(y_type_2))) - #ind_args=np.array(ind_args) + # ind_args=np.array(ind_args) for column in range(len(peaks_neg_tot) - 1): - #print(column,'column') + # print(column,'column') ind_args_in_col = ind_args[x_starting == column] ind_args_in_col = np.array(ind_args_in_col) - #print(len(y_type_2)) + # print(len(y_type_2)) y_column = y_type_2[ind_args_in_col] x_start_column = x_starting[ind_args_in_col] x_end_column = x_ending[ind_args_in_col] @@ -2144,9 +2142,9 @@ def return_boxes_of_images_by_order_of_reading_new( y_col_sort = y_column[ind_args_col_sorted] x_start_column_sort = x_start_column[ind_args_col_sorted] x_end_column_sort = x_end_column[ind_args_col_sorted] - #print('babali4') + # print('babali4') for ii in range(len(y_col_sort)): - #print('babali5') + # print('babali5') y_lines_by_order.append(y_col_sort[ii]) x_start_by_order.append(x_start_column_sort[ii]) x_end_by_order.append(x_end_column_sort[ii] - 1) @@ -2156,33 +2154,33 @@ def return_boxes_of_images_by_order_of_reading_new( x_start_copy = list(x_start_by_order) x_end_copy = list(x_end_by_order) - #print(y_copy,'y_copy') + # print(y_copy,'y_copy') y_itself = y_copy.pop(il) x_start_itself = x_start_copy.pop(il) x_end_itself = x_end_copy.pop(il) - #print(y_copy,'y_copy2') + # print(y_copy,'y_copy2') for column in range(x_start_itself, x_end_itself + 1): - #print(column,'cols') + # print(column,'cols') y_in_cols = [] for yic in range(len(y_copy)): - #print('burda') + # print('burda') if (y_copy[yic] > y_itself and x_start_copy[yic] <= column <= x_end_copy[yic]): y_in_cols.append(y_copy[yic]) - #print('burda2') - #print(y_in_cols,'y_in_cols') + # print('burda2') + # print(y_in_cols,'y_in_cols') if len(y_in_cols) > 0: y_down = np.min(y_in_cols) else: y_down = [int(splitter_y_new[i + 1])][0] - #print(y_itself,'y_itself') + # print(y_itself,'y_itself') boxes.append([peaks_neg_tot[column], peaks_neg_tot[column + 1], y_itself, y_down]) - #else: - #boxes.append([ 0, regions_without_separators[:,:].shape[1] ,splitter_y_new[i],splitter_y_new[i+1]]) + # else: + # boxes.append([ 0, regions_without_separators[:,:].shape[1] ,splitter_y_new[i],splitter_y_new[i+1]]) if right2left_readingorder: peaks_neg_tot_tables_new = [] diff --git a/src/eynollah/utils/contour.py b/src/eynollah/utils/contour.py index 09738cd..0700fef 100644 --- a/src/eynollah/utils/contour.py +++ b/src/eynollah/utils/contour.py @@ -65,7 +65,7 @@ def filter_contours_area_of_image_tables(image, contours, hierarchy, max_area, m polygon = geometry.Polygon([point[0] for point in c]) # area = cv2.contourArea(c) area = polygon.area - ##print(np.prod(thresh.shape[:2])) + # print(np.prod(thresh.shape[:2])) # Check that polygon has area greater than minimal area # print(hierarchy[0][jv][3],hierarchy ) if (min_area * np.prod(image.shape[:2]) <= area <= max_area * np.prod(image.shape[:2]) and @@ -200,7 +200,7 @@ def get_textregion_contours_in_org_image(cnts, img, slope_first): # print(img.shape,'img') img_copy = rotation_image_new(img_copy, -slope_first) - ##print(img_copy.shape,'img_copy') + # print(img_copy.shape,'img_copy') # plt.imshow(img_copy) # plt.show() @@ -258,8 +258,8 @@ def get_textregion_contours_in_org_image_light(cnts, img, slope_first, map=map): if not len(cnts): return [] img = cv2.resize(img, (int(img.shape[1] / 6), int(img.shape[0] / 6)), interpolation=cv2.INTER_NEAREST) - ##cnts = list( (np.array(cnts)/2).astype(np.int16) ) - #cnts = cnts/2 + # cnts = list( (np.array(cnts)/2).astype(np.int16) ) + # cnts = cnts/2 cnts = [(i / 6).astype(np.int) for i in cnts] results = map(partial(do_back_rotation_and_get_cnt_back, img=img, diff --git a/src/eynollah/utils/drop_capitals.py b/src/eynollah/utils/drop_capitals.py index 0e84a05..975f493 100644 --- a/src/eynollah/utils/drop_capitals.py +++ b/src/eynollah/utils/drop_capitals.py @@ -48,7 +48,7 @@ def adhere_drop_capital_region_into_corresponding_textline( # plt.imshow(img_con[:,:,0]) # plt.show() - ##img_con=cv2.dilate(img_con, kernel, iterations=30) + # img_con=cv2.dilate(img_con, kernel, iterations=30) # plt.imshow(img_con[:,:,0]) # plt.show() @@ -126,13 +126,13 @@ def adhere_drop_capital_region_into_corresponding_textline( contours_combined = return_contours_of_interested_region(img_textlines, 255, 0) - #plt.imshow(img_textlines) - #plt.show() + # plt.imshow(img_textlines) + # plt.show() - #imgray = cv2.cvtColor(img_textlines, cv2.COLOR_BGR2GRAY) - #ret, thresh = cv2.threshold(imgray, 0, 255, 0) + # imgray = cv2.cvtColor(img_textlines, cv2.COLOR_BGR2GRAY) + # ret, thresh = cv2.threshold(imgray, 0, 255, 0) - #contours_combined, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) + # contours_combined, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) areas_cnt_text = np.array( [cv2.contourArea(contours_combined[j]) for j in range(len(contours_combined))]) @@ -193,10 +193,10 @@ def adhere_drop_capital_region_into_corresponding_textline( img_textlines = img_textlines.astype(np.uint8) contours_combined = return_contours_of_interested_region(img_textlines, 255, 0) - ##imgray = cv2.cvtColor(img_textlines, cv2.COLOR_BGR2GRAY) - ##ret, thresh = cv2.threshold(imgray, 0, 255, 0) + # imgray = cv2.cvtColor(img_textlines, cv2.COLOR_BGR2GRAY) + # ret, thresh = cv2.threshold(imgray, 0, 255, 0) - ##contours_combined, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) + # contours_combined, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) areas_cnt_text = np.array( [cv2.contourArea(contours_combined[j]) for j in range(len(contours_combined))]) @@ -209,7 +209,7 @@ def adhere_drop_capital_region_into_corresponding_textline( # contours_biggest[:,0,1]=contours_biggest[:,0,1]#-all_box_coord[int(region_final)][0] # print(np.shape(contours_biggest),'contours_biggest') # print(np.shape(all_found_textline_polygons[int(region_final)][arg_min])) - ##contours_biggest=contours_biggest.reshape(np.shape(contours_biggest)[0],np.shape(contours_biggest)[2]) + # contours_biggest=contours_biggest.reshape(np.shape(contours_biggest)[0],np.shape(contours_biggest)[2]) if len(contours_combined) == 1: all_found_textline_polygons[int(region_final)][arg_min] = contours_biggest elif len(contours_combined) == 2: @@ -250,10 +250,10 @@ def adhere_drop_capital_region_into_corresponding_textline( img_textlines = img_textlines.astype(np.uint8) contours_combined = return_contours_of_interested_region(img_textlines, 255, 0) - #imgray = cv2.cvtColor(img_textlines, cv2.COLOR_BGR2GRAY) - #ret, thresh = cv2.threshold(imgray, 0, 255, 0) + # imgray = cv2.cvtColor(img_textlines, cv2.COLOR_BGR2GRAY) + # ret, thresh = cv2.threshold(imgray, 0, 255, 0) - #contours_combined, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) + # contours_combined, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # print(len(contours_combined),'len textlines mixed') areas_cnt_text = np.array( @@ -266,7 +266,7 @@ def adhere_drop_capital_region_into_corresponding_textline( contours_biggest[:, 0, 0] = contours_biggest[:, 0, 0] # -all_box_coord[int(region_final)][2] contours_biggest[:, 0, 1] = contours_biggest[:, 0, 1] # -all_box_coord[int(region_final)][0] - ##contours_biggest=contours_biggest.reshape(np.shape(contours_biggest)[0],np.shape(contours_biggest)[2]) + # contours_biggest=contours_biggest.reshape(np.shape(contours_biggest)[0],np.shape(contours_biggest)[2]) if len(contours_combined) == 1: all_found_textline_polygons[int(region_final)][arg_min] = contours_biggest elif len(contours_combined) == 2: @@ -281,49 +281,49 @@ def adhere_drop_capital_region_into_corresponding_textline( else: pass - ##cx_t,cy_t ,_, _, _ ,_,_= find_new_features_of_contours(all_found_textline_polygons[int(region_final)]) - ###print(all_box_coord[j_cont]) - ###print(cx_t) - ###print(cy_t) - ###print(cx_d[i_drop]) - ###print(cy_d[i_drop]) - ##y_lines=all_box_coord[int(region_final)][0]+np.array(cy_t) + # cx_t,cy_t ,_, _, _ ,_,_= find_new_features_of_contours(all_found_textline_polygons[int(region_final)]) + # print(all_box_coord[j_cont]) + # print(cx_t) + # print(cy_t) + # print(cx_d[i_drop]) + # print(cy_d[i_drop]) + # y_lines=all_box_coord[int(region_final)][0]+np.array(cy_t) - ##y_lines[y_lines 1: @@ -369,10 +369,10 @@ def adhere_drop_capital_region_into_corresponding_textline( img_textlines = img_textlines.astype(np.uint8) contours_combined = return_contours_of_interested_region(img_textlines, 255, 0) - #imgray = cv2.cvtColor(img_textlines, cv2.COLOR_BGR2GRAY) - #ret, thresh = cv2.threshold(imgray, 0, 255, 0) + # imgray = cv2.cvtColor(img_textlines, cv2.COLOR_BGR2GRAY) + # ret, thresh = cv2.threshold(imgray, 0, 255, 0) - #contours_combined, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) + # contours_combined, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # print(len(contours_combined),'len textlines mixed') areas_cnt_text = np.array( @@ -437,10 +437,10 @@ def adhere_drop_capital_region_into_corresponding_textline( img_textlines = img_textlines.astype(np.uint8) contours_combined = return_contours_of_interested_region(img_textlines, 255, 0) - #imgray = cv2.cvtColor(img_textlines, cv2.COLOR_BGR2GRAY) - #ret, thresh = cv2.threshold(imgray, 0, 255, 0) + # imgray = cv2.cvtColor(img_textlines, cv2.COLOR_BGR2GRAY) + # ret, thresh = cv2.threshold(imgray, 0, 255, 0) - #contours_combined, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) + # contours_combined, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # print(len(contours_combined),'len textlines mixed') areas_cnt_text = np.array( @@ -469,69 +469,69 @@ def adhere_drop_capital_region_into_corresponding_textline( else: pass - #####for i_drop in range(len(polygons_of_drop_capitals)): - #####for j_cont in range(len(contours_only_text_parent)): - #####img_con=np.zeros((text_regions_p.shape[0],text_regions_p.shape[1],3)) - #####img_con=cv2.fillPoly(img_con,pts=[polygons_of_drop_capitals[i_drop] ],color=(255,255,255)) - #####img_con=cv2.fillPoly(img_con,pts=[contours_only_text_parent[j_cont]],color=(255,255,255)) - - #####img_con=img_con.astype(np.uint8) - ######imgray = cv2.cvtColor(img_con, cv2.COLOR_BGR2GRAY) - ######ret, thresh = cv2.threshold(imgray, 0, 255, 0) - - ######contours_new,hierarchy=cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) - - #####contours_new,hir_new=return_contours_of_image(img_con) - #####contours_new_parent=return_parent_contours( contours_new,hir_new) - ######plt.imshow(img_con) - ######plt.show() - #####try: - #####if len(contours_new_parent)==1: - ######print(all_found_textline_polygons[j_cont][0]) - #####cx_t,cy_t ,_, _, _ ,_,_= find_new_features_of_contours(all_found_textline_polygons[j_cont]) - ######print(all_box_coord[j_cont]) - ######print(cx_t) - ######print(cy_t) - ######print(cx_d[i_drop]) - ######print(cy_d[i_drop]) - #####y_lines=all_box_coord[j_cont][0]+np.array(cy_t) - - ######print(y_lines) - - #####arg_min=np.argmin(np.abs(y_lines-y_min_d[i_drop]) ) - ######print(arg_min) - - #####cnt_nearest=np.copy(all_found_textline_polygons[j_cont][arg_min]) - #####cnt_nearest[:,0]=all_found_textline_polygons[j_cont][arg_min][:,0]+all_box_coord[j_cont][2] - #####cnt_nearest[:,1]=all_found_textline_polygons[j_cont][arg_min][:,1]+all_box_coord[j_cont][0] - - #####img_textlines=np.zeros((text_regions_p.shape[0],text_regions_p.shape[1],3)) - #####img_textlines=cv2.fillPoly(img_textlines,pts=[cnt_nearest],color=(255,255,255)) - #####img_textlines=cv2.fillPoly(img_textlines,pts=[polygons_of_drop_capitals[i_drop] ],color=(255,255,255)) - - #####img_textlines=img_textlines.astype(np.uint8) - #####imgray = cv2.cvtColor(img_textlines, cv2.COLOR_BGR2GRAY) - #####ret, thresh = cv2.threshold(imgray, 0, 255, 0) - - #####contours_combined,hierarchy=cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) - - #####areas_cnt_text=np.array([cv2.contourArea(contours_combined[j]) for j in range(len(contours_combined))]) - - #####contours_biggest=contours_combined[np.argmax(areas_cnt_text)] - - ######print(np.shape(contours_biggest)) - ######print(contours_biggest[:]) - #####contours_biggest[:,0,0]=contours_biggest[:,0,0]-all_box_coord[j_cont][2] - #####contours_biggest[:,0,1]=contours_biggest[:,0,1]-all_box_coord[j_cont][0] - - #####all_found_textline_polygons[j_cont][arg_min]=contours_biggest - ######print(contours_biggest) - ######plt.imshow(img_textlines[:,:,0]) - ######plt.show() - #####else: - #####pass - #####except: - #####pass + # for i_drop in range(len(polygons_of_drop_capitals)): + # for j_cont in range(len(contours_only_text_parent)): + # img_con=np.zeros((text_regions_p.shape[0],text_regions_p.shape[1],3)) + # img_con=cv2.fillPoly(img_con,pts=[polygons_of_drop_capitals[i_drop] ],color=(255,255,255)) + # img_con=cv2.fillPoly(img_con,pts=[contours_only_text_parent[j_cont]],color=(255,255,255)) + + # img_con=img_con.astype(np.uint8) + # #imgray = cv2.cvtColor(img_con, cv2.COLOR_BGR2GRAY) + # #ret, thresh = cv2.threshold(imgray, 0, 255, 0) + + # #contours_new,hierarchy=cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) + + # contours_new,hir_new=return_contours_of_image(img_con) + # contours_new_parent=return_parent_contours( contours_new,hir_new) + # #plt.imshow(img_con) + # #plt.show() + # try: + # if len(contours_new_parent)==1: + # #print(all_found_textline_polygons[j_cont][0]) + # cx_t,cy_t ,_, _, _ ,_,_= find_new_features_of_contours(all_found_textline_polygons[j_cont]) + # #print(all_box_coord[j_cont]) + # #print(cx_t) + # #print(cy_t) + # #print(cx_d[i_drop]) + # #print(cy_d[i_drop]) + # y_lines=all_box_coord[j_cont][0]+np.array(cy_t) + + # #print(y_lines) + + # arg_min=np.argmin(np.abs(y_lines-y_min_d[i_drop]) ) + # #print(arg_min) + + # cnt_nearest=np.copy(all_found_textline_polygons[j_cont][arg_min]) + # cnt_nearest[:,0]=all_found_textline_polygons[j_cont][arg_min][:,0]+all_box_coord[j_cont][2] + # cnt_nearest[:,1]=all_found_textline_polygons[j_cont][arg_min][:,1]+all_box_coord[j_cont][0] + + # img_textlines=np.zeros((text_regions_p.shape[0],text_regions_p.shape[1],3)) + # img_textlines=cv2.fillPoly(img_textlines,pts=[cnt_nearest],color=(255,255,255)) + # img_textlines=cv2.fillPoly(img_textlines,pts=[polygons_of_drop_capitals[i_drop] ],color=(255,255,255)) + + # img_textlines=img_textlines.astype(np.uint8) + # imgray = cv2.cvtColor(img_textlines, cv2.COLOR_BGR2GRAY) + # ret, thresh = cv2.threshold(imgray, 0, 255, 0) + + # contours_combined,hierarchy=cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) + + # areas_cnt_text=np.array([cv2.contourArea(contours_combined[j]) for j in range(len(contours_combined))]) + + # contours_biggest=contours_combined[np.argmax(areas_cnt_text)] + + # #print(np.shape(contours_biggest)) + # #print(contours_biggest[:]) + # contours_biggest[:,0,0]=contours_biggest[:,0,0]-all_box_coord[j_cont][2] + # contours_biggest[:,0,1]=contours_biggest[:,0,1]-all_box_coord[j_cont][0] + + # all_found_textline_polygons[j_cont][arg_min]=contours_biggest + # #print(contours_biggest) + # #plt.imshow(img_textlines[:,:,0]) + # #plt.show() + # else: + # pass + # except: + # pass return all_found_textline_polygons diff --git a/src/eynollah/utils/marginals.py b/src/eynollah/utils/marginals.py index c858fc0..6759942 100644 --- a/src/eynollah/utils/marginals.py +++ b/src/eynollah/utils/marginals.py @@ -12,7 +12,7 @@ def get_marginals(text_with_lines, text_regions, num_col, slope_deskew, light_ve mask_marginals = mask_marginals.astype(np.uint8) text_with_lines = text_with_lines.astype(np.uint8) - ##text_with_lines=cv2.erode(text_with_lines,self.kernel,iterations=3) + # text_with_lines=cv2.erode(text_with_lines,self.kernel,iterations=3) text_with_lines_eroded = cv2.erode(text_with_lines, kernel, iterations=5) @@ -34,7 +34,7 @@ def get_marginals(text_with_lines, text_regions, num_col, slope_deskew, light_ve thickness_along_y_percent = text_with_lines_y_eroded.max() / (float(text_with_lines.shape[0])) * 100 - #print(thickness_along_y_percent,'thickness_along_y_percent') + # print(thickness_along_y_percent,'thickness_along_y_percent') if thickness_along_y_percent < 30: min_textline_thickness = 8 @@ -191,25 +191,25 @@ def get_marginals(text_with_lines, text_regions, num_col, slope_deskew, light_ve text_regions = cv2.fillPoly(text_regions, pts=marginlas_should_be_main_text, color=(4, 4)) - #text_regions[:,:int(x_min_marginals_left[0])][text_regions[:,:int(x_min_marginals_left[0])]==1]=0 - #text_regions[:,int(x_min_marginals_right[0]):][text_regions[:,int(x_min_marginals_right[0]):]==1]=0 + # text_regions[:,:int(x_min_marginals_left[0])][text_regions[:,:int(x_min_marginals_left[0])]==1]=0 + # text_regions[:,int(x_min_marginals_right[0]):][text_regions[:,int(x_min_marginals_right[0]):]==1]=0 text_regions[:, :int(min_point_of_left_marginal)][ text_regions[:, :int(min_point_of_left_marginal)] == 1] = 0 text_regions[:, int(max_point_of_right_marginal):][ text_regions[:, int(max_point_of_right_marginal):] == 1] = 0 - ###text_regions[:,0:point_left][text_regions[:,0:point_left]==1]=4 + # text_regions[:,0:point_left][text_regions[:,0:point_left]==1]=4 - ###text_regions[:,point_right:][ text_regions[:,point_right:]==1]=4 - #plt.plot(region_sum_0) - #plt.plot(peaks,region_sum_0[peaks],'*') - #plt.show() + # text_regions[:,point_right:][ text_regions[:,point_right:]==1]=4 + # plt.plot(region_sum_0) + # plt.plot(peaks,region_sum_0[peaks],'*') + # plt.show() - #plt.imshow(text_regions) - #plt.show() + # plt.imshow(text_regions) + # plt.show() - #sys.exit() + # sys.exit() else: pass return text_regions diff --git a/src/eynollah/utils/separate_lines.py b/src/eynollah/utils/separate_lines.py index 3694ccf..e5fc381 100644 --- a/src/eynollah/utils/separate_lines.py +++ b/src/eynollah/utils/separate_lines.py @@ -217,14 +217,14 @@ def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help): textline_con_fil = filter_contours_area_of_image(img_patch, textline_con, hierarchy, max_area=1, min_area=0.0008) - y_diff_mean = np.mean(np.diff(peaks_new_tot)) #self.find_contours_mean_y_diff(textline_con_fil) + y_diff_mean = np.mean(np.diff(peaks_new_tot)) # self.find_contours_mean_y_diff(textline_con_fil) sigma_gaus = int(y_diff_mean * (7. / 40.0)) - #print(sigma_gaus,'sigma_gaus') + # print(sigma_gaus,'sigma_gaus') except: sigma_gaus = 12 if sigma_gaus < 3: sigma_gaus = 3 - #print(sigma_gaus,'sigma') + # print(sigma_gaus,'sigma') y_padded_smoothed = gaussian_filter1d(y_padded, sigma_gaus) y_padded_up_to_down = -y_padded + np.max(y_padded) @@ -279,21 +279,21 @@ def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help): peaks_new_tot.append(i1) peaks_new_tot = np.sort(peaks_new_tot) - ##plt.plot(y_padded_up_to_down_padded) - ##plt.plot(peaks_neg,y_padded_up_to_down_padded[peaks_neg],'*') - ##plt.show() + # plt.plot(y_padded_up_to_down_padded) + # plt.plot(peaks_neg,y_padded_up_to_down_padded[peaks_neg],'*') + # plt.show() - ##plt.plot(y_padded_up_to_down_padded) - ##plt.plot(peaks_neg_new,y_padded_up_to_down_padded[peaks_neg_new],'*') - ##plt.show() + # plt.plot(y_padded_up_to_down_padded) + # plt.plot(peaks_neg_new,y_padded_up_to_down_padded[peaks_neg_new],'*') + # plt.show() - ##plt.plot(y_padded_smoothed) - ##plt.plot(peaks,y_padded_smoothed[peaks],'*') - ##plt.show() + # plt.plot(y_padded_smoothed) + # plt.plot(peaks,y_padded_smoothed[peaks],'*') + # plt.show() - ##plt.plot(y_padded_smoothed) - ##plt.plot(peaks_new_tot,y_padded_smoothed[peaks_new_tot],'*') - ##plt.show() + # plt.plot(y_padded_smoothed) + # plt.plot(peaks_new_tot,y_padded_smoothed[peaks_new_tot],'*') + # plt.show() peaks = peaks_new_tot[:] peaks_neg = peaks_neg_new[:] else: @@ -327,29 +327,29 @@ def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help): dis_to_next_down = abs(peaks[jj] - peaks_neg[jj + 1]) if peaks_values[jj] > mean_value_of_peaks - std_value_of_peaks / 2.: - point_up = peaks[jj] + first_nonzero - int(1.3 * dis_to_next_up) ##+int(dis_to_next_up*1./4.0) - point_down = y_max_cont - 1 ##peaks[jj] + first_nonzero + int(1.3 * dis_to_next_down) #point_up# np.max(y_cont)#peaks[jj] + first_nonzero + int(1.4 * dis_to_next_down) ###-int(dis_to_next_down*1./4.0) + point_up = peaks[jj] + first_nonzero - int(1.3 * dis_to_next_up) # +int(dis_to_next_up*1./4.0) + point_down = y_max_cont - 1 # peaks[jj] + first_nonzero + int(1.3 * dis_to_next_down) #point_up# np.max(y_cont)#peaks[jj] + first_nonzero + int(1.4 * dis_to_next_down) # #-int(dis_to_next_down*1./4.0) else: - point_up = peaks[jj] + first_nonzero - int(1.4 * dis_to_next_up) ##+int(dis_to_next_up*1./4.0) - point_down = y_max_cont - 1 ##peaks[jj] + first_nonzero + int(1.6 * dis_to_next_down) #point_up# np.max(y_cont)#peaks[jj] + first_nonzero + int(1.4 * dis_to_next_down) ###-int(dis_to_next_down*1./4.0) + point_up = peaks[jj] + first_nonzero - int(1.4 * dis_to_next_up) # +int(dis_to_next_up*1./4.0) + point_down = y_max_cont - 1 # peaks[jj] + first_nonzero + int(1.6 * dis_to_next_down) #point_up# np.max(y_cont)#peaks[jj] + first_nonzero + int(1.4 * dis_to_next_down) # #-int(dis_to_next_down*1./4.0) point_down_narrow = peaks[jj] + first_nonzero + int( - 1.4 * dis_to_next_down) ###-int(dis_to_next_down*1./2) + 1.4 * dis_to_next_down) # #-int(dis_to_next_down*1./2) else: dis_to_next_up = abs(peaks[jj] - peaks_neg[jj]) dis_to_next_down = abs(peaks[jj] - peaks_neg[jj + 1]) if peaks_values[jj] > mean_value_of_peaks - std_value_of_peaks / 2.: - point_up = peaks[jj] + first_nonzero - int(1.1 * dis_to_next_up) ##+int(dis_to_next_up*1./4.0) + point_up = peaks[jj] + first_nonzero - int(1.1 * dis_to_next_up) # +int(dis_to_next_up*1./4.0) point_down = peaks[jj] + first_nonzero + int( - 1.1 * dis_to_next_down) ###-int(dis_to_next_down*1./4.0) + 1.1 * dis_to_next_down) # #-int(dis_to_next_down*1./4.0) else: - point_up = peaks[jj] + first_nonzero - int(1.23 * dis_to_next_up) ##+int(dis_to_next_up*1./4.0) + point_up = peaks[jj] + first_nonzero - int(1.23 * dis_to_next_up) # +int(dis_to_next_up*1./4.0) point_down = peaks[jj] + first_nonzero + int( - 1.33 * dis_to_next_down) ###-int(dis_to_next_down*1./4.0) + 1.33 * dis_to_next_down) # #-int(dis_to_next_down*1./4.0) point_down_narrow = peaks[jj] + first_nonzero + int( - 1.1 * dis_to_next_down) ###-int(dis_to_next_down*1./2) + 1.1 * dis_to_next_down) # #-int(dis_to_next_down*1./2) if point_down_narrow >= img_patch.shape[0]: point_down_narrow = img_patch.shape[0] - 2 @@ -422,8 +422,8 @@ def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help): else: x_min = np.min(xvinside) # max(x_min_interest,x_min_cont) x_max = np.max(xvinside) # min(x_max_interest,x_max_cont) - #x_min = x_min_cont - #x_max = x_max_cont + # x_min = x_min_cont + # x_max = x_max_cont y_min = y_min_cont y_max = y_max_cont @@ -469,7 +469,7 @@ def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help): dis_to_next = np.abs(peaks[1] - peaks[0]) for jj in range(len(peaks)): if jj == 0: - point_up = 0 #peaks[jj] + first_nonzero - int(1. / 1.7 * dis_to_next) + point_up = 0 # peaks[jj] + first_nonzero - int(1. / 1.7 * dis_to_next) if point_up < 0: point_up = 1 point_down = peaks_neg[1] + first_nonzero # peaks[jj] + first_nonzero + int(1. / 1.8 * dis_to_next) @@ -478,7 +478,7 @@ def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help): if point_down >= img_patch.shape[0]: point_down = img_patch.shape[0] - 2 try: - point_up = peaks_neg[2] + first_nonzero #peaks[jj] + first_nonzero - int(1. / 1.8 * dis_to_next) + point_up = peaks_neg[2] + first_nonzero # peaks[jj] + first_nonzero - int(1. / 1.8 * dis_to_next) except: point_up = peaks[jj] + first_nonzero - int(1. / 1.8 * dis_to_next) @@ -705,29 +705,29 @@ def separate_lines_vertical(img_patch, contour_text_interest, thetha): dis_to_next_down = abs(peaks[jj] - peaks_neg[jj + 1]) if peaks_values[jj] > mean_value_of_peaks - std_value_of_peaks / 2.0: - point_up = peaks[jj] + first_nonzero - int(1.3 * dis_to_next_up) ##+int(dis_to_next_up*1./4.0) - point_down = x_max_cont - 1 ##peaks[jj] + first_nonzero + int(1.3 * dis_to_next_down) #point_up# np.max(y_cont)#peaks[jj] + first_nonzero + int(1.4 * dis_to_next_down) ###-int(dis_to_next_down*1./4.0) + point_up = peaks[jj] + first_nonzero - int(1.3 * dis_to_next_up) # +int(dis_to_next_up*1./4.0) + point_down = x_max_cont - 1 # peaks[jj] + first_nonzero + int(1.3 * dis_to_next_down) #point_up# np.max(y_cont)#peaks[jj] + first_nonzero + int(1.4 * dis_to_next_down) # #-int(dis_to_next_down*1./4.0) else: - point_up = peaks[jj] + first_nonzero - int(1.4 * dis_to_next_up) ##+int(dis_to_next_up*1./4.0) - point_down = x_max_cont - 1 ##peaks[jj] + first_nonzero + int(1.6 * dis_to_next_down) #point_up# np.max(y_cont)#peaks[jj] + first_nonzero + int(1.4 * dis_to_next_down) ###-int(dis_to_next_down*1./4.0) + point_up = peaks[jj] + first_nonzero - int(1.4 * dis_to_next_up) # +int(dis_to_next_up*1./4.0) + point_down = x_max_cont - 1 # peaks[jj] + first_nonzero + int(1.6 * dis_to_next_down) #point_up# np.max(y_cont)#peaks[jj] + first_nonzero + int(1.4 * dis_to_next_down) # #-int(dis_to_next_down*1./4.0) point_down_narrow = peaks[jj] + first_nonzero + int( - 1.4 * dis_to_next_down) ###-int(dis_to_next_down*1./2) + 1.4 * dis_to_next_down) # #-int(dis_to_next_down*1./2) else: dis_to_next_up = abs(peaks[jj] - peaks_neg[jj]) dis_to_next_down = abs(peaks[jj] - peaks_neg[jj + 1]) if peaks_values[jj] > mean_value_of_peaks - std_value_of_peaks / 2.0: - point_up = peaks[jj] + first_nonzero - int(1.1 * dis_to_next_up) ##+int(dis_to_next_up*1./4.0) + point_up = peaks[jj] + first_nonzero - int(1.1 * dis_to_next_up) # +int(dis_to_next_up*1./4.0) point_down = peaks[jj] + first_nonzero + int( - 1.1 * dis_to_next_down) ###-int(dis_to_next_down*1./4.0) + 1.1 * dis_to_next_down) # #-int(dis_to_next_down*1./4.0) else: - point_up = peaks[jj] + first_nonzero - int(1.23 * dis_to_next_up) ##+int(dis_to_next_up*1./4.0) + point_up = peaks[jj] + first_nonzero - int(1.23 * dis_to_next_up) # +int(dis_to_next_up*1./4.0) point_down = peaks[jj] + first_nonzero + int( - 1.33 * dis_to_next_down) ###-int(dis_to_next_down*1./4.0) + 1.33 * dis_to_next_down) # #-int(dis_to_next_down*1./4.0) point_down_narrow = peaks[jj] + first_nonzero + int( - 1.1 * dis_to_next_down) ###-int(dis_to_next_down*1./2) + 1.1 * dis_to_next_down) # #-int(dis_to_next_down*1./2) if point_down_narrow >= img_patch.shape[0]: point_down_narrow = img_patch.shape[0] - 2 @@ -1104,8 +1104,8 @@ def separate_lines_new_inside_tiles2(img_patch, thetha): std_value_of_peaks = np.std(y_padded_smoothed[peaks]) peaks_values = y_padded_smoothed[peaks] - ###peaks_neg = peaks_neg - 20 - 20 - ###peaks = peaks - 20 + # #peaks_neg = peaks_neg - 20 - 20 + # #peaks = peaks - 20 peaks_neg_true = peaks_neg[:] peaks_pos_true = peaks[:] @@ -1125,14 +1125,14 @@ def separate_lines_new_inside_tiles2(img_patch, thetha): peaks_pos_true = peaks_pos_true - 20 for i in range(len(peaks_pos_true)): - ##img_patch[peaks_pos_true[i]-8:peaks_pos_true[i]+8,:]=1 + # img_patch[peaks_pos_true[i]-8:peaks_pos_true[i]+8,:]=1 img_patch[peaks_pos_true[i] - 6: peaks_pos_true[i] + 6, :] = 1 else: pass kernel = np.ones((5, 5), np.uint8) # img_patch = cv2.erode(img_patch,kernel,iterations = 3) - #######################img_patch = cv2.erode(img_patch,kernel,iterations = 2) + # # # # # # # # # # # #img_patch = cv2.erode(img_patch,kernel,iterations = 2) img_patch = cv2.erode(img_patch, kernel, iterations=1) return img_patch @@ -1156,8 +1156,8 @@ def separate_lines_new_inside_tiles(img_path, thetha): mada_n = img_path.sum(axis=1) - ##plt.plot(mada_n) - ##plt.show() + # plt.plot(mada_n) + # plt.show() first_nonzero = 0 # (next((i for i, x in enumerate(mada_n) if x), None)) @@ -1252,7 +1252,7 @@ def separate_lines_new_inside_tiles(img_path, thetha): #plt.xlabel('Dichte') #plt.ylabel('Y') - ##plt.plot([0,len(y)], [grenze,grenze]) + # plt.plot([0,len(y)], [grenze,grenze]) #plt.show() """ peaks_neg_true = peaks_neg_true - 20 - 20 @@ -1300,7 +1300,7 @@ def separate_lines_vertical_cont(img_patch, contour_text_interest, thetha, box_i contours_imgs, hierarchy, max_area=max_area, min_area=min_area) cont_final = [] - ###print(add_boxes_coor_into_textlines,'ikki') + # #print(add_boxes_coor_into_textlines,'ikki') for i in range(len(contours_imgs)): img_contour = np.zeros((cnts_images.shape[0], cnts_images.shape[1], 3)) img_contour = cv2.fillPoly(img_contour, pts=[contours_imgs[i]], color=(255, 255, 255)) @@ -1311,16 +1311,16 @@ def separate_lines_vertical_cont(img_patch, contour_text_interest, thetha, box_i _, threshrot = cv2.threshold(imgrayrot, 0, 255, 0) contours_text_rot, _ = cv2.findContours(threshrot.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) - ##contour_text_copy[:, 0, 0] = contour_text_copy[:, 0, 0] - box_ind[ - ##0] - ##contour_text_copy[:, 0, 1] = contour_text_copy[:, 0, 1] - box_ind[1] - ##if add_boxes_coor_into_textlines: - ##print(np.shape(contours_text_rot[0]),'sjppo') - ##contours_text_rot[0][:, 0, 0]=contours_text_rot[0][:, 0, 0] + box_ind[0] - ##contours_text_rot[0][:, 0, 1]=contours_text_rot[0][:, 0, 1] + box_ind[1] + # contour_text_copy[:, 0, 0] = contour_text_copy[:, 0, 0] - box_ind[ + # 0] + # contour_text_copy[:, 0, 1] = contour_text_copy[:, 0, 1] - box_ind[1] + # if add_boxes_coor_into_textlines: + # print(np.shape(contours_text_rot[0]),'sjppo') + # contours_text_rot[0][:, 0, 0]=contours_text_rot[0][:, 0, 0] + box_ind[0] + # contours_text_rot[0][:, 0, 1]=contours_text_rot[0][:, 0, 1] + box_ind[1] cont_final.append(contours_text_rot[0]) - ##print(cont_final,'nadizzzz') + # print(cont_final,'nadizzzz') return None, cont_final @@ -1549,7 +1549,7 @@ def return_deskew_slop(img_patch_org, sigma_des, n_tot_angles=100, plotter.save_plot_of_textline_density(img_patch_org) img_int = np.zeros((img_patch_org.shape[0], img_patch_org.shape[1])) - img_int[:, :] = img_patch_org[:, :] #img_patch_org[:,:,0] + img_int[:, :] = img_patch_org[:, :] # img_patch_org[:,:,0] max_shape = np.max(img_int.shape) img_resized = np.zeros((int(max_shape * 1.1), int(max_shape * 1.1))) @@ -1557,25 +1557,25 @@ def return_deskew_slop(img_patch_org, sigma_des, n_tot_angles=100, onset_x = int((img_resized.shape[1] - img_int.shape[1]) / 2.) onset_y = int((img_resized.shape[0] - img_int.shape[0]) / 2.) - #img_resized=np.zeros((int( img_int.shape[0]*(1.8) ) , int( img_int.shape[1]*(2.6) ) )) - #img_resized[ int( img_int.shape[0]*(.4)):int( img_int.shape[0]*(.4))+img_int.shape[0] , int( img_int.shape[1]*(.8)):int( img_int.shape[1]*(.8))+img_int.shape[1] ]=img_int[:,:] + # img_resized=np.zeros((int( img_int.shape[0]*(1.8) ) , int( img_int.shape[1]*(2.6) ) )) + # img_resized[ int( img_int.shape[0]*(.4)):int( img_int.shape[0]*(.4))+img_int.shape[0] , int( img_int.shape[1]*(.8)):int( img_int.shape[1]*(.8))+img_int.shape[1] ]=img_int[:,:] img_resized[onset_y:onset_y + img_int.shape[0], onset_x:onset_x + img_int.shape[1]] = img_int[:, :] - #print(img_resized.shape,'img_resizedshape') - #plt.imshow(img_resized) - #plt.show() + # print(img_resized.shape,'img_resizedshape') + # plt.imshow(img_resized) + # plt.show() if main_page and img_patch_org.shape[1] > img_patch_org.shape[0]: - #plt.imshow(img_resized) - #plt.show() + # plt.imshow(img_resized) + # plt.show() angles = np.array([-45, 0, 45, 90, ]) angle = get_smallest_skew(img_resized, sigma_des, angles, map=map, logger=logger, plotter=plotter) angles = np.linspace(angle - 22.5, angle + 22.5, n_tot_angles) angle = get_smallest_skew(img_resized, sigma_des, angles, map=map, logger=logger, plotter=plotter) elif main_page: - #plt.imshow(img_resized) - #plt.show() - angles = np.linspace(-12, 12, n_tot_angles) #np.array([0 , 45 , 90 , -45]) + # plt.imshow(img_resized) + # plt.show() + angles = np.linspace(-12, 12, n_tot_angles) # np.array([0 , 45 , 90 , -45]) angle = get_smallest_skew(img_resized, sigma_des, angles, map=map, logger=logger, plotter=plotter) early_slope_edge = 11 @@ -1672,12 +1672,12 @@ def do_work_of_slopes_new( all_text_region_raw = textline_mask_tot_ea[y: y + h, x: x + w].copy() mask_only_con_region = mask_only_con_region[y: y + h, x: x + w] - ##plt.imshow(textline_mask_tot_ea) - ##plt.show() - ##plt.imshow(all_text_region_raw) - ##plt.show() - ##plt.imshow(mask_only_con_region) - ##plt.show() + # plt.imshow(textline_mask_tot_ea) + # plt.show() + # plt.imshow(all_text_region_raw) + # plt.show() + # plt.imshow(mask_only_con_region) + # plt.show() all_text_region_raw[mask_only_con_region == 0] = 0 cnt_clean_rot = textline_contours_postprocessing(all_text_region_raw, slope_for_all, contour_par, box_text) @@ -1746,7 +1746,7 @@ def do_work_of_slopes_new_curved( logger=logger, plotter=plotter) # new line added - ##print(np.shape(textline_rotated_separated),np.shape(mask_biggest)) + # print(np.shape(textline_rotated_separated),np.shape(mask_biggest)) textline_rotated_separated[mask_region_in_patch_region[:, :] != 1] = 0 # till here diff --git a/src/eynollah/writer.py b/src/eynollah/writer.py index 369b6e8..ad4d5a9 100644 --- a/src/eynollah/writer.py +++ b/src/eynollah/writer.py @@ -174,7 +174,7 @@ class EynollahXmlWriter: if ocr_all_textlines_textregion: textline.set_TextEquiv([TextEquivType(Unicode=ocr_all_textlines_textregion[j])]) text_region.add_TextLine(textline) - #region_bboxes = all_box_coord[region_idx] + # region_bboxes = all_box_coord[region_idx] points_co = '' for idx_contour_textline, contour_textline in enumerate(all_found_textline_polygons[j]): if len(contour_textline) == 2: @@ -342,9 +342,9 @@ class EynollahXmlWriter: points=self.calculate_polygon_coords(found_polygons_drop_capitals[mm], page_coord))) page.add_TextRegion(dropcapital) - ###all_box_coord_drop = None - ###slopes_drop = None - ###self.serialize_lines_in_dropcapital(dropcapital, [found_polygons_drop_capitals[mm]], mm, page_coord, all_box_coord_drop, slopes_drop, counter, ocr_all_textlines_textregion=None) + # all_box_coord_drop = None + # slopes_drop = None + # self.serialize_lines_in_dropcapital(dropcapital, [found_polygons_drop_capitals[mm]], mm, page_coord, all_box_coord_drop, slopes_drop, counter, ocr_all_textlines_textregion=None) for mm in range(len(found_polygons_text_region_img)): page.add_ImageRegion(ImageRegionType(id=counter.next_region_id, Coords=CoordsType( diff --git a/tests/base.py b/tests/base.py index 4ff508e..841355d 100644 --- a/tests/base.py +++ b/tests/base.py @@ -45,7 +45,7 @@ class CapturingTestCase(TestCase): """ self.capture_out_err() # XXX snapshot just before executing the CLI code = 0 - sys.argv[1:] = args # XXX necessary because sys.argv reflects pytest args not cli args + sys.argv[1:] = args # XXX necessary because sys.argv reflects pytest args not cli args try: cli.main(args=args) except SystemExit as e: