|
|
|
@ -38,7 +38,8 @@ sys.stderr = stderr
|
|
|
|
|
tf.get_logger().setLevel("ERROR")
|
|
|
|
|
warnings.filterwarnings("ignore")
|
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
|
from tensorflow.python.keras.backend import set_session
|
|
|
|
|
# use tf1 compatibility for keras backend
|
|
|
|
|
from tensorflow.compat.v1.keras.backend import set_session
|
|
|
|
|
from tensorflow.keras import layers
|
|
|
|
|
|
|
|
|
|
from .utils.contour import (
|
|
|
|
@ -253,11 +254,11 @@ class Eynollah:
|
|
|
|
|
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_machine_dir = dir_models + "/model_ens_reading_order_machine_based"
|
|
|
|
|
self.model_region_dir_p_1_2_sp_np = dir_models + "/modelens_e_l_all_sp_0_1_2_3_4_171024"#"/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_fully_new = dir_models + "/model_2_full_layout_new_trans"
|
|
|
|
|
self.model_region_dir_fully = dir_models + "/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_p_ens_light_only_images_extraction = dir_models + "/eynollah-main-regions_20231127_672_org_ens_11_13_16_17_18"
|
|
|
|
|
if self.textline_light:
|
|
|
|
|
self.model_textline_dir = dir_models + "/modelens_textline_0_1__2_4_16092024"#"/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"#
|
|
|
|
|
else:
|
|
|
|
@ -1237,14 +1238,12 @@ class Eynollah:
|
|
|
|
|
|
|
|
|
|
batch_indexer = batch_indexer + 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if batch_indexer == n_batch_inference:
|
|
|
|
|
label_p_pred = model.predict(img_patch,verbose=0)
|
|
|
|
|
|
|
|
|
|
seg = np.argmax(label_p_pred, axis=3)
|
|
|
|
|
|
|
|
|
|
if thresholding_for_some_classes_in_light_version:
|
|
|
|
|
|
|
|
|
|
seg_art = label_p_pred[:,:,:,4]
|
|
|
|
|
seg_art[seg_art<0.2] =0
|
|
|
|
|
seg_art[seg_art>0] =1
|
|
|
|
@ -1253,9 +1252,7 @@ class Eynollah:
|
|
|
|
|
seg_line[seg_line>0.1] =1
|
|
|
|
|
seg_line[seg_line<1] =0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
seg[seg_art==1]=4
|
|
|
|
|
##seg[seg_background==1]=0
|
|
|
|
|
seg[(seg_line==1) & (seg==0)]=3
|
|
|
|
|
if thresholding_for_artificial_class_in_light_version:
|
|
|
|
|
seg_art = label_p_pred[:,:,:,2]
|
|
|
|
@ -1383,7 +1380,6 @@ class Eynollah:
|
|
|
|
|
|
|
|
|
|
indexer_inside_batch = indexer_inside_batch +1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
list_i_s = []
|
|
|
|
|
list_j_s = []
|
|
|
|
|
list_x_u = []
|
|
|
|
@ -1392,7 +1388,6 @@ class Eynollah:
|
|
|
|
|
list_y_d = []
|
|
|
|
|
|
|
|
|
|
batch_indexer = 0
|
|
|
|
|
|
|
|
|
|
img_patch = np.zeros((n_batch_inference, img_height_model, img_width_model, 3))
|
|
|
|
|
|
|
|
|
|
prediction_true = prediction_true.astype(np.uint8)
|
|
|
|
@ -2271,6 +2266,7 @@ class Eynollah:
|
|
|
|
|
polygons_of_images_fin.append(np.array([[page_coord_img[2], page_coord_img[0]], [page_coord_img[3], page_coord_img[0]], [page_coord_img[3], page_coord_img[1]], [page_coord_img[2], page_coord_img[1]]]) )
|
|
|
|
|
|
|
|
|
|
return text_regions_p_true, erosion_hurts, polygons_lines_xml, polygons_of_images_fin, image_page, page_coord, cont_page
|
|
|
|
|
|
|
|
|
|
def get_regions_light_v(self,img,is_image_enhanced, num_col_classifier, skip_layout_and_reading_order=False):
|
|
|
|
|
self.logger.debug("enter get_regions_light_v")
|
|
|
|
|
t_in = time.time()
|
|
|
|
@ -3228,11 +3224,9 @@ class Eynollah:
|
|
|
|
|
return prediction_table_erode.astype(np.int16)
|
|
|
|
|
|
|
|
|
|
def run_graphics_and_columns_light(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')
|
|
|
|
|
t_in_gr = time.time()
|
|
|
|
|
|
|
|
|
|
img_g = self.imread(grayscale=True, uint8=True)
|
|
|
|
|
|
|
|
|
|
img_g3 = np.zeros((img_g.shape[0], img_g.shape[1], 3))
|
|
|
|
@ -3721,7 +3715,6 @@ class Eynollah:
|
|
|
|
|
|
|
|
|
|
height1 =672#448
|
|
|
|
|
width1 = 448#224
|
|
|
|
|
t0_tot = time.time()
|
|
|
|
|
|
|
|
|
|
height2 =672#448
|
|
|
|
|
width2= 448#224
|
|
|
|
@ -4854,6 +4847,23 @@ class Eynollah:
|
|
|
|
|
self.reset_file_name_dir(os.path.join(self.dir_in,img_name))
|
|
|
|
|
#print("text region early -11 in %.1fs", time.time() - t0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if self.extract_only_images:
|
|
|
|
|
img_res, is_image_enhanced, num_col_classifier, num_column_is_classified = self.run_enhancement(self.light_version)
|
|
|
|
|
self.logger.info("Enhancing took %.1fs ", time.time() - t0)
|
|
|
|
|
|
|
|
|
|
text_regions_p_1 ,erosion_hurts, polygons_lines_xml,polygons_of_images,image_page, page_coord, cont_page = self.get_regions_light_v_extract_only_images(img_res, is_image_enhanced, num_col_classifier)
|
|
|
|
|
ocr_all_textlines = None
|
|
|
|
|
pcgts = self.writer.build_pagexml_no_full_layout([], page_coord, [], [], [], [], polygons_of_images, [], [], [], [], [], cont_page, [], [], ocr_all_textlines)
|
|
|
|
|
|
|
|
|
|
if self.plotter:
|
|
|
|
|
self.plotter.write_images_into_directory(polygons_of_images, image_page)
|
|
|
|
|
|
|
|
|
|
if self.dir_in:
|
|
|
|
|
self.writer.write_pagexml(pcgts)
|
|
|
|
|
else:
|
|
|
|
|
return pcgts
|
|
|
|
|
else:
|
|
|
|
|
img_res, is_image_enhanced, num_col_classifier, num_column_is_classified = self.run_enhancement(self.light_version)
|
|
|
|
|
self.logger.info("Enhancing took %.1fs ", time.time() - t0)
|
|
|
|
|
#print("text region early -1 in %.1fs", time.time() - t0)
|
|
|
|
|