|
|
|
@ -291,7 +291,7 @@ class Eynollah:
|
|
|
|
|
# "/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"
|
|
|
|
|
# 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"
|
|
|
|
@ -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:
|
|
|
|
@ -1123,7 +1123,7 @@ 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)
|
|
|
|
|
# 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
|
|
|
|
@ -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)
|
|
|
|
@ -1862,23 +1862,23 @@ class Eynollah:
|
|
|
|
|
# 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 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)
|
|
|
|
@ -1892,7 +1892,7 @@ class Eynollah:
|
|
|
|
|
img_bin = np.copy(img_resized)
|
|
|
|
|
# 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)
|
|
|
|
@ -1907,7 +1907,7 @@ class Eynollah:
|
|
|
|
|
# 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,7 +1934,7 @@ 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)
|
|
|
|
|
# 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()
|
|
|
|
@ -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
|
|
|
|
@ -1978,7 +1978,7 @@ class Eynollah:
|
|
|
|
|
# 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)
|
|
|
|
@ -2024,7 +2024,7 @@ class Eynollah:
|
|
|
|
|
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)
|
|
|
|
@ -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)):
|
|
|
|
@ -2880,10 +2880,10 @@ class Eynollah:
|
|
|
|
|
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
|
|
|
|
@ -3311,7 +3311,7 @@ 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=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
|
|
|
|
@ -3348,11 +3348,11 @@ class Eynollah:
|
|
|
|
|
# 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
|
|
|
|
|
# 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
|
|
|
|
|
# text_regions_p[:, :][regions_fully_np[:, :, 0] == 4] = 4
|
|
|
|
|
# plt.imshow(text_regions_p)
|
|
|
|
|
# plt.show()
|
|
|
|
|
####if not self.tables:
|
|
|
|
|
# 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)
|
|
|
|
@ -3572,7 +3572,7 @@ class Eynollah:
|
|
|
|
|
# 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.plot([peaks_final[1], peaks_final[1]], [0, height-1])
|
|
|
|
|
# plt.savefig('./'+str(ind_tot)+'.png')
|
|
|
|
|
|
|
|
|
|
return peaks_final
|
|
|
|
@ -3666,14 +3666,14 @@ class Eynollah:
|
|
|
|
|
|
|
|
|
|
# print(generated_text1,'generated_text1')
|
|
|
|
|
# print(generated_text2, 'generated_text2')
|
|
|
|
|
#print('########################################')
|
|
|
|
|
# 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('##########')
|
|
|
|
|
return generated_text
|
|
|
|
|
|
|
|
|
|
def return_ocr_of_textline(
|
|
|
|
@ -3706,7 +3706,7 @@ class Eynollah:
|
|
|
|
|
generated_text2 = processor.batch_decode(generated_ids2, skip_special_tokens=True)[0]
|
|
|
|
|
# print(generated_text1,'generated_text1')
|
|
|
|
|
# print(generated_text2, 'generated_text2')
|
|
|
|
|
#print('########################################')
|
|
|
|
|
# print('##########')
|
|
|
|
|
|
|
|
|
|
match = sq(None, generated_text1, generated_text2).find_longest_match(
|
|
|
|
|
0, len(generated_text1), 0, len(generated_text2))
|
|
|
|
@ -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):
|
|
|
|
@ -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(
|
|
|
|
@ -4533,21 +4533,21 @@ class Eynollah:
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
# 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
|
|
|
|
@ -4557,7 +4557,7 @@ class Eynollah:
|
|
|
|
|
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
|
|
|
|
|
# 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:
|
|
|
|
@ -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)
|
|
|
|
|
|
|
|
|
@ -4703,7 +4703,7 @@ class Eynollah:
|
|
|
|
|
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
|
|
|
|
|
# birdan sora chock chakir
|
|
|
|
|
if not self.curved_line:
|
|
|
|
|
if self.light_version:
|
|
|
|
|
if self.textline_light:
|
|
|
|
@ -4723,7 +4723,7 @@ class Eynollah:
|
|
|
|
|
# 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_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(
|
|
|
|
@ -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()]
|
|
|
|
|