|
|
|
@ -705,7 +705,9 @@ class eynollah:
|
|
|
|
|
del img
|
|
|
|
|
del imgray
|
|
|
|
|
|
|
|
|
|
K.clear_session()
|
|
|
|
|
gc.collect()
|
|
|
|
|
self.logger.debug("exit extract_page")
|
|
|
|
|
return croped_page, page_coord
|
|
|
|
|
|
|
|
|
|
def extract_text_regions(self, img, patches, cols):
|
|
|
|
@ -2140,6 +2142,45 @@ class eynollah:
|
|
|
|
|
return self.do_order_of_regions_full_layout(*args, **kwargs)
|
|
|
|
|
return self.do_order_of_regions_no_full_layout(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
def run_graphics_and_columns(self, text_regions_p_1, num_column_is_classified):
|
|
|
|
|
img_g = cv2.imread(self.image_filename, cv2.IMREAD_GRAYSCALE)
|
|
|
|
|
img_g = img_g.astype(np.uint8)
|
|
|
|
|
|
|
|
|
|
img_g3 = np.zeros((img_g.shape[0], img_g.shape[1], 3))
|
|
|
|
|
img_g3 = img_g3.astype(np.uint8)
|
|
|
|
|
img_g3[:, :, 0] = img_g[:, :]
|
|
|
|
|
img_g3[:, :, 1] = img_g[:, :]
|
|
|
|
|
img_g3[:, :, 2] = img_g[:, :]
|
|
|
|
|
|
|
|
|
|
image_page, page_coord = self.extract_page()
|
|
|
|
|
if self.plotter:
|
|
|
|
|
self.plotter.save_page_image(image_page)
|
|
|
|
|
|
|
|
|
|
img_g3_page = img_g3[page_coord[0] : page_coord[1], page_coord[2] : page_coord[3], :]
|
|
|
|
|
|
|
|
|
|
text_regions_p_1 = text_regions_p_1[page_coord[0] : page_coord[1], page_coord[2] : page_coord[3]]
|
|
|
|
|
|
|
|
|
|
mask_images = (text_regions_p_1[:, :] == 2) * 1
|
|
|
|
|
mask_images = mask_images.astype(np.uint8)
|
|
|
|
|
mask_images = cv2.erode(mask_images[:, :], self.kernel, iterations=10)
|
|
|
|
|
|
|
|
|
|
mask_lines = (text_regions_p_1[:, :] == 3) * 1
|
|
|
|
|
mask_lines = mask_lines.astype(np.uint8)
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
img_only_regions = cv2.erode(img_only_regions_with_sep[:, :], self.kernel, iterations=6)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
num_col, peaks_neg_fin = find_num_col(img_only_regions, multiplier=6.0)
|
|
|
|
|
if not num_column_is_classified:
|
|
|
|
|
num_col_classifier = num_col + 1
|
|
|
|
|
except:
|
|
|
|
|
num_col = None
|
|
|
|
|
peaks_neg_fin = []
|
|
|
|
|
num_col_classifier = None
|
|
|
|
|
return num_col, num_col_classifier, img_only_regions, page_coord, image_page, mask_images, mask_lines
|
|
|
|
|
|
|
|
|
|
def run_enhancement(self):
|
|
|
|
|
self.logger.info("resize and enhance image")
|
|
|
|
|
is_image_enhanced, img_org, img_res, _, num_column_is_classified = self.resize_and_enhance_image_with_column_classifier()
|
|
|
|
@ -2163,6 +2204,7 @@ class eynollah:
|
|
|
|
|
self.get_image_and_scales_after_enhancing(img_org, img_res)
|
|
|
|
|
return img_res, is_image_enhanced, num_column_is_classified
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
|
"""
|
|
|
|
|
Get image and scales, then extract the page of scanned image
|
|
|
|
@ -2177,65 +2219,17 @@ class eynollah:
|
|
|
|
|
t1 = time.time()
|
|
|
|
|
text_regions_p_1 = self.get_regions_from_xy_2models(img_res, is_image_enhanced)
|
|
|
|
|
self.logger.info("Textregion detection took %ss ", str(time.time() - t1))
|
|
|
|
|
t1 = time.time()
|
|
|
|
|
|
|
|
|
|
img_g = cv2.imread(self.image_filename, cv2.IMREAD_GRAYSCALE)
|
|
|
|
|
img_g = img_g.astype(np.uint8)
|
|
|
|
|
|
|
|
|
|
img_g3 = np.zeros((img_g.shape[0], img_g.shape[1], 3))
|
|
|
|
|
img_g3 = img_g3.astype(np.uint8)
|
|
|
|
|
img_g3[:, :, 0] = img_g[:, :]
|
|
|
|
|
img_g3[:, :, 1] = img_g[:, :]
|
|
|
|
|
img_g3[:, :, 2] = img_g[:, :]
|
|
|
|
|
|
|
|
|
|
image_page, page_coord = self.extract_page()
|
|
|
|
|
# print(image_page.shape,'page')
|
|
|
|
|
if self.plotter:
|
|
|
|
|
self.plotter.save_page_image(image_page)
|
|
|
|
|
K.clear_session()
|
|
|
|
|
gc.collect()
|
|
|
|
|
|
|
|
|
|
img_g3_page = img_g3[page_coord[0] : page_coord[1], page_coord[2] : page_coord[3], :]
|
|
|
|
|
del img_g3
|
|
|
|
|
del img_g
|
|
|
|
|
|
|
|
|
|
text_regions_p_1 = text_regions_p_1[page_coord[0] : page_coord[1], page_coord[2] : page_coord[3]]
|
|
|
|
|
|
|
|
|
|
mask_images = (text_regions_p_1[:, :] == 2) * 1
|
|
|
|
|
mask_images = mask_images.astype(np.uint8)
|
|
|
|
|
mask_images = cv2.erode(mask_images[:, :], self.kernel, iterations=10)
|
|
|
|
|
|
|
|
|
|
mask_lines = (text_regions_p_1[:, :] == 3) * 1
|
|
|
|
|
mask_lines = mask_lines.astype(np.uint8)
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
img_only_regions = cv2.erode(img_only_regions_with_sep[:, :], self.kernel, iterations=6)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
num_col, peaks_neg_fin = find_num_col(img_only_regions, multiplier=6.0)
|
|
|
|
|
if not num_column_is_classified:
|
|
|
|
|
num_col_classifier = num_col + 1
|
|
|
|
|
except:
|
|
|
|
|
num_col = None
|
|
|
|
|
peaks_neg_fin = []
|
|
|
|
|
t1 = time.time()
|
|
|
|
|
num_col, num_col_classifier, img_only_regions, page_coord, image_page, mask_images, mask_lines = self.run_graphics_and_columns(text_regions_p_1, num_column_is_classified)
|
|
|
|
|
self.logger.info("Graphics detection took %ss ", str(time.time() - t1))
|
|
|
|
|
|
|
|
|
|
#print(num_col, "num_colnum_col")
|
|
|
|
|
if not num_col:
|
|
|
|
|
self.logger.info("No columns detected, outputting an empty PAGE-XML")
|
|
|
|
|
txt_con_org = []
|
|
|
|
|
order_text_new = []
|
|
|
|
|
id_of_texts_tot = []
|
|
|
|
|
all_found_texline_polygons = []
|
|
|
|
|
all_box_coord = []
|
|
|
|
|
polygons_of_images = []
|
|
|
|
|
polygons_of_marginals = []
|
|
|
|
|
all_found_texline_polygons_marginals = []
|
|
|
|
|
all_box_coord_marginals = []
|
|
|
|
|
slopes = []
|
|
|
|
|
slopes_marginals = []
|
|
|
|
|
self.write_into_page_xml(txt_con_org, page_coord, self.dir_out, order_text_new, id_of_texts_tot, all_found_texline_polygons, all_box_coord, polygons_of_images, polygons_of_marginals, all_found_texline_polygons_marginals, all_box_coord_marginals, self.curved_line, slopes, slopes_marginals)
|
|
|
|
|
else:
|
|
|
|
|
self.write_into_page_xml([], page_coord, self.dir_out, [], [], [], [], [], [], [], [], self.curved_line, [], [])
|
|
|
|
|
self.logger.info("Job done in %ss", str(time.time() - t1))
|
|
|
|
|
return
|
|
|
|
|
patches = True
|
|
|
|
|
scaler_h_textline = 1 # 1.2#1.2
|
|
|
|
|
scaler_w_textline = 1 # 0.9#1
|
|
|
|
|