@ -1493,99 +1493,106 @@ def filter_small_drop_capitals_from_no_patch_layout(layout_no_patch, layout1):
def find_num_col_deskew ( regions_without_seperators , sigma_ , multiplier = 3.8 ) :
regions_without_seperators_0 = regions_without_seperators [ : , : ] . sum ( axis = 1 )
meda_n_updown = regions_without_seperators_0 [ len ( regions_without_seperators_0 ) : : - 1 ]
##meda_n_updown=regions_without_seperators_0[len(regions_without_seperators_0)::-1 ]
first_nonzero = next ( ( i for i , x in enumerate ( regions_without_seperators_0 ) if x ) , 0 )
last_nonzero = next ( ( i for i , x in enumerate ( meda_n_updown ) if x ) , 0 )
##first_nonzero=(next((i for i, x in enumerate(regions_without_seperators_0) if x), 0))
##last_nonzero=(next((i for i, x in enumerate(meda_n_updown) if x), 0))
##last_nonzero=len(regions_without_seperators_0)-last_nonzero
last_nonzero = len ( regions_without_seperators_0 ) - last_nonzero
y = regions_without_seperators_0 #[first_nonzero:last_nonzero]
y_help = np . zeros ( len ( y ) + 20 )
##y_help=np.zeros(len(y)+20 )
y_help [ 10 : len ( y ) + 10 ] = y
##y_help[10:len(y)+10]= y
x = np . array ( range ( len ( y ) ) )
##x=np.array( range(len(y)) )
zneg_rev = - y_help + np . max ( y_help )
zneg = np . zeros ( len ( zneg_rev ) + 20 )
zneg [ 10 : len ( zneg_rev ) + 10 ] = zneg_rev
##zneg_rev=-y_help+np.max(y_help)
##zneg=np.zeros(len(zneg_rev)+20)
##zneg[10:len(zneg_rev)+10]=zneg_rev
z = gaussian_filter1d ( y , sigma_ )
zneg = gaussian_filter1d ( zneg , sigma_ )
###zneg= gaussian_filter1d(zneg, sigma_ )
peaks_neg , _ = find_peaks ( zneg , height = 0 )
peaks , _ = find_peaks ( z , height = 0 )
peaks_neg = peaks_neg - 10 - 10
###peaks_neg, _ = find_peaks(zneg, height=0)
###peaks, _ = find_peaks(z, height=0)
# print(np.std(z),'np.std(z)np.std(z)np.std(z)')
# ##peaks_neg=peaks_neg-10-10
##plt.plot(z)
##plt.show()
####print(np.std(z),'np.std(z)np.std(z)np.std(z)')
##plt.imshow(regions_without_seperators)
##plt.show()
"""
last_nonzero = last_nonzero - 0 #100
first_nonzero = first_nonzero + 0 #+100
#####plt.plot(z)
#####plt.show()
peaks_neg = peaks_neg [ ( peaks_neg > first_nonzero ) & ( peaks_neg < last_nonzero ) ]
#####plt.imshow(regions_without_seperators)
#####plt.show()
###"""
###last_nonzero=last_nonzero-0#100
###first_nonzero=first_nonzero+0#+100
peaks = peaks [ ( peaks > .06 * regions_without_seperators . shape [ 1 ] ) & ( peaks < 0.94 * regions_without_seperators . shape [ 1 ] ) ]
"""
interest_pos = z [ peaks ]
###peaks_neg=peaks_neg[(peaks_neg>first_nonzero) & (peaks_neg<last_nonzero)]
interest_pos = interest_pos [ interest_pos > 10 ]
###peaks=peaks[(peaks>.06*regions_without_seperators.shape[1]) & (peaks<0.94*regions_without_seperators.shape[1])]
###"""
###interest_pos=z[peaks]
interest_neg = z [ peaks_neg ]
###interest_pos=interest_pos[interest_pos>10 ]
min_peaks_pos = np . mean ( interest_pos )
min_peaks_neg = 0 # np.min(interest_neg)
###interest_neg=z[peaks_neg]
dis_talaei = ( min_peaks_pos - min_peaks_neg ) / multiplier
# print(interest_pos)
grenze = min_peaks_pos - dis_talaei # np.mean(y[peaks_neg[0]:peaks_neg[len(peaks_neg)-1]])-np.std(y[peaks_neg[0]:peaks_neg[len(peaks_neg)-1]])/2.0
###min_peaks_pos=np.mean(interest_pos)
###min_peaks_neg=0#np.min(interest_neg)
interest_neg_fin = interest_neg [ ( interest_neg < grenze ) ]
peaks_neg_fin = peaks_neg [ ( interest_neg < grenze ) ]
interest_neg_fin = interest_neg [ ( interest_neg < grenze ) ]
###dis_talaei=(min_peaks_pos-min_peaks_neg)/multiplier
####print(interest_pos)
###grenze=min_peaks_pos-dis_talaei#np.mean(y[peaks_neg[0]:peaks_neg[len(peaks_neg)-1]])-np.std(y[peaks_neg[0]:peaks_neg[len(peaks_neg)-1]])/2.0
"""
if interest_neg [ 0 ] < 0.1 :
interest_neg = interest_neg [ 1 : ]
if interest_neg [ len ( interest_neg ) - 1 ] < 0.1 :
interest_neg = interest_neg [ : len ( interest_neg ) - 1 ]
###interest_neg_fin=interest_neg[(interest_neg<grenze)]
###peaks_neg_fin=peaks_neg[(interest_neg<grenze)]
###interest_neg_fin=interest_neg[(interest_neg<grenze)]
###"""
###if interest_neg[0]<0.1:
###interest_neg=interest_neg[1:]
###if interest_neg[len(interest_neg)-1]<0.1:
###interest_neg=interest_neg[:len(interest_neg)-1]
min_peaks_pos = np . min ( interest_pos )
min_peaks_neg = 0 #np.min(interest_neg)
###min_peaks_pos=np.min(interest_pos)
###min_peaks_neg=0#np.min(interest_neg)
dis_talaei = ( min_peaks_pos - min_peaks_neg ) / multiplier
grenze = min_peaks_pos - dis_talaei #np.mean(y[peaks_neg[0]:peaks_neg[len(peaks_neg)-1]])-np.std(y[peaks_neg[0]:peaks_neg[len(peaks_neg)-1]])/2.0
"""
# interest_neg_fin=interest_neg#[(interest_neg<grenze)]
# peaks_neg_fin=peaks_neg#[(interest_neg<grenze)]
# interest_neg_fin=interest_neg#[(interest_neg<grenze)]
num_col = ( len ( interest_neg_fin ) ) + 1
###dis_talaei=(min_peaks_pos-min_peaks_neg)/multiplier
###grenze=min_peaks_pos-dis_talaei#np.mean(y[peaks_neg[0]:peaks_neg[len(peaks_neg)-1]])-np.std(y[peaks_neg[0]:peaks_neg[len(peaks_neg)-1]])/2.0
###"""
####interest_neg_fin=interest_neg#[(interest_neg<grenze)]
####peaks_neg_fin=peaks_neg#[(interest_neg<grenze)]
####interest_neg_fin=interest_neg#[(interest_neg<grenze)]
p_l = 0
p_u = len ( y ) - 1
p_m = int ( len ( y ) / 2.0 )
p_g_l = int ( len ( y ) / 3.0 )
p_g_u = len ( y ) - int ( len ( y ) / 3.0 )
###num_col=(len(interest_neg_fin))+1
diff_peaks = np . abs ( np . diff ( peaks_neg_fin ) )
diff_peaks_annormal = diff_peaks [ diff_peaks < 30 ]
###p_l=0
###p_u=len(y)-1
###p_m=int(len(y)/2.)
###p_g_l=int(len(y)/3.)
###p_g_u=len(y)-int(len(y)/3.)
###diff_peaks=np.abs( np.diff(peaks_neg_fin) )
###diff_peaks_annormal=diff_peaks[diff_peaks<30]
#print(len(interest_neg_fin),np.mean(interest_neg_fin))
return interest_neg_fin , np . std ( z )
return np. std ( z ) # interest_neg_fin,np.std(z)
def return_hor_spliter_by_index_for_without_verticals ( peaks_neg_fin_t , x_min_hor_some , x_max_hor_some ) :
# print(peaks_neg_fin_t,x_min_hor_some,x_max_hor_some)
@ -2343,20 +2350,22 @@ def return_regions_without_seperators(regions_pre):
def return_deskew_slop ( img_patch_org , sigma_des , main_page = False , dir_of_all = None , f_name = None ) :
if main_page and dir_of_all is not None :
plt . figure ( figsize = ( 70 , 40 ) )
plt . rcParams [ " font.size " ] = " 50 "
plt . rcParams [ ' font.size ' ] = ' 50 '
plt . subplot ( 1 , 2 , 1 )
plt . imshow ( img_patch_org )
plt . subplot ( 1 , 2 , 2 )
plt . plot ( gaussian_filter1d ( img_patch_org . sum ( axis = 1 ) , 3 ) , np . array ( range ( len ( gaussian_filter1d ( img_patch_org . sum ( axis = 1 ) , 3 ) ) ) ) , linewidth = 8 )
plt . xlabel ( " Density of textline prediction in direction of X axis " , fontsize = 60 )
plt . ylabel ( " Height " , fontsize = 60 )
plt . xlabel ( ' Density of textline prediction in direction of X axis ' , fontsize = 60 )
plt . ylabel ( ' Height ' , fontsize = 60 )
plt . yticks ( [ 0 , len ( gaussian_filter1d ( img_patch_org . sum ( axis = 1 ) , 3 ) ) ] )
plt . gca ( ) . invert_yaxis ( )
plt . savefig ( os . path . join ( dir_of_all , f_name + " _density_of_textline.png " ) )
plt . savefig ( os . path . join ( dir_of_all , f_name + ' _density_of_textline.png ' ) )
#print(np.max(img_patch_org.sum(axis=0)) ,np.max(img_patch_org.sum(axis=1)),'axislar')
#img_patch_org=resize_image(img_patch_org,int(img_patch_org.shape[0]*2.5),int(img_patch_org.shape[1]/2.5))
@ -2366,60 +2375,76 @@ def return_deskew_slop(img_patch_org, sigma_des, main_page=False, dir_of_all=Non
img_int = np . zeros ( ( img_patch_org . shape [ 0 ] , img_patch_org . shape [ 1 ] ) )
img_int [ : , : ] = img_patch_org [ : , : ] #img_patch_org[:,:,0]
img_resized = np . zeros ( ( int ( img_int . shape [ 0 ] * ( 1.8 ) ) , int ( img_int . shape [ 1 ] * ( 2.6 ) ) ) )
img_resized [ int ( img_int . shape [ 0 ] * ( 0.4 ) ) : int ( img_int . shape [ 0 ] * ( 0.4 ) ) + img_int . shape [ 0 ] , int ( img_int . shape [ 1 ] * ( 0.8 ) ) : int ( img_int . shape [ 1 ] * ( 0.8 ) ) + img_int . shape [ 1 ] ] = img_int [ : , : ]
max_shape = np . max ( img_int . shape )
img_resized = np . zeros ( ( int ( max_shape * ( 1.1 ) ) , int ( max_shape * ( 1.1 ) ) ) )
onset_x = int ( ( img_resized . shape [ 1 ] - img_int . shape [ 1 ] ) / 2. )
onset_y = int ( ( img_resized . shape [ 0 ] - img_int . shape [ 0 ] ) / 2. )
#img_resized=np.zeros((int( img_int.shape[0]*(1.8) ) , int( img_int.shape[1]*(2.6) ) ))
#img_resized[ int( img_int.shape[0]*(.4)):int( img_int.shape[0]*(.4))+img_int.shape[0] , int( img_int.shape[1]*(.8)):int( img_int.shape[1]*(.8))+img_int.shape[1] ]=img_int[:,:]
img_resized [ onset_y : onset_y + img_int . shape [ 0 ] , onset_x : onset_x + img_int . shape [ 1 ] ] = img_int [ : , : ]
#print(img_resized.shape,'img_resizedshape')
#plt.imshow(img_resized)
#plt.show()
if main_page and img_patch_org . shape [ 1 ] > img_patch_org . shape [ 0 ] :
#plt.imshow(img_resized)
#plt.show()
angels = np . array (
[
- 45 ,
0 ,
45 ,
90 ,
]
) # np.linspace(-12,12,100)#np.array([0 , 45 , 90 , -45])
res = [ ]
num_of_peaks = [ ]
index_cor = [ ]
angels = np . array ( [ - 45 , 0 , 45 , 90 , ] ) #np.linspace(-12,12,100)#np.array([0 , 45 , 90 , -45])
#res=[]
#num_of_peaks=[]
#index_cor=[]
var_res = [ ]
indexer = 0
#indexer=0
for rot in angels :
img_rot = rotate_image ( img_resized , rot )
img_rot = self . rotate_image ( img_resized , rot )
#plt.imshow(img_rot)
#plt.show()
img_rot [ img_rot != 0 ] = 1
# res_me=np.mean(find_num_col_deskew(img_rot,sigma_des,2.0 ))
# res_me=np.mean(self. find_num_col_deskew(img_rot,sigma_des,2.0 ))
# neg_peaks,var_spectrum=find_num_col_deskew(img_rot,sigma_des,20.3 )
#neg_peaks,var_spectrum=self.find_num_col_deskew(img_rot,sigma_des,20.3 )
#print(var_spectrum,'var_spectrum')
try :
neg_peaks, var_spectrum = find_num_col_deskew ( img_rot , sigma_des , 20.3 )
# print(rot,var_spectrum,'var_spectrum')
res_me = np . mean ( neg_peaks )
if res_me == 0 :
res_me = VERY_LARGE_NUMBER
else :
pass
var_spectrum= self . find_num_col_deskew ( img_rot , sigma_des , 20.3 )
# # print(rot,var_spectrum,'var_spectrum')
#res_me=np.mean(neg_peaks )
#if res_me==0 :
#res_me=1000000000000000000000
#else :
# pass
res_num = len ( neg_peaks )
#res_num=len(neg_peaks )
except :
res_me = VERY_LARGE_NUMBER
res_num = 0
#res_me=1000000000000000000000
#res_num= 0
var_spectrum = 0
if isNaN ( res_me ) :
pass
else :
res . append ( res_me )
#if self.isNaN(res_me):
#pass
#else:
#res.append( res_me )
#var_res.append(var_spectrum)
#num_of_peaks.append( res_num )
#index_cor.append(indexer)
#indexer=indexer+1
var_res . append ( var_spectrum )
num_of_peaks . append ( res_num )
index_cor . append ( indexer )
indexer = indexer + 1
#index_cor.append(indexer )
#indexer=indexer+1
try :
var_res = np . array ( var_res )
@ -2428,42 +2453,31 @@ def return_deskew_slop(img_patch_org, sigma_des, main_page=False, dir_of_all=Non
except :
ang_int = 0
angels = np . linspace ( ang_int - 22.5 , ang_int + 22.5 , 100 )
res = [ ]
num_of_peaks = [ ]
index_cor = [ ]
#res=[ ]
#num_of_peaks=[ ]
#index_cor=[ ]
var_res = [ ]
indexer = 0
for rot in angels :
img_rot = rotate_image ( img_resized , rot )
img_rot = self . rotate_image ( img_resized , rot )
##plt.imshow(img_rot)
##plt.show()
img_rot [ img_rot != 0 ] = 1
# res_me=np.mean(find_num_col_deskew(img_rot,sigma_des,2.0 ))
# res_me=np.mean(self. find_num_col_deskew(img_rot,sigma_des,2.0 ))
try :
neg_peaks , var_spectrum = find_num_col_deskew ( img_rot , sigma_des , 20.3 )
# print(indexer,'indexer')
res_me = np . mean ( neg_peaks )
if res_me == 0 :
res_me = VERY_LARGE_NUMBER
else :
pass
var_spectrum = self . find_num_col_deskew ( img_rot , sigma_des , 20.3 )
res_num = len ( neg_peaks )
except :
res_me = VERY_LARGE_NUMBER
res_num = 0
var_spectrum = 0
if isNaN ( res_me ) :
pass
else :
res . append ( res_me )
var_res . append ( var_spectrum )
num_of_peaks . append ( res_num )
index_cor . append ( indexer )
indexer = indexer + 1
try :
var_res = np . array ( var_res )
@ -2478,55 +2492,40 @@ def return_deskew_slop(img_patch_org, sigma_des, main_page=False, dir_of_all=Non
#plt.show()
angels = np . linspace ( - 12 , 12 , 100 ) #np.array([0 , 45 , 90 , -45])
res = [ ]
num_of_peaks = [ ]
index_cor = [ ]
var_res = [ ]
indexer = 0
for rot in angels :
img_rot = rotate_image ( img_resized , rot )
img_rot = self . rotate_image ( img_resized , rot )
#plt.imshow(img_rot)
#plt.show()
img_rot [ img_rot != 0 ] = 1
# res_me=np.mean(find_num_col_deskew(img_rot,sigma_des,2.0 ))
#res_me=np.mean(self.find_num_col_deskew(img_rot,sigma_des,2.0 ))
# neg_peaks,var_spectrum=find_num_col_deskew(img_rot,sigma_des,20.3 )
# neg_peaks,var_spectrum=self. find_num_col_deskew(img_rot,sigma_des,20.3 )
#print(var_spectrum,'var_spectrum')
try :
neg_peaks , var_spectrum = find_num_col_deskew ( img_rot , sigma_des , 20.3 )
# print(rot,var_spectrum,'var_spectrum')
res_me = np . mean ( neg_peaks )
if res_me == 0 :
res_me = VERY_LARGE_NUMBER
else :
pass
var_spectrum = self . find_num_col_deskew ( img_rot , sigma_des , 20.3 )
res_num = len ( neg_peaks )
except :
res_me = VERY_LARGE_NUMBER
res_num = 0
var_spectrum = 0
if isNaN ( res_me ) :
pass
else :
res . append ( res_me )
var_res . append ( var_spectrum )
num_of_peaks . append ( res_num )
index_cor . append ( indexer )
indexer = indexer + 1
if dir_of_all is not None :
print ( " galdi? " )
if self . dir_of_all is not None :
#print('galdi?')
plt . figure ( figsize = ( 60 , 30 ) )
plt . rcParams [ " font.size " ] = " 50 "
plt . plot ( angels , np . array ( var_res ) , " -o " , markersize = 25 , linewidth = 4 )
plt . xlabel ( " angle " , fontsize = 50 )
plt . ylabel ( " variance of sum of rotated textline in direction of x axis " , fontsize = 50 )
plt . rcParams [ ' font.size ' ] = ' 50 '
plt . plot ( angels , np . array ( var_res ) , ' -o ' , markersize = 25 , linewidth = 4 )
plt . xlabel ( ' angle ' , fontsize = 50 )
plt . ylabel ( ' variance of sum of rotated textline in direction of x axis ' , fontsize = 50 )
plt . plot ( angels [ np . argmax ( var_res ) ] , var_res [ np . argmax ( np . array ( var_res ) ) ] , ' * ' , markersize = 50 , label = ' Angle of deskewing= ' + str ( " {:.2f} " . format ( angels [ np . argmax ( var_res ) ] ) ) + r ' $ \ degree$ ' )
plt . legend ( loc = ' best ' )
plt . savefig ( os . path . join ( self . dir_of_all , self . f_name + ' _rotation_angle.png ' ) )
plt . plot ( angels [ np . argmax ( var_res ) ] , var_res [ np . argmax ( np . array ( var_res ) ) ] , " * " , markersize = 50 , label = " Angle of deskewing= " + str ( " {:.2f} " . format ( angels [ np . argmax ( var_res ) ] ) ) + r " $ \ degree$ " )
plt . legend ( loc = " best " )
plt . savefig ( os . path . join ( dir_of_all , f_name + " _rotation_angle.png " ) )
try :
var_res = np . array ( var_res )
@ -2535,45 +2534,27 @@ def return_deskew_slop(img_patch_org, sigma_des, main_page=False, dir_of_all=Non
except :
ang_int = 0
early_slope_edge = 11
if abs ( ang_int ) > early_slope_edge and ang_int < 0 :
angels = np . linspace ( - 90 , - 12 , 100 )
res = [ ]
num_of_peaks = [ ]
index_cor = [ ]
var_res = [ ]
indexer = 0
for rot in angels :
img_rot = rotate_image ( img_resized , rot )
img_rot = self . rotate_image ( img_resized , rot )
##plt.imshow(img_rot)
##plt.show()
img_rot [ img_rot != 0 ] = 1
# res_me=np.mean(find_num_col_deskew(img_rot,sigma_des,2.0 ))
# res_me=np.mean(self. find_num_col_deskew(img_rot,sigma_des,2.0 ))
try :
neg_peaks , var_spectrum = find_num_col_deskew ( img_rot , sigma_des , 20.3 )
# print(indexer,'indexer')
res_me = np . mean ( neg_peaks )
if res_me == 0 :
res_me = VERY_LARGE_NUMBER
else :
pass
res_num = len ( neg_peaks )
var_spectrum = self . find_num_col_deskew ( img_rot , sigma_des , 20.3 )
except :
res_me = VERY_LARGE_NUMBER
res_num = 0
var_spectrum = 0
if isNaN ( res_me ) :
pass
else :
res . append ( res_me )
var_res . append ( var_spectrum )
num_of_peaks . append ( res_num )
index_cor . append ( indexer )
indexer = indexer + 1
try :
var_res = np . array ( var_res )
@ -2586,40 +2567,23 @@ def return_deskew_slop(img_patch_org, sigma_des, main_page=False, dir_of_all=Non
angels = np . linspace ( 90 , 12 , 100 )
res = [ ]
num_of_peaks = [ ]
index_cor = [ ]
var_res = [ ]
indexer = 0
for rot in angels :
img_rot = rotate_image ( img_resized , rot )
img_rot = self . rotate_image ( img_resized , rot )
##plt.imshow(img_rot)
##plt.show()
img_rot [ img_rot != 0 ] = 1
# res_me=np.mean(find_num_col_deskew(img_rot,sigma_des,2.0 ))
# res_me=np.mean(self. find_num_col_deskew(img_rot,sigma_des,2.0 ))
try :
neg_peaks, var_spectrum = find_num_col_deskew ( img_rot , sigma_des , 20.3 )
var_spectrum= self . find_num_col_deskew ( img_rot , sigma_des , 20.3 )
#print(indexer,'indexer')
res_me = np . mean ( neg_peaks )
if res_me == 0 :
res_me = VERY_LARGE_NUMBER
else :
pass
res_num = len ( neg_peaks )
except :
res_me = VERY_LARGE_NUMBER
res_num = 0
var_spectrum = 0
if isNaN ( res_me ) :
pass
else :
res . append ( res_me )
var_res . append ( var_spectrum )
num_of_peaks . append ( res_num )
index_cor . append ( indexer )
indexer = indexer + 1
try :
var_res = np . array ( var_res )
@ -2629,45 +2593,29 @@ def return_deskew_slop(img_patch_org, sigma_des, main_page=False, dir_of_all=Non
ang_int = 0
else :
angels = np . linspace ( - 25 , 25 , 60 )
res = [ ]
num_of_peaks = [ ]
index_cor = [ ]
var_res = [ ]
indexer = 0
for rot in angels :
img_rot = rotate_image ( img_resized , rot )
img_rot = self . rotate_image ( img_resized , rot )
#plt.imshow(img_rot)
#plt.show()
img_rot [ img_rot != 0 ] = 1
# res_me=np.mean(find_num_col_deskew(img_rot,sigma_des,2.0 ))
#res_me=np.mean(self.find_num_col_deskew(img_rot,sigma_des,2.0 ))
# neg_peaks,var_spectrum=find_num_col_deskew(img_rot,sigma_des,20.3 )
# neg_peaks,var_spectrum=self. find_num_col_deskew(img_rot,sigma_des,20.3 )
#print(var_spectrum,'var_spectrum')
try :
neg_peaks , var_spectrum = find_num_col_deskew ( img_rot , sigma_des , 20.3 )
# print(rot,var_spectrum,'var_spectrum')
res_me = np . mean ( neg_peaks )
if res_me == 0 :
res_me = VERY_LARGE_NUMBER
else :
pass
res_num = len ( neg_peaks )
var_spectrum = self . find_num_col_deskew ( img_rot , sigma_des , 20.3 )
except :
res_me = VERY_LARGE_NUMBER
res_num = 0
var_spectrum = 0
if isNaN ( res_me ) :
pass
else :
res . append ( res_me )
var_res . append ( var_spectrum )
num_of_peaks . append ( res_num )
index_cor . append ( indexer )
indexer = indexer + 1
try :
var_res = np . array ( var_res )
@ -2676,47 +2624,35 @@ def return_deskew_slop(img_patch_org, sigma_des, main_page=False, dir_of_all=Non
except :
ang_int = 0
# print(ang_int,'ang_int')
#plt.plot(var_res)
#plt.show()
##plt.plot(mom3_res)
##plt.show()
#print(ang_int,'ang_int111')
early_slope_edge = 22
if abs ( ang_int ) > early_slope_edge and ang_int < 0 :
angels = np . linspace ( - 90 , - 25 , 60 )
res = [ ]
num_of_peaks = [ ]
index_cor = [ ]
var_res = [ ]
indexer = 0
for rot in angels :
img_rot = rotate_image ( img_resized , rot )
img_rot = self . rotate_image ( img_resized , rot )
##plt.imshow(img_rot)
##plt.show()
img_rot [ img_rot != 0 ] = 1
# res_me=np.mean(find_num_col_deskew(img_rot,sigma_des,2.0 ))
# res_me=np.mean(self. find_num_col_deskew(img_rot,sigma_des,2.0 ))
try :
neg_peaks , var_spectrum = find_num_col_deskew ( img_rot , sigma_des , 20.3 )
# print(indexer,'indexer')
res_me = np . mean ( neg_peaks )
if res_me == 0 :
res_me = VERY_LARGE_NUMBER
else :
pass
var_spectrum = self . find_num_col_deskew ( img_rot , sigma_des , 20.3 )
res_num = len ( neg_peaks )
except :
res_me = VERY_LARGE_NUMBER
res_num = 0
var_spectrum = 0
if isNaN ( res_me ) :
pass
else :
res . append ( res_me )
var_res . append ( var_spectrum )
num_of_peaks . append ( res_num )
index_cor . append ( indexer )
indexer = indexer + 1
try :
var_res = np . array ( var_res )
@ -2729,40 +2665,24 @@ def return_deskew_slop(img_patch_org, sigma_des, main_page=False, dir_of_all=Non
angels = np . linspace ( 90 , 25 , 60 )
res = [ ]
num_of_peaks = [ ]
index_cor = [ ]
var_res = [ ]
indexer = 0
for rot in angels :
img_rot = rotate_image ( img_resized , rot )
img_rot = self . rotate_image ( img_resized , rot )
##plt.imshow(img_rot)
##plt.show()
img_rot [ img_rot != 0 ] = 1
# res_me=np.mean(find_num_col_deskew(img_rot,sigma_des,2.0 ))
# res_me=np.mean(self. find_num_col_deskew(img_rot,sigma_des,2.0 ))
try :
neg_peaks, var_spectrum = find_num_col_deskew ( img_rot , sigma_des , 20.3 )
var_spectrum= self . find_num_col_deskew ( img_rot , sigma_des , 20.3 )
#print(indexer,'indexer')
res_me = np . mean ( neg_peaks )
if res_me == 0 :
res_me = VERY_LARGE_NUMBER
else :
pass
res_num = len ( neg_peaks )
except :
res_me = VERY_LARGE_NUMBER
res_num = 0
var_spectrum = 0
if isNaN ( res_me ) :
pass
else :
res . append ( res_me )
var_res . append ( var_spectrum )
num_of_peaks . append ( res_num )
index_cor . append ( indexer )
indexer = indexer + 1
try :
var_res = np . array ( var_res )
@ -2773,6 +2693,7 @@ def return_deskew_slop(img_patch_org, sigma_des, main_page=False, dir_of_all=Non
return ang_int
def put_drop_out_from_only_drop_model ( layout_no_patch , layout1 ) :
drop_only = ( layout_no_patch [ : , : , 0 ] == 4 ) * 1
@ -2826,11 +2747,11 @@ def putt_bb_of_drop_capitals_of_model_in_patches_in_layout(layout_in_patch):
return layout_in_patch
def check_any_text_region_in_model_one_is_main_or_header ( regions_model_1 , regions_model_full , contours_only_text_parent , all_box_coord , all_found_texline_polygons , slopes , contours_only_text_parent_d_ordered ) :
text_only = ( regions_model_1 [ : , : ] == 1 ) * 1
contours_only_text , hir_on_text = return_contours_of_image ( text_only )
#text_only=(regions_model_1[:,:]==1)* 1
#contours_only_text,hir_on_text=self.return_contours_of_image(text_only )
"""
contours_only_text_parent = return_parent_contours ( contours_only_text , hir_on_text )
contours_only_text_parent = self . return_parent_contours ( contours_only_text , hir_on_text )
areas_cnt_text = np . array ( [ cv2 . contourArea ( contours_only_text_parent [ j ] ) for j in range ( len ( contours_only_text_parent ) ) ] )
areas_cnt_text = areas_cnt_text / float ( text_only . shape [ 0 ] * text_only . shape [ 1 ] )
@ -2842,11 +2763,13 @@ def check_any_text_region_in_model_one_is_main_or_header(regions_model_1, region
contours_only_text_parent = [ contours_only_text_parent [ jz ] for jz in range ( len ( contours_only_text_parent ) ) if areas_cnt_text [ jz ] > 0.00001 ]
"""
cx_main , cy_main , x_min_main , x_max_main , y_min_main , y_max_main , y_corr_x_min_from_argmin = find_new_features_of_contoures ( contours_only_text_parent )
cx_main , cy_main , x_min_main , x_max_main , y_min_main , y_max_main , y_corr_x_min_from_argmin = self . find_new_features_of_contoures ( contours_only_text_parent )
length_con = x_max_main - x_min_main
height_con = y_max_main - y_min_main
all_found_texline_polygons_main = [ ]
all_found_texline_polygons_head = [ ]
@ -2867,11 +2790,14 @@ def check_any_text_region_in_model_one_is_main_or_header(regions_model_1, region
img = np . zeros ( ( regions_model_1 . shape [ 0 ] , regions_model_1 . shape [ 1 ] , 3 ) )
img = cv2 . fillPoly ( img , pts = [ con ] , color = ( 255 , 255 , 255 ) )
all_pixels = ( ( img [ : , : , 0 ] == 255 ) * 1 ) . sum ( )
pixels_header = ( ( ( img [ : , : , 0 ] == 255 ) & ( regions_model_full [ : , : , 0 ] == 2 ) ) * 1 ) . sum ( )
pixels_main = all_pixels - pixels_header
if ( pixels_header > = pixels_main ) and ( ( length_con [ ii ] / float ( height_con [ ii ] ) ) > = 1.3 ) :
regions_model_1 [ : , : ] [ ( regions_model_1 [ : , : ] == 1 ) & ( img [ : , : , 0 ] == 255 ) ] = 2
contours_only_text_parent_head . append ( con )
@ -2891,6 +2817,8 @@ def check_any_text_region_in_model_one_is_main_or_header(regions_model_1, region
#print(all_pixels,pixels_main,pixels_header)
#plt.imshow(img[:,:,0])
#plt.show()
return regions_model_1 , contours_only_text_parent_main , contours_only_text_parent_head , all_box_coord_main , all_box_coord_head , all_found_texline_polygons_main , all_found_texline_polygons_head , slopes_main , slopes_head , contours_only_text_parent_main_d , contours_only_text_parent_head_d