|
|
@ -13,11 +13,10 @@ import time
|
|
|
|
import warnings
|
|
|
|
import warnings
|
|
|
|
from pathlib import Path
|
|
|
|
from pathlib import Path
|
|
|
|
from multiprocessing import Process, Queue, cpu_count
|
|
|
|
from multiprocessing import Process, Queue, cpu_count
|
|
|
|
|
|
|
|
import gc
|
|
|
|
from ocrd_utils import getLogger
|
|
|
|
from ocrd_utils import getLogger
|
|
|
|
import cv2
|
|
|
|
import cv2
|
|
|
|
import numpy as np
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
|
|
|
|
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
|
|
|
|
stderr = sys.stderr
|
|
|
|
stderr = sys.stderr
|
|
|
|
sys.stderr = open(os.devnull, "w")
|
|
|
|
sys.stderr = open(os.devnull, "w")
|
|
|
@ -149,7 +148,7 @@ class Eynollah:
|
|
|
|
|
|
|
|
|
|
|
|
def predict_enhancement(self, img):
|
|
|
|
def predict_enhancement(self, img):
|
|
|
|
self.logger.debug("enter predict_enhancement")
|
|
|
|
self.logger.debug("enter predict_enhancement")
|
|
|
|
model_enhancement, _ = self.start_new_session_and_model(self.model_dir_of_enhancement)
|
|
|
|
model_enhancement, session_enhancement = self.start_new_session_and_model(self.model_dir_of_enhancement)
|
|
|
|
|
|
|
|
|
|
|
|
img_height_model = model_enhancement.layers[len(model_enhancement.layers) - 1].output_shape[1]
|
|
|
|
img_height_model = model_enhancement.layers[len(model_enhancement.layers) - 1].output_shape[1]
|
|
|
|
img_width_model = model_enhancement.layers[len(model_enhancement.layers) - 1].output_shape[2]
|
|
|
|
img_width_model = model_enhancement.layers[len(model_enhancement.layers) - 1].output_shape[2]
|
|
|
@ -230,6 +229,10 @@ class Eynollah:
|
|
|
|
prediction_true[index_y_d + margin : index_y_u - margin, index_x_d + margin : index_x_u - margin, :] = seg
|
|
|
|
prediction_true[index_y_d + margin : index_y_u - margin, index_x_d + margin : index_x_u - margin, :] = seg
|
|
|
|
|
|
|
|
|
|
|
|
prediction_true = prediction_true.astype(int)
|
|
|
|
prediction_true = prediction_true.astype(int)
|
|
|
|
|
|
|
|
session_enhancement.close()
|
|
|
|
|
|
|
|
del model_enhancement
|
|
|
|
|
|
|
|
del session_enhancement
|
|
|
|
|
|
|
|
gc.collect()
|
|
|
|
|
|
|
|
|
|
|
|
return prediction_true
|
|
|
|
return prediction_true
|
|
|
|
|
|
|
|
|
|
|
@ -325,7 +328,13 @@ class Eynollah:
|
|
|
|
|
|
|
|
|
|
|
|
session_col_classifier.close()
|
|
|
|
session_col_classifier.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
del model_num_classifier
|
|
|
|
|
|
|
|
del session_col_classifier
|
|
|
|
|
|
|
|
|
|
|
|
K.clear_session()
|
|
|
|
K.clear_session()
|
|
|
|
|
|
|
|
gc.collect()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
img_new, _ = self.calculate_width_height_by_columns(img, num_col, width_early, label_p_pred)
|
|
|
|
img_new, _ = self.calculate_width_height_by_columns(img, num_col, width_early, label_p_pred)
|
|
|
|
|
|
|
|
|
|
|
@ -376,6 +385,9 @@ class Eynollah:
|
|
|
|
num_column_is_classified = True
|
|
|
|
num_column_is_classified = True
|
|
|
|
image_res = np.copy(img)
|
|
|
|
image_res = np.copy(img)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
session_col_classifier.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.logger.debug("exit resize_and_enhance_image_with_column_classifier")
|
|
|
|
self.logger.debug("exit resize_and_enhance_image_with_column_classifier")
|
|
|
|
return is_image_enhanced, img, image_res, num_col, num_column_is_classified
|
|
|
|
return is_image_enhanced, img, image_res, num_col, num_column_is_classified
|
|
|
|
|
|
|
|
|
|
|
@ -429,7 +441,7 @@ class Eynollah:
|
|
|
|
self.writer.height_org = self.height_org
|
|
|
|
self.writer.height_org = self.height_org
|
|
|
|
self.writer.width_org = self.width_org
|
|
|
|
self.writer.width_org = self.width_org
|
|
|
|
|
|
|
|
|
|
|
|
def start_new_session_and_model(self, model_dir):
|
|
|
|
def start_new_session_and_model_old(self, model_dir):
|
|
|
|
self.logger.debug("enter start_new_session_and_model (model_dir=%s)", model_dir)
|
|
|
|
self.logger.debug("enter start_new_session_and_model (model_dir=%s)", model_dir)
|
|
|
|
config = tf.ConfigProto()
|
|
|
|
config = tf.ConfigProto()
|
|
|
|
config.gpu_options.allow_growth = True
|
|
|
|
config.gpu_options.allow_growth = True
|
|
|
@ -439,6 +451,15 @@ class Eynollah:
|
|
|
|
|
|
|
|
|
|
|
|
return model, session
|
|
|
|
return model, session
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
|
|
|
model = load_model(model_dir, compile=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return model, session
|
|
|
|
|
|
|
|
|
|
|
|
def do_prediction(self, patches, img, model, marginal_of_patch_percent=0.1):
|
|
|
|
def do_prediction(self, patches, img, model, marginal_of_patch_percent=0.1):
|
|
|
|
self.logger.debug("enter do_prediction")
|
|
|
|
self.logger.debug("enter do_prediction")
|
|
|
|
|
|
|
|
|
|
|
@ -554,6 +575,8 @@ class Eynollah:
|
|
|
|
prediction_true[index_y_d + margin : index_y_u - margin, index_x_d + margin : index_x_u - margin, :] = seg_color
|
|
|
|
prediction_true[index_y_d + margin : index_y_u - margin, index_x_d + margin : index_x_u - margin, :] = seg_color
|
|
|
|
|
|
|
|
|
|
|
|
prediction_true = prediction_true.astype(np.uint8)
|
|
|
|
prediction_true = prediction_true.astype(np.uint8)
|
|
|
|
|
|
|
|
del model
|
|
|
|
|
|
|
|
gc.collect()
|
|
|
|
return prediction_true
|
|
|
|
return prediction_true
|
|
|
|
|
|
|
|
|
|
|
|
def early_page_for_num_of_column_classification(self):
|
|
|
|
def early_page_for_num_of_column_classification(self):
|
|
|
@ -574,7 +597,10 @@ class Eynollah:
|
|
|
|
box = [x, y, w, h]
|
|
|
|
box = [x, y, w, h]
|
|
|
|
croped_page, page_coord = crop_image_inside_box(box, img)
|
|
|
|
croped_page, page_coord = crop_image_inside_box(box, img)
|
|
|
|
session_page.close()
|
|
|
|
session_page.close()
|
|
|
|
|
|
|
|
del model_page
|
|
|
|
|
|
|
|
del session_page
|
|
|
|
|
|
|
|
gc.collect()
|
|
|
|
|
|
|
|
K.clear_session()
|
|
|
|
self.logger.debug("exit early_page_for_num_of_column_classification")
|
|
|
|
self.logger.debug("exit early_page_for_num_of_column_classification")
|
|
|
|
return croped_page, page_coord
|
|
|
|
return croped_page, page_coord
|
|
|
|
|
|
|
|
|
|
|
@ -606,7 +632,9 @@ class Eynollah:
|
|
|
|
croped_page, page_coord = crop_image_inside_box(box, self.image)
|
|
|
|
croped_page, page_coord = crop_image_inside_box(box, self.image)
|
|
|
|
cont_page.append(np.array([[page_coord[2], page_coord[0]], [page_coord[3], page_coord[0]], [page_coord[3], page_coord[1]], [page_coord[2], page_coord[1]]]))
|
|
|
|
cont_page.append(np.array([[page_coord[2], page_coord[0]], [page_coord[3], page_coord[0]], [page_coord[3], page_coord[1]], [page_coord[2], page_coord[1]]]))
|
|
|
|
session_page.close()
|
|
|
|
session_page.close()
|
|
|
|
|
|
|
|
del model_page
|
|
|
|
|
|
|
|
del session_page
|
|
|
|
|
|
|
|
gc.collect()
|
|
|
|
K.clear_session()
|
|
|
|
K.clear_session()
|
|
|
|
self.logger.debug("exit extract_page")
|
|
|
|
self.logger.debug("exit extract_page")
|
|
|
|
return croped_page, page_coord, cont_page
|
|
|
|
return croped_page, page_coord, cont_page
|
|
|
@ -704,6 +732,10 @@ class Eynollah:
|
|
|
|
prediction_regions = resize_image(prediction_regions, img_height_h, img_width_h)
|
|
|
|
prediction_regions = resize_image(prediction_regions, img_height_h, img_width_h)
|
|
|
|
|
|
|
|
|
|
|
|
session_region.close()
|
|
|
|
session_region.close()
|
|
|
|
|
|
|
|
del model_region
|
|
|
|
|
|
|
|
del session_region
|
|
|
|
|
|
|
|
gc.collect()
|
|
|
|
|
|
|
|
|
|
|
|
self.logger.debug("exit extract_text_regions")
|
|
|
|
self.logger.debug("exit extract_text_regions")
|
|
|
|
return prediction_regions, prediction_regions2
|
|
|
|
return prediction_regions, prediction_regions2
|
|
|
|
|
|
|
|
|
|
|
@ -1000,11 +1032,10 @@ class Eynollah:
|
|
|
|
prediction_textline = resize_image(prediction_textline, img_h, img_w)
|
|
|
|
prediction_textline = resize_image(prediction_textline, img_h, img_w)
|
|
|
|
prediction_textline_longshot = self.do_prediction(False, img, model_textline)
|
|
|
|
prediction_textline_longshot = self.do_prediction(False, img, model_textline)
|
|
|
|
prediction_textline_longshot_true_size = resize_image(prediction_textline_longshot, img_h, img_w)
|
|
|
|
prediction_textline_longshot_true_size = resize_image(prediction_textline_longshot, img_h, img_w)
|
|
|
|
##plt.imshow(prediction_textline_streched[:,:,0])
|
|
|
|
|
|
|
|
##plt.show()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
session_textline.close()
|
|
|
|
session_textline.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return prediction_textline[:, :, 0], prediction_textline_longshot_true_size[:, :, 0]
|
|
|
|
return prediction_textline[:, :, 0], prediction_textline_longshot_true_size[:, :, 0]
|
|
|
|
|
|
|
|
|
|
|
|
def do_work_of_slopes(self, q, poly, box_sub, boxes_per_process, textline_mask_tot, contours_per_process):
|
|
|
|
def do_work_of_slopes(self, q, poly, box_sub, boxes_per_process, textline_mask_tot, contours_per_process):
|
|
|
@ -1071,18 +1102,22 @@ class Eynollah:
|
|
|
|
##plt.show()
|
|
|
|
##plt.show()
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
session_region.close()
|
|
|
|
session_region.close()
|
|
|
|
|
|
|
|
del model_region
|
|
|
|
|
|
|
|
del session_region
|
|
|
|
|
|
|
|
gc.collect()
|
|
|
|
|
|
|
|
|
|
|
|
model_region, session_region = self.start_new_session_and_model(self.model_region_dir_p2)
|
|
|
|
model_region, session_region = self.start_new_session_and_model(self.model_region_dir_p2)
|
|
|
|
img = resize_image(img_org, int(img_org.shape[0]), int(img_org.shape[1]))
|
|
|
|
img = resize_image(img_org, int(img_org.shape[0]), int(img_org.shape[1]))
|
|
|
|
prediction_regions_org2 = self.do_prediction(True, img, model_region, 0.2)
|
|
|
|
prediction_regions_org2 = self.do_prediction(True, img, model_region, 0.2)
|
|
|
|
prediction_regions_org2=resize_image(prediction_regions_org2, img_height_h, img_width_h )
|
|
|
|
prediction_regions_org2=resize_image(prediction_regions_org2, img_height_h, img_width_h )
|
|
|
|
|
|
|
|
|
|
|
|
#plt.imshow(prediction_regions_org2[:,:,0])
|
|
|
|
|
|
|
|
#plt.show()
|
|
|
|
|
|
|
|
##prediction_regions_org=prediction_regions_org[:,:,0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
session_region.close()
|
|
|
|
session_region.close()
|
|
|
|
|
|
|
|
del model_region
|
|
|
|
|
|
|
|
del session_region
|
|
|
|
|
|
|
|
gc.collect()
|
|
|
|
|
|
|
|
|
|
|
|
mask_zeros2 = (prediction_regions_org2[:,:,0] == 0)
|
|
|
|
mask_zeros2 = (prediction_regions_org2[:,:,0] == 0)
|
|
|
|
mask_lines2 = (prediction_regions_org2[:,:,0] == 3)
|
|
|
|
mask_lines2 = (prediction_regions_org2[:,:,0] == 3)
|
|
|
@ -1303,7 +1338,7 @@ class Eynollah:
|
|
|
|
arg_order_v = indexes_sorted_main[zahler]
|
|
|
|
arg_order_v = indexes_sorted_main[zahler]
|
|
|
|
order_by_con_main[args_contours_box[indexes_by_type_main[zahler]]] = np.where(indexes_sorted == arg_order_v)[0][0] + ref_point
|
|
|
|
order_by_con_main[args_contours_box[indexes_by_type_main[zahler]]] = np.where(indexes_sorted == arg_order_v)[0][0] + ref_point
|
|
|
|
|
|
|
|
|
|
|
|
for jji, _ in range(len(id_of_texts)):
|
|
|
|
for jji, _ in enumerate(id_of_texts):
|
|
|
|
order_of_texts_tot.append(order_of_texts[jji] + ref_point)
|
|
|
|
order_of_texts_tot.append(order_of_texts[jji] + ref_point)
|
|
|
|
id_of_texts_tot.append(id_of_texts[jji])
|
|
|
|
id_of_texts_tot.append(id_of_texts[jji])
|
|
|
|
ref_point += len(id_of_texts)
|
|
|
|
ref_point += len(id_of_texts)
|
|
|
|