@ -38,6 +38,7 @@ sys.stderr = open(os.devnull, "w")
import tensorflow as tf
from tensorflow . python . keras import backend as K
from tensorflow . keras . models import load_model
sys . stderr = stderr
tf . get_logger ( ) . setLevel ( " ERROR " )
warnings . filterwarnings ( " ignore " )
@ -131,8 +132,8 @@ class Patches(layers.Layer):
patch_dims = patches . shape [ - 1 ]
patches = tf . reshape ( patches , [ batch_size , - 1 , patch_dims ] )
return patches
def get_config ( self ) :
def get_config ( self ) :
config = super ( ) . get_config ( ) . copy ( )
config . update ( {
' patch_size ' : self . patch_size ,
@ -153,8 +154,8 @@ class PatchEncoder(layers.Layer):
positions = tf . range ( start = 0 , limit = self . num_patches , delta = 1 )
encoded = self . projection ( patch ) + self . position_embedding ( positions )
return encoded
def get_config ( self ) :
def get_config ( self ) :
config = super ( ) . get_config ( ) . copy ( )
config . update ( {
' num_patches ' : self . num_patches ,
@ -1037,7 +1038,6 @@ class Eynollah:
np . newaxis ]
indexer_inside_batch + = 1
list_i_s = [ ]
list_j_s = [ ]
list_x_u = [ ]
@ -1493,7 +1493,8 @@ class Eynollah:
else :
img = resize_image ( img , int ( img_height_h * 2500 / float ( img_width_h ) ) , 2500 ) . astype ( np . uint8 )
prediction_regions = self . do_prediction ( patches , img , model_region , marginal_of_patch_percent = 0.1 , n_batch_inference = 3 )
prediction_regions = self . do_prediction ( patches , img , model_region , marginal_of_patch_percent = 0.1 ,
n_batch_inference = 3 )
prediction_regions = resize_image ( prediction_regions , img_height_h , img_width_h )
self . logger . debug ( " exit extract_text_regions " )
return prediction_regions , prediction_regions
@ -1556,7 +1557,8 @@ class Eynollah:
self . logger . debug ( " exit extract_text_regions " )
return prediction_regions , prediction_regions2
def get_slopes_and_deskew_new_light2 ( self , contours , contours_par , textline_mask_tot , image_page_rotated , boxes , slope_deskew ) :
def get_slopes_and_deskew_new_light2 ( self , contours , contours_par , textline_mask_tot , image_page_rotated , boxes ,
slope_deskew ) :
polygons_of_textlines = return_contours_of_interested_region ( textline_mask_tot , 1 , 0.00001 )
M_main_tot = [ cv2 . moments ( polygons_of_textlines [ j ] )
@ -1582,9 +1584,11 @@ class Eynollah:
_ , crop_coor = crop_image_inside_box ( boxes [ index ] , image_page_rotated )
all_box_coord . append ( crop_coor )
return all_found_textline_polygons , boxes , contours , contours_par , all_box_coord , np . array ( range ( len ( contours_par ) ) ) , slopes
return all_found_textline_polygons , boxes , contours , contours_par , all_box_coord , np . array (
range ( len ( contours_par ) ) ) , slopes
def get_slopes_and_deskew_new_light ( self , contours , contours_par , textline_mask_tot , image_page_rotated , boxes , slope_deskew ) :
def get_slopes_and_deskew_new_light ( self , contours , contours_par , textline_mask_tot , image_page_rotated , boxes ,
slope_deskew ) :
if not len ( contours ) :
return [ ] , [ ] , [ ] , [ ] , [ ] , [ ] , [ ]
self . logger . debug ( " enter get_slopes_and_deskew_new_light " )
@ -1598,7 +1602,8 @@ class Eynollah:
self . logger . debug ( " exit get_slopes_and_deskew_new_light " )
return tuple ( zip ( * results ) )
def get_slopes_and_deskew_new ( self , contours , contours_par , textline_mask_tot , image_page_rotated , boxes , slope_deskew ) :
def get_slopes_and_deskew_new ( self , contours , contours_par , textline_mask_tot , image_page_rotated , boxes ,
slope_deskew ) :
if not len ( contours ) :
return [ ] , [ ] , [ ] , [ ] , [ ] , [ ] , [ ]
self . logger . debug ( " enter get_slopes_and_deskew_new " )
@ -1615,7 +1620,8 @@ class Eynollah:
self . logger . debug ( " exit get_slopes_and_deskew_new " )
return tuple ( zip ( * results ) )
def get_slopes_and_deskew_new_curved ( self , contours , contours_par , textline_mask_tot , image_page_rotated , boxes , mask_texts_only , num_col , scale_par , slope_deskew ) :
def get_slopes_and_deskew_new_curved ( self , contours , contours_par , textline_mask_tot , image_page_rotated , boxes ,
mask_texts_only , num_col , scale_par , slope_deskew ) :
if not len ( contours ) :
return [ ] , [ ] , [ ] , [ ] , [ ] , [ ] , [ ]
self . logger . debug ( " enter get_slopes_and_deskew_new_curved " )
@ -1680,7 +1686,6 @@ class Eynollah:
return ( ( prediction_textline [ : , : , 0 ] == 1 ) . astype ( np . uint8 ) ,
( prediction_textline_longshot_true_size [ : , : , 0 ] == 1 ) . astype ( np . uint8 ) )
def do_work_of_slopes ( self , q , poly , box_sub , boxes_per_process , textline_mask_tot , contours_per_process ) :
self . logger . debug ( ' enter do_work_of_slopes ' )
slope_biggest = 0
@ -1688,17 +1693,20 @@ class Eynollah:
boxes_sub_new = [ ]
poly_sub = [ ]
for mv in range ( len ( boxes_per_process ) ) :
crop_img , _ = crop_image_inside_box ( boxes_per_process [ mv ] , np . repeat ( textline_mask_tot [ : , : , np . newaxis ] , 3 , axis = 2 ) )
crop_img , _ = crop_image_inside_box ( boxes_per_process [ mv ] ,
np . repeat ( textline_mask_tot [ : , : , np . newaxis ] , 3 , axis = 2 ) )
crop_img = crop_img [ : , : , 0 ]
crop_img = cv2 . erode ( crop_img , KERNEL , iterations = 2 )
try :
textline_con , hierarchy = return_contours_of_image ( crop_img )
textline_con_fil = filter_contours_area_of_image ( crop_img , textline_con , hierarchy , max_area = 1 , min_area = 0.0008 )
textline_con_fil = filter_contours_area_of_image ( crop_img , textline_con , hierarchy , max_area = 1 ,
min_area = 0.0008 )
y_diff_mean = find_contours_mean_y_diff ( textline_con_fil )
sigma_des = max ( 1 , int ( y_diff_mean * ( 4.0 / 40.0 ) ) )
crop_img [ crop_img > 0 ] = 1
slope_corresponding_textregion = return_deskew_slop ( crop_img , sigma_des ,
map = self . executor . map , logger = self . logger , plotter = self . plotter )
map = self . executor . map , logger = self . logger ,
plotter = self . plotter )
except Exception as why :
self . logger . error ( why )
slope_corresponding_textregion = MAX_SLOPE
@ -1741,7 +1749,8 @@ class Eynollah:
img_resized = resize_image ( img , img_h_new , img_w_new )
if not self . dir_in :
self . model_region , _ = self . start_new_session_and_model ( self . model_region_dir_p_ens_light_only_images_extraction )
self . model_region , _ = self . start_new_session_and_model (
self . model_region_dir_p_ens_light_only_images_extraction )
prediction_regions_org = self . do_prediction_new_concept ( True , img_resized , self . model_region )
@ -1884,7 +1893,8 @@ class Eynollah:
#print("inside 1 ", time.time()-t_in)
###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 ) ) )
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 )
@ -2019,7 +2029,8 @@ class Eynollah:
try :
img_only_regions = cv2 . erode ( img_only_regions_with_sep [ : , : ] , KERNEL , iterations = 20 )
_ , _ = find_num_col ( img_only_regions , num_col_classifier , self . tables , multiplier = 6.0 )
img = resize_image ( img_org , int ( img_org . shape [ 0 ] ) , int ( img_org . shape [ 1 ] * ( 1.2 if is_image_enhanced else 1 ) ) )
img = resize_image ( img_org , int ( img_org . shape [ 0 ] ) ,
int ( img_org . shape [ 1 ] * ( 1.2 if is_image_enhanced else 1 ) ) )
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 )
@ -2113,7 +2124,6 @@ class Eynollah:
ratio_y = 1
ratio_x = 1
img = resize_image ( prediction_bin , int ( img_org . shape [ 0 ] * ratio_y ) , int ( img_org . shape [ 1 ] * ratio_x ) )
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 )
@ -2130,7 +2140,6 @@ class Eynollah:
#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
mask_images_only = ( prediction_regions_org == 2 ) * 1
@ -2537,7 +2546,8 @@ class Eynollah:
contours_new . append ( contours_sep [ ji ] )
if num_col_classifier > = 2 :
only_recent_contour_image = np . zeros ( ( layout . shape [ 0 ] , layout . shape [ 1 ] ) )
only_recent_contour_image = cv2 . fillPoly ( only_recent_contour_image , pts = [ contours_sep [ ji ] ] , color = ( 1 , 1 , 1 ) )
only_recent_contour_image = cv2 . fillPoly ( only_recent_contour_image , pts = [ contours_sep [ ji ] ] ,
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')
@ -2552,7 +2562,8 @@ class Eynollah:
contours_new . append ( contours [ i ] )
if num_col_classifier > = 2 :
only_recent_contour_image = np . zeros ( ( layout . shape [ 0 ] , layout . shape [ 1 ] ) )
only_recent_contour_image = cv2 . fillPoly ( only_recent_contour_image , pts = [ contours [ i ] ] , color = ( 1 , 1 , 1 ) )
only_recent_contour_image = cv2 . fillPoly ( only_recent_contour_image , pts = [ contours [ i ] ] ,
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 ( )
@ -2622,9 +2633,11 @@ class Eynollah:
contours , hirarchy = cv2 . findContours ( thresh . copy ( ) , cv2 . RETR_TREE , cv2 . CHAIN_APPROX_SIMPLE )
if indiv == pixel_table :
main_contours = filter_contours_area_of_image_tables ( thresh , contours , hirarchy , max_area = 1 , min_area = 0.001 )
main_contours = filter_contours_area_of_image_tables ( thresh , contours , hirarchy , max_area = 1 ,
min_area = 0.001 )
else :
main_contours = filter_contours_area_of_image_tables ( thresh , contours , hirarchy , max_area = 1 , min_area = min_area )
main_contours = filter_contours_area_of_image_tables ( thresh , contours , hirarchy , max_area = 1 ,
min_area = min_area )
img_comm = cv2 . fillPoly ( img_comm , pts = main_contours , color = ( indiv , indiv , indiv ) )
img_comm = img_comm . astype ( np . uint8 )
@ -2659,8 +2672,10 @@ class Eynollah:
y_min_main_line , y_max_main_line = find_features_of_contours ( contours_line )
y_min_main_tab , y_max_main_tab = find_features_of_contours ( contours_tab )
cx_tab_m_text , cy_tab_m_text , x_min_tab_m_text , x_max_tab_m_text , y_min_tab_m_text , y_max_tab_m_text , _ = find_new_features_of_contours ( contours_table_m_text )
cx_tabl , cy_tabl , x_min_tabl , x_max_tabl , y_min_tabl , y_max_tabl , _ = find_new_features_of_contours ( contours_tab )
cx_tab_m_text , cy_tab_m_text , x_min_tab_m_text , x_max_tab_m_text , y_min_tab_m_text , y_max_tab_m_text , _ = find_new_features_of_contours (
contours_table_m_text )
cx_tabl , cy_tabl , x_min_tabl , x_max_tabl , y_min_tabl , y_max_tabl , _ = find_new_features_of_contours (
contours_tab )
if len ( y_min_main_tab ) > 0 :
y_down_tabs = [ ]
@ -2670,9 +2685,13 @@ class Eynollah:
y_down_tab = [ ]
y_up_tab = [ ]
for i_l in range ( len ( y_min_main_line ) ) :
if y_min_main_tab [ i_t ] > y_min_main_line [ i_l ] and y_max_main_tab [ i_t ] > y_min_main_line [ i_l ] and y_min_main_tab [ i_t ] > y_max_main_line [ i_l ] and y_max_main_tab [ i_t ] > y_min_main_line [ i_l ] :
if y_min_main_tab [ i_t ] > y_min_main_line [ i_l ] and y_max_main_tab [ i_t ] > y_min_main_line [
i_l ] and y_min_main_tab [ i_t ] > y_max_main_line [ i_l ] and y_max_main_tab [ i_t ] > \
y_min_main_line [ i_l ] :
pass
elif y_min_main_tab [ i_t ] < y_max_main_line [ i_l ] and y_max_main_tab [ i_t ] < y_max_main_line [ i_l ] and y_max_main_tab [ i_t ] < y_min_main_line [ i_l ] and y_min_main_tab [ i_t ] < y_min_main_line [ i_l ] :
elif y_min_main_tab [ i_t ] < y_max_main_line [ i_l ] and y_max_main_tab [ i_t ] < \
y_max_main_line [ i_l ] and y_max_main_tab [ i_t ] < y_min_main_line [ i_l ] and \
y_min_main_tab [ i_t ] < y_min_main_line [ i_l ] :
pass
elif np . abs ( y_max_main_line [ i_l ] - y_min_main_line [ i_l ] ) < 100 :
pass
@ -2801,7 +2820,8 @@ class Eynollah:
prediction_table_full_erode = cv2 . dilate ( prediction_table_full_erode , KERNEL , iterations = 4 )
prediction_table_full_updown_erode = cv2 . erode ( pre_updown [ : , : , 0 ] , KERNEL , iterations = 4 )
prediction_table_full_updown_erode = cv2 . dilate ( prediction_table_full_updown_erode , KERNEL , iterations = 4 )
prediction_table_full_updown_erode = cv2 . dilate ( prediction_table_full_updown_erode , KERNEL ,
iterations = 4 )
prediction_table [ : , 0 : img_w_half , : ] = pre1 [ : , : , : ]
prediction_table [ : , img_w_half : , : ] = pre2 [ : , : , : ]
@ -2964,7 +2984,8 @@ class Eynollah:
else :
self . get_image_and_scales ( img_org , img_res , scale )
if self . allow_scaling :
img_org , img_res , is_image_enhanced = self . resize_image_with_column_classifier ( is_image_enhanced , img_bin )
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)
return img_res , is_image_enhanced , num_col_classifier , num_column_is_classified
@ -2973,7 +2994,8 @@ class Eynollah:
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 )
textline_mask_tot_ea , _ = self . textline_contours ( image_page , True , scaler_h_textline , scaler_w_textline ,
num_col_classifier )
if self . textline_light :
textline_mask_tot_ea = textline_mask_tot_ea . astype ( np . int16 )
@ -3035,7 +3057,8 @@ class Eynollah:
regions_without_separators_d = ( text_regions_p_1_n [ : , : ] == 1 ) * 1
if self . tables :
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)
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')
if self . tables :
regions_without_separators [ table_prediction == 1 ] = 1
@ -3100,11 +3123,13 @@ class Eynollah:
else :
text_regions_p_tables = np . copy ( text_regions_p_1_n )
text_regions_p_tables = np . round ( text_regions_p_tables )
text_regions_p_tables [ : , : ] [ ( text_regions_p_tables [ : , : ] != 3 ) & ( table_prediction_n [ : , : ] == 1 ) ] = 10
text_regions_p_tables [ : , : ] [
( text_regions_p_tables [ : , : ] != 3 ) & ( table_prediction_n [ : , : ] == 1 ) ] = 10
pixel_line = 3
img_revised_tab2 = self . add_tables_heuristic_to_layout (
text_regions_p_tables , boxes_d , 0 , splitter_y_new_d , peaks_neg_tot_tables_d , text_regions_p_tables ,
text_regions_p_tables , boxes_d , 0 , splitter_y_new_d , peaks_neg_tot_tables_d ,
text_regions_p_tables ,
num_col_classifier , 0.000005 , pixel_line )
img_revised_tab2_d , _ = self . check_iou_of_bounding_box_and_contour_for_tables (
img_revised_tab2 , table_prediction_n , 10 , num_col_classifier )
@ -3112,7 +3137,8 @@ class Eynollah:
img_revised_tab2_d_rotated = rotate_image ( img_revised_tab2_d , - slope_deskew )
img_revised_tab2_d_rotated = np . round ( img_revised_tab2_d_rotated )
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 ] )
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')
self . logger . info ( " detecting boxes took %.1f s " , time . time ( ) - t1 )
@ -3171,11 +3197,15 @@ class Eynollah:
img_revised_tab = text_regions_p [ : , : ]
if np . abs ( slope_deskew ) > = SLOPE_THRESHOLD :
image_page_rotated_n , textline_mask_tot_d , text_regions_p_1_n , table_prediction_n = \
rotation_not_90_func ( image_page , textline_mask_tot , text_regions_p , table_prediction , slope_deskew )
rotation_not_90_func ( image_page , textline_mask_tot , text_regions_p , table_prediction ,
slope_deskew )
text_regions_p_1_n = resize_image ( text_regions_p_1_n , text_regions_p . shape [ 0 ] , text_regions_p . shape [ 1 ] )
textline_mask_tot_d = resize_image ( textline_mask_tot_d , text_regions_p . shape [ 0 ] , text_regions_p . shape [ 1 ] )
table_prediction_n = resize_image ( table_prediction_n , text_regions_p . shape [ 0 ] , text_regions_p . shape [ 1 ] )
text_regions_p_1_n = resize_image ( text_regions_p_1_n , text_regions_p . shape [ 0 ] ,
text_regions_p . shape [ 1 ] )
textline_mask_tot_d = resize_image ( textline_mask_tot_d , text_regions_p . shape [ 0 ] ,
text_regions_p . shape [ 1 ] )
table_prediction_n = resize_image ( table_prediction_n , text_regions_p . shape [ 0 ] ,
text_regions_p . shape [ 1 ] )
regions_without_separators_d = ( text_regions_p_1_n [ : , : ] == 1 ) * 1
regions_without_separators_d [ table_prediction_n [ : , : ] == 1 ] = 1
@ -3191,11 +3221,15 @@ class Eynollah:
else :
if np . abs ( slope_deskew ) > = SLOPE_THRESHOLD :
image_page_rotated_n , textline_mask_tot_d , text_regions_p_1_n , table_prediction_n = \
rotation_not_90_func ( image_page , textline_mask_tot , text_regions_p , table_prediction , slope_deskew )
rotation_not_90_func ( image_page , textline_mask_tot , text_regions_p , table_prediction ,
slope_deskew )
text_regions_p_1_n = resize_image ( text_regions_p_1_n , text_regions_p . shape [ 0 ] , text_regions_p . shape [ 1 ] )
textline_mask_tot_d = resize_image ( textline_mask_tot_d , text_regions_p . shape [ 0 ] , text_regions_p . shape [ 1 ] )
table_prediction_n = resize_image ( table_prediction_n , text_regions_p . shape [ 0 ] , text_regions_p . shape [ 1 ] )
text_regions_p_1_n = resize_image ( text_regions_p_1_n , text_regions_p . shape [ 0 ] ,
text_regions_p . shape [ 1 ] )
textline_mask_tot_d = resize_image ( textline_mask_tot_d , text_regions_p . shape [ 0 ] ,
text_regions_p . shape [ 1 ] )
table_prediction_n = resize_image ( table_prediction_n , text_regions_p . shape [ 0 ] ,
text_regions_p . shape [ 1 ] )
regions_without_separators_d = ( text_regions_p_1_n [ : , : ] == 1 ) * 1
regions_without_separators_d [ table_prediction_n [ : , : ] == 1 ] = 1
@ -3227,7 +3261,8 @@ class Eynollah:
if np . abs ( slope_deskew ) > = SLOPE_THRESHOLD :
regions_without_separators_d = regions_without_separators_d . astype ( np . uint8 )
regions_without_separators_d = cv2 . erode ( regions_without_separators_d [ : , : ] , KERNEL , iterations = 6 )
regions_without_separators_d = cv2 . erode ( regions_without_separators_d [ : , : ] , KERNEL ,
iterations = 6 )
else :
pass
@ -3250,11 +3285,13 @@ class Eynollah:
num_col_classifier , erosion_hurts , self . tables , self . right2left )
text_regions_p_tables = np . copy ( text_regions_p_1_n )
text_regions_p_tables = np . round ( text_regions_p_tables )
text_regions_p_tables [ : , : ] [ ( text_regions_p_tables [ : , : ] != 3 ) & ( table_prediction_n [ : , : ] == 1 ) ] = 10
text_regions_p_tables [ : , : ] [
( text_regions_p_tables [ : , : ] != 3 ) & ( table_prediction_n [ : , : ] == 1 ) ] = 10
pixel_line = 3
img_revised_tab2 = self . add_tables_heuristic_to_layout (
text_regions_p_tables , boxes_d , 0 , splitter_y_new_d , peaks_neg_tot_tables_d , text_regions_p_tables ,
text_regions_p_tables , boxes_d , 0 , splitter_y_new_d , peaks_neg_tot_tables_d ,
text_regions_p_tables ,
num_col_classifier , 0.000005 , pixel_line )
img_revised_tab2_d , _ = self . check_iou_of_bounding_box_and_contour_for_tables (
@ -3264,7 +3301,8 @@ class Eynollah:
img_revised_tab2_d_rotated = np . round ( img_revised_tab2_d_rotated )
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 ] )
img_revised_tab2_d_rotated = resize_image ( img_revised_tab2_d_rotated , text_regions_p . shape [ 0 ] ,
text_regions_p . shape [ 1 ] )
if np . abs ( slope_deskew ) < 0.13 :
img_revised_tab = np . copy ( img_revised_tab2 [ : , : , 0 ] )
@ -3758,9 +3796,11 @@ class Eynollah:
if abs_diff [ i ] == 0 :
inc_x [ i + 1 ] = dilation_m2 * ( - 1 * y_differential_mask_nonzeros [ i ] )
inc_y [ i + 1 ] = dilation_m2 * ( x_differential_mask_nonzeros [ i ] )
elif abs_diff [ i ] != 0 and x_differential_mask_nonzeros [ i ] == 0 and y_differential_mask_nonzeros [ i ] != 0 :
elif abs_diff [ i ] != 0 and x_differential_mask_nonzeros [ i ] == 0 and y_differential_mask_nonzeros [
i ] != 0 :
inc_x [ i + 1 ] = dilation_m1 * ( - 1 * y_differential_mask_nonzeros [ i ] )
elif abs_diff [ i ] != 0 and x_differential_mask_nonzeros [ i ] != 0 and y_differential_mask_nonzeros [ i ] == 0 :
elif abs_diff [ i ] != 0 and x_differential_mask_nonzeros [ i ] != 0 and y_differential_mask_nonzeros [
i ] == 0 :
inc_y [ i + 1 ] = dilation_m1 * ( x_differential_mask_nonzeros [ i ] )
elif abs_diff [ i ] != 0 and abs_diff [ i ] > = 3 :
@ -3964,9 +4004,11 @@ class Eynollah:
if abs_diff [ i ] == 0 :
inc_x [ i + 1 ] = dilation_m2 * ( - 1 * y_differential_mask_nonzeros [ i ] )
inc_y [ i + 1 ] = dilation_m2 * ( x_differential_mask_nonzeros [ i ] )
elif abs_diff [ i ] != 0 and x_differential_mask_nonzeros [ i ] == 0 and y_differential_mask_nonzeros [ i ] != 0 :
elif abs_diff [ i ] != 0 and x_differential_mask_nonzeros [ i ] == 0 and y_differential_mask_nonzeros [
i ] != 0 :
inc_x [ i + 1 ] = dilation_m1 * ( - 1 * y_differential_mask_nonzeros [ i ] )
elif abs_diff [ i ] != 0 and x_differential_mask_nonzeros [ i ] != 0 and y_differential_mask_nonzeros [ i ] == 0 :
elif abs_diff [ i ] != 0 and x_differential_mask_nonzeros [ i ] != 0 and y_differential_mask_nonzeros [
i ] == 0 :
inc_y [ i + 1 ] = dilation_m1 * ( x_differential_mask_nonzeros [ i ] )
elif abs_diff [ i ] != 0 and abs_diff [ i ] > = 3 :
@ -4039,7 +4081,8 @@ class Eynollah:
results = [ cv2 . pointPolygonTest ( contours [ ind ] , ( cx_main [ ind_small ] , cy_main [ ind_small ] ) , False )
for ind in contours_index_big ]
if marginal_cnts :
results_marginal = [ cv2 . pointPolygonTest ( marginal_cnts [ ind ] , ( cx_main [ ind_small ] , cy_main [ ind_small ] ) , False )
results_marginal = [
cv2 . pointPolygonTest ( marginal_cnts [ ind ] , ( cx_main [ ind_small ] , cy_main [ ind_small ] ) , False )
for ind in range ( len ( marginal_cnts ) ) ]
results_marginal = np . array ( results_marginal )
@ -4094,7 +4137,8 @@ class Eynollah:
args_with_bigger_area = np . array ( args_all ) [ areas_without > 1.5 * area_of_con_interest ]
if len ( args_with_bigger_area ) > 0 :
results = [ cv2 . pointPolygonTest ( contours_txtline_of_all_textregions [ ind ] , ( cx_main_tot [ ij ] , cy_main_tot [ ij ] ) , False )
results = [ cv2 . pointPolygonTest ( contours_txtline_of_all_textregions [ ind ] ,
( cx_main_tot [ ij ] , cy_main_tot [ ij ] ) , False )
for ind in args_with_bigger_area ]
results = np . array ( results )
if np . any ( results == 1 ) :
@ -4106,7 +4150,8 @@ class Eynollah:
textregion_index_to_del = np . array ( textregion_index_to_del )
textline_in_textregion_index_to_del = np . array ( textline_in_textregion_index_to_del )
for ind_u_a_trs in np . unique ( textregion_index_to_del ) :
textline_in_textregion_index_to_del_ind = textline_in_textregion_index_to_del [ textregion_index_to_del == ind_u_a_trs ]
textline_in_textregion_index_to_del_ind = textline_in_textregion_index_to_del [
textregion_index_to_del == ind_u_a_trs ]
textline_in_textregion_index_to_del_ind = np . sort ( textline_in_textregion_index_to_del_ind ) [ : : - 1 ]
for ittrd in textline_in_textregion_index_to_del_ind :
contours [ ind_u_a_trs ] . pop ( ittrd )
@ -4154,7 +4199,8 @@ class Eynollah:
if len ( contours_only_text_parent_d_ordered ) > 0 :
contours_only_text_parent_d_ordered . pop ( ind_u_a_trs )
return contours , text_con_org , contours_textline , contours_only_text_parent_d_ordered , np . array ( range ( len ( contours ) ) )
return contours , text_con_org , contours_textline , contours_only_text_parent_d_ordered , np . array (
range ( len ( contours ) ) )
def dilate_textlines ( self , all_found_textline_polygons ) :
for j in range ( len ( all_found_textline_polygons ) ) :
@ -4325,7 +4371,8 @@ class Eynollah:
self . logger . warning ( " will skip input for existing output file ' %s ' " , self . writer . output_filename )
continue
img_res , is_image_enhanced , num_col_classifier , num_column_is_classified = self . run_enhancement ( self . light_version )
img_res , is_image_enhanced , num_col_classifier , num_column_is_classified = self . run_enhancement (
self . light_version )
self . logger . info ( " Enhancing took %.1f s " , time . time ( ) - t0 )
if self . extract_only_images :
text_regions_p_1 , erosion_hurts , polygons_lines_xml , polygons_of_images , image_page , page_coord , cont_page = \
@ -4352,7 +4399,6 @@ 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)
cnt_clean_rot_raw , hir_on_cnt_clean_rot = return_contours_of_image ( textline_mask_tot_ea )
@ -4366,7 +4412,6 @@ class Eynollah:
all_found_textline_polygons = self . filter_contours_inside_a_bigger_one (
all_found_textline_polygons , textline_mask_tot_ea , type_contour = " textline " )
order_text_new = [ 0 ]
slopes = [ 0 ]
id_of_texts_tot = [ ' region_0001 ' ]
@ -4414,7 +4459,8 @@ class Eynollah:
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 )
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)
textline_mask_tot_ea_org = np . copy ( textline_mask_tot_ea )
@ -4428,7 +4474,8 @@ class Eynollah:
t1 = time . time ( )
num_col , num_col_classifier , img_only_regions , page_coord , image_page , mask_images , mask_lines , \
text_regions_p_1 , cont_page , table_prediction = \
self . run_graphics_and_columns ( text_regions_p_1 , num_col_classifier , num_column_is_classified , erosion_hurts )
self . run_graphics_and_columns ( text_regions_p_1 , num_col_classifier , num_column_is_classified ,
erosion_hurts )
self . logger . info ( " Graphics detection took %.1f s " , time . time ( ) - t1 )
#self.logger.info('cont_page %s', cont_page)
#plt.imshow(table_prediction)
@ -4505,7 +4552,6 @@ class Eynollah:
drop_label_in_full_layout = 4
textline_mask_tot_ea_org [ img_revised_tab == drop_label_in_full_layout ] = 0
text_only = ( img_revised_tab [ : , : ] == 1 ) * 1
if np . abs ( slope_deskew ) > = SLOPE_THRESHOLD :
text_only_d = ( text_regions_p_1_n [ : , : ] == 1 ) * 1
@ -4562,8 +4608,10 @@ class Eynollah:
areas_cnt_text_d = self . return_list_of_contours_with_desired_order (
areas_cnt_text_d , index_con_parents_d )
cx_bigest_d_big , cy_biggest_d_big , _ , _ , _ , _ , _ = find_new_features_of_contours ( [ contours_biggest_d ] )
cx_bigest_d , cy_biggest_d , _ , _ , _ , _ , _ = find_new_features_of_contours ( contours_only_text_parent_d )
cx_bigest_d_big , cy_biggest_d_big , _ , _ , _ , _ , _ = find_new_features_of_contours (
[ contours_biggest_d ] )
cx_bigest_d , cy_biggest_d , _ , _ , _ , _ , _ = find_new_features_of_contours (
contours_only_text_parent_d )
try :
if len ( cx_bigest_d ) > = 5 :
cx_bigest_d_last5 = cx_bigest_d [ - 5 : ]
@ -4684,7 +4732,8 @@ class Eynollah:
all_found_textline_polygons_marginals )
contours_only_text_parent , txt_con_org , all_found_textline_polygons , contours_only_text_parent_d_ordered , \
index_by_text_par_con = self . filter_contours_without_textline_inside (
contours_only_text_parent , txt_con_org , all_found_textline_polygons , contours_only_text_parent_d_ordered )
contours_only_text_parent , txt_con_org , all_found_textline_polygons ,
contours_only_text_parent_d_ordered )
else :
textline_mask_tot_ea = cv2 . erode ( textline_mask_tot_ea , kernel = KERNEL , iterations = 1 )
all_found_textline_polygons , boxes_text , txt_con_org , contours_only_text_parent , all_box_coord , \
@ -4784,10 +4833,12 @@ class Eynollah:
if num_col_classifier > = 3 :
if np . abs ( slope_deskew ) < SLOPE_THRESHOLD :
regions_without_separators = regions_without_separators . astype ( np . uint8 )
regions_without_separators = cv2 . erode ( regions_without_separators [ : , : ] , KERNEL , iterations = 6 )
regions_without_separators = cv2 . erode ( regions_without_separators [ : , : ] , KERNEL ,
iterations = 6 )
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 )
regions_without_separators_d = cv2 . erode ( regions_without_separators_d [ : , : ] , KERNEL ,
iterations = 6 )
if np . abs ( slope_deskew ) < SLOPE_THRESHOLD :
boxes , peaks_neg_tot_tables = return_boxes_of_images_by_order_of_reading_new (
@ -4812,7 +4863,8 @@ class Eynollah:
contours_only_text_parent , contours_only_text_parent_h , boxes , textline_mask_tot )
else :
order_text_new , id_of_texts_tot = self . do_order_of_regions (
contours_only_text_parent_d_ordered , contours_only_text_parent_h_d_ordered , boxes_d , textline_mask_tot_d )
contours_only_text_parent_d_ordered , contours_only_text_parent_h_d_ordered , boxes_d ,
textline_mask_tot_d )
self . logger . info ( " detection of reading order took %.1f s " , time . time ( ) - t_order )
if self . ocr :
@ -4852,7 +4904,8 @@ class Eynollah:
#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 )
contours_only_text_parent_d_ordered , contours_only_text_parent_h , boxes_d ,
textline_mask_tot_d )
if self . ocr :
device = cuda . get_current_device ( )
@ -4897,7 +4950,9 @@ class Eynollah:
img_croped = img_poly_on_img [ y : y + h , x : x + w , : ]
#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 )
text_ocr = self . return_ocr_of_textline_without_common_section ( img_croped , model_ocr ,
processor , device , w ,
h2w_ratio , ind_tot )
ocr_textline_in_textregion . append ( text_ocr )
ind_tot = ind_tot + 1
ocr_all_textlines . append ( ocr_textline_in_textregion )
@ -4961,11 +5016,9 @@ class Eynollah_ocr:
model_ocr . get_layer ( name = " image " ) . input ,
model_ocr . get_layer ( name = " dense2 " ) . output )
with open ( os . path . join ( self . model_ocr_dir , " characters_org.txt " ) , " r " ) as config_file :
characters = json . load ( config_file )
AUTOTUNE = tf . data . AUTOTUNE
# Mapping characters to integers.
@ -5002,7 +5055,6 @@ class Eynollah_ocr:
output . append ( d )
return output
def distortion_free_resize ( self , image , img_size ) :
w , h = img_size
image = tf . image . resize ( image , size = ( h , w ) , preserve_aspect_ratio = True )
@ -5073,6 +5125,7 @@ class Eynollah_ocr:
return [ image1 , image2 ]
else :
return None
def preprocess_and_resize_image_for_ocrcnn_model ( self , img , image_height , image_width ) :
ratio = image_height / float ( img . shape [ 0 ] )
w_ratio = int ( ratio * img . shape [ 1 ] )
@ -5110,8 +5163,6 @@ class Eynollah_ocr:
region_tags = np . unique ( [ x for x in alltags if x . endswith ( ' TextRegion ' ) ] )
cropped_lines = [ ]
cropped_lines_region_indexer = [ ]
cropped_lines_meging_indexing = [ ]
@ -5125,7 +5176,8 @@ class Eynollah_ocr:
if child_textlines . tag . endswith ( " Coords " ) :
cropped_lines_region_indexer . append ( indexer_text_region )
p_h = child_textlines . attrib [ ' points ' ] . split ( ' ' )
textline_coords = np . array ( [ [ int ( x . split ( ' , ' ) [ 0 ] ) , int ( x . split ( ' , ' ) [ 1 ] ) ] for x in p_h ] )
textline_coords = np . array (
[ [ int ( x . split ( ' , ' ) [ 0 ] ) , int ( x . split ( ' , ' ) [ 1 ] ) ] for x in p_h ] )
x , y , w , h = cv2 . boundingRect ( textline_coords )
h2w_ratio = h / float ( w )
@ -5154,7 +5206,6 @@ class Eynollah_ocr:
cropped_lines_meging_indexing . append ( 0 )
indexer_text_region = indexer_text_region + 1
extracted_texts = [ ]
n_iterations = math . ceil ( len ( cropped_lines ) / b_s )
@ -5172,7 +5223,11 @@ class Eynollah_ocr:
extracted_texts = extracted_texts + generated_text_merged
extracted_texts_merged = [ extracted_texts [ ind ] if cropped_lines_meging_indexing [ ind ] == 0 else extracted_texts [ ind ] + extracted_texts [ ind + 1 ] if cropped_lines_meging_indexing [ ind ] == 1 else None for ind in range ( len ( cropped_lines_meging_indexing ) ) ]
extracted_texts_merged = [
extracted_texts [ ind ] if cropped_lines_meging_indexing [ ind ] == 0 else extracted_texts [ ind ] +
extracted_texts [ ind + 1 ] if
cropped_lines_meging_indexing [ ind ] == 1 else None for ind in
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))
@ -5182,23 +5237,21 @@ class Eynollah_ocr:
#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 ) [ np . array ( cropped_lines_region_indexer ) == ind ]
extracted_texts_merged_un = np . array ( extracted_texts_merged ) [
np . array ( cropped_lines_region_indexer ) == ind ]
text_by_textregion . append ( " " . join ( extracted_texts_merged_un ) )
#print(len(text_by_textregion) , indexer_text_region, "text_by_textregion")
#print(time.time() - t0 ,'elapsed time')
indexer = 0
indexer_textregion = 0
for nn in root1 . iter ( region_tags ) :
text_subelement_textregion = ET . SubElement ( nn , ' TextEquiv ' )
unicode_textregion = ET . SubElement ( text_subelement_textregion , ' Unicode ' )
has_textline = False
for child_textregion in nn :
if child_textregion . tag . endswith ( " TextLine " ) :
@ -5211,8 +5264,6 @@ class Eynollah_ocr:
unicode_textregion . text = text_by_textregion [ indexer_textregion ]
indexer_textregion = indexer_textregion + 1
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)
@ -5223,7 +5274,6 @@ class Eynollah_ocr:
image_height = 32
b_s = 8
img_size = ( image_width , image_height )
for ind_img in ls_imgs :
@ -5258,7 +5308,8 @@ class Eynollah_ocr:
if child_textlines . tag . endswith ( " Coords " ) :
cropped_lines_region_indexer . append ( indexer_text_region )
p_h = child_textlines . attrib [ ' points ' ] . split ( ' ' )
textline_coords = np . array ( [ [ int ( x . split ( ' , ' ) [ 0 ] ) , int ( x . split ( ' , ' ) [ 1 ] ) ] for x in p_h ] )
textline_coords = np . array (
[ [ int ( x . split ( ' , ' ) [ 0 ] ) , int ( x . split ( ' , ' ) [ 1 ] ) ] for x in p_h ] )
x , y , w , h = cv2 . boundingRect ( textline_coords )
@ -5275,21 +5326,27 @@ class Eynollah_ocr:
if not self . export_textline_images_and_text :
if h2w_ratio > 0.05 :
img_fin = self . preprocess_and_resize_image_for_ocrcnn_model ( img_crop , image_height , image_width )
img_fin = self . preprocess_and_resize_image_for_ocrcnn_model ( img_crop ,
image_height ,
image_width )
cropped_lines . append ( img_fin )
cropped_lines_meging_indexing . append ( 0 )
else :
splited_images = self . return_textlines_split_if_needed ( img_crop )
if splited_images :
img_fin = self . preprocess_and_resize_image_for_ocrcnn_model ( splited_images [ 0 ] , image_height , image_width )
img_fin = self . preprocess_and_resize_image_for_ocrcnn_model (
splited_images [ 0 ] , image_height , image_width )
cropped_lines . append ( img_fin )
cropped_lines_meging_indexing . append ( 1 )
img_fin = self . preprocess_and_resize_image_for_ocrcnn_model ( splited_images [ 1 ] , image_height , image_width )
img_fin = self . preprocess_and_resize_image_for_ocrcnn_model (
splited_images [ 1 ] , image_height , image_width )
cropped_lines . append ( img_fin )
cropped_lines_meging_indexing . append ( - 1 )
else :
img_fin = self . preprocess_and_resize_image_for_ocrcnn_model ( img_crop , image_height , image_width )
img_fin = self . preprocess_and_resize_image_for_ocrcnn_model ( img_crop ,
image_height ,
image_width )
cropped_lines . append ( img_fin )
cropped_lines_meging_indexing . append ( 0 )
@ -5301,10 +5358,12 @@ class Eynollah_ocr:
if not textline_text :
pass
else :
with open ( os . path . join ( self . dir_out , file_name + ' _line_ ' + str ( indexer_textlines ) + ' .txt ' ) , ' w ' ) as text_file :
with open ( os . path . join ( self . dir_out , file_name + ' _line_ ' + str (
indexer_textlines ) + ' .txt ' ) , ' w ' ) as text_file :
text_file . write ( textline_text )
cv2 . imwrite ( os . path . join ( self . dir_out , file_name + ' _line_ ' + str ( indexer_textlines ) + ' .png ' ) , img_crop )
cv2 . imwrite ( os . path . join ( self . dir_out , file_name + ' _line_ ' + str (
indexer_textlines ) + ' .png ' ) , img_crop )
indexer_textlines + = 1
@ -5328,7 +5387,6 @@ class Eynollah_ocr:
imgs = cropped_lines [ n_start : n_end ]
imgs = np . array ( imgs ) . reshape ( b_s , image_height , image_width , 3 )
preds = self . prediction_model . predict ( imgs , verbose = 0 )
pred_texts = self . decode_batch_predictions ( preds )
@ -5336,14 +5394,19 @@ class Eynollah_ocr:
pred_texts_ib = pred_texts [ ib ] . strip ( " [UNK] " )
extracted_texts . append ( pred_texts_ib )
extracted_texts_merged = [ extracted_texts [ ind ] if cropped_lines_meging_indexing [ ind ] == 0 else extracted_texts [ ind ] + extracted_texts [ ind + 1 ] if cropped_lines_meging_indexing [ ind ] == 1 else None for ind in range ( len ( cropped_lines_meging_indexing ) ) ]
extracted_texts_merged = [
extracted_texts [ ind ] if cropped_lines_meging_indexing [ ind ] == 0 else extracted_texts [ ind ] +
extracted_texts [ ind + 1 ] if
cropped_lines_meging_indexing [ ind ] == 1 else None for ind in
range ( len ( cropped_lines_meging_indexing ) ) ]
extracted_texts_merged = [ ind for ind in extracted_texts_merged if ind is not None ]
unique_cropped_lines_region_indexer = np . 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 ) [ np . array ( cropped_lines_region_indexer ) == ind ]
extracted_texts_merged_un = np . array ( extracted_texts_merged ) [
np . array ( cropped_lines_region_indexer ) == ind ]
text_by_textregion . append ( " " . join ( extracted_texts_merged_un ) )
indexer = 0
@ -5352,7 +5415,6 @@ class Eynollah_ocr:
text_subelement_textregion = ET . SubElement ( nn , ' TextEquiv ' )
unicode_textregion = ET . SubElement ( text_subelement_textregion , ' Unicode ' )
has_textline = False
for child_textregion in nn :
if child_textregion . tag . endswith ( " TextLine " ) :
@ -5366,5 +5428,6 @@ class Eynollah_ocr:
indexer_textregion = indexer_textregion + 1
ET . register_namespace ( " " , name_space )
tree1 . write ( out_file_ocr , xml_declaration = True , method = ' xml ' , encoding = " utf8 " , default_namespace = None )
tree1 . write ( out_file_ocr , xml_declaration = True , method = ' xml ' , encoding = " utf8 " ,
default_namespace = None )
#print("Job done in %.1fs", time.time() - t0)