do_image_rotation / return_deskew_slop: avoid code duplication, simplify via mp.Pool

pull/142/head
Robert Sachunsky 2 weeks ago
parent 6fe02df973
commit 54cb15056b

@ -1,3 +1,4 @@
from functools import partial
import numpy as np import numpy as np
import cv2 import cv2
from scipy.signal import find_peaks from scipy.signal import find_peaks
@ -1570,19 +1571,15 @@ def separate_lines_new2(img_path, thetha, num_col, slope_region, plotter=None):
# plt.show() # plt.show()
return img_patch_ineterst_revised return img_patch_ineterst_revised
def do_image_rotation(queue_of_all_params,angels_per_process, img_resized, sigma_des): def do_image_rotation(angle, img, sigma_des):
angels_per_each_subprocess = [] print(f"rotating image by {angle}")
for mv in range(len(angels_per_process)): img_rot = rotate_image(img, angle)
print(f"rotating image by {angels_per_process[mv]}")
img_rot=rotate_image(img_resized,angels_per_process[mv])
img_rot[img_rot!=0] = 1 img_rot[img_rot!=0] = 1
try: try:
var_spectrum=find_num_col_deskew(img_rot,sigma_des,20.3 ) var = find_num_col_deskew(img_rot, sigma_des, 20.3)
except: except:
var_spectrum=0 var = 0
angels_per_each_subprocess.append(var_spectrum) return var
queue_of_all_params.put([angels_per_each_subprocess])
def return_deskew_slop(img_patch_org, sigma_des,n_tot_angles=100, main_page=False, plotter=None): def return_deskew_slop(img_patch_org, sigma_des,n_tot_angles=100, main_page=False, plotter=None):
num_cores = cpu_count() num_cores = cpu_count()
@ -1614,375 +1611,50 @@ def return_deskew_slop(img_patch_org, sigma_des,n_tot_angles=100, main_page=Fals
#plt.show() #plt.show()
if main_page and img_patch_org.shape[1] > img_patch_org.shape[0]: if main_page and img_patch_org.shape[1] > img_patch_org.shape[0]:
#plt.imshow(img_resized) #plt.imshow(img_resized)
#plt.show() #plt.show()
angels=np.array([-45, 0 , 45 , 90 , ])#np.linspace(-12,12,100)#np.array([0 , 45 , 90 , -45]) angles = np.array([-45, 0, 45, 90,])
angle = get_smallest_skew(img_resized, sigma_des, angles, num_cores=num_cores, plotter=plotter)
queue_of_all_params = Queue()
processes = []
nh = np.linspace(0, len(angels), num_cores + 1)
for i in range(num_cores):
angels_per_process = angels[int(nh[i]) : int(nh[i + 1])]
processes.append(Process(target=do_image_rotation, args=(queue_of_all_params, angels_per_process, img_resized, sigma_des)))
for i in range(num_cores):
processes[i].start()
var_res=[]
for i in range(num_cores):
list_all_par = queue_of_all_params.get(True)
angles_for_subprocess = list_all_par[0]
for j in range(len(angles_for_subprocess)):
var_res.append(angles_for_subprocess[j])
for i in range(num_cores):
processes[i].join()
###for rot in angels:
###img_rot=rotate_image(img_resized,rot)
####plt.imshow(img_rot)
####plt.show()
###img_rot[img_rot!=0]=1
####neg_peaks,var_spectrum=self.find_num_col_deskew(img_rot,sigma_des,20.3 )
####print(var_spectrum,'var_spectrum')
###try:
###var_spectrum=find_num_col_deskew(img_rot,sigma_des,20.3 )
#####print(rot,var_spectrum,'var_spectrum')
###except:
###var_spectrum=0
###var_res.append(var_spectrum)
angles = np.linspace(angle - 22.5, angle + 22.5, n_tot_angles)
angle = get_smallest_skew(img_resized, sigma_des, angles, num_cores=num_cores, plotter=plotter)
elif main_page:
try:
var_res=np.array(var_res)
ang_int=angels[np.argmax(var_res)]#angels_sorted[arg_final]#angels[arg_sort_early[arg_sort[arg_final]]]#angels[arg_fin]
except:
ang_int=0
angels=np.linspace(ang_int-22.5,ang_int+22.5,n_tot_angles)
queue_of_all_params = Queue()
processes = []
nh = np.linspace(0, len(angels), num_cores + 1)
for i in range(num_cores):
angels_per_process = angels[int(nh[i]) : int(nh[i + 1])]
processes.append(Process(target=do_image_rotation, args=(queue_of_all_params, angels_per_process, img_resized, sigma_des)))
for i in range(num_cores):
processes[i].start()
var_res=[]
for i in range(num_cores):
list_all_par = queue_of_all_params.get(True)
angles_for_subprocess = list_all_par[0]
for j in range(len(angles_for_subprocess)):
var_res.append(angles_for_subprocess[j])
for i in range(num_cores):
processes[i].join()
##var_res=[]
##for rot in angels:
##img_rot=rotate_image(img_resized,rot)
####plt.imshow(img_rot)
####plt.show()
##img_rot[img_rot!=0]=1
##try:
##var_spectrum=find_num_col_deskew(img_rot,sigma_des,20.3 )
##except:
##var_spectrum=0
##var_res.append(var_spectrum)
try:
var_res=np.array(var_res)
ang_int=angels[np.argmax(var_res)]#angels_sorted[arg_final]#angels[arg_sort_early[arg_sort[arg_final]]]#angels[arg_fin]
except:
ang_int=0
elif main_page and img_patch_org.shape[1]<=img_patch_org.shape[0]:
#plt.imshow(img_resized) #plt.imshow(img_resized)
#plt.show() #plt.show()
angels=np.linspace(-12,12,n_tot_angles)#np.array([0 , 45 , 90 , -45]) angles = np.linspace(-12, 12, n_tot_angles)#np.array([0 , 45 , 90 , -45])
angle = get_smallest_skew(img_resized, sigma_des, angles, num_cores=num_cores, plotter=plotter)
queue_of_all_params = Queue()
processes = []
nh = np.linspace(0, len(angels), num_cores + 1)
for i in range(num_cores):
angels_per_process = angels[int(nh[i]) : int(nh[i + 1])]
processes.append(Process(target=do_image_rotation, args=(queue_of_all_params, angels_per_process, img_resized, sigma_des)))
for i in range(num_cores):
processes[i].start()
var_res=[]
for i in range(num_cores):
list_all_par = queue_of_all_params.get(True)
angles_for_subprocess = list_all_par[0]
for j in range(len(angles_for_subprocess)):
var_res.append(angles_for_subprocess[j])
for i in range(num_cores):
processes[i].join()
##var_res=[]
##for rot in angels:
##img_rot=rotate_image(img_resized,rot)
###plt.imshow(img_rot)
###plt.show()
##img_rot[img_rot!=0]=1
###neg_peaks,var_spectrum=self.find_num_col_deskew(img_rot,sigma_des,20.3 )
###print(var_spectrum,'var_spectrum')
##try:
##var_spectrum=find_num_col_deskew(img_rot,sigma_des,20.3 )
##except:
##var_spectrum=0
##var_res.append(var_spectrum)
if plotter:
plotter.save_plot_of_rotation_angle(angels, var_res)
try:
var_res=np.array(var_res)
ang_int=angels[np.argmax(var_res)]#angels_sorted[arg_final]#angels[arg_sort_early[arg_sort[arg_final]]]#angels[arg_fin]
except:
ang_int=0
early_slope_edge=11 early_slope_edge=11
if abs(ang_int)>early_slope_edge and ang_int<0: if abs(angle) > early_slope_edge:
angels=np.linspace(-90,-12,n_tot_angles) if angle < 0:
angles = np.linspace(-90, -12, n_tot_angles)
queue_of_all_params = Queue()
processes = []
nh = np.linspace(0, len(angels), num_cores + 1)
for i in range(num_cores):
angels_per_process = angels[int(nh[i]) : int(nh[i + 1])]
processes.append(Process(target=do_image_rotation, args=(queue_of_all_params, angels_per_process, img_resized, sigma_des)))
for i in range(num_cores):
processes[i].start()
var_res=[]
for i in range(num_cores):
list_all_par = queue_of_all_params.get(True)
angles_for_subprocess = list_all_par[0]
for j in range(len(angles_for_subprocess)):
var_res.append(angles_for_subprocess[j])
for i in range(num_cores):
processes[i].join()
##var_res=[]
##for rot in angels:
##img_rot=rotate_image(img_resized,rot)
####plt.imshow(img_rot)
####plt.show()
##img_rot[img_rot!=0]=1
##try:
##var_spectrum=find_num_col_deskew(img_rot,sigma_des,20.3 )
##except:
##var_spectrum=0
##var_res.append(var_spectrum)
try:
var_res=np.array(var_res)
ang_int=angels[np.argmax(var_res)]#angels_sorted[arg_final]#angels[arg_sort_early[arg_sort[arg_final]]]#angels[arg_fin]
except:
ang_int=0
elif abs(ang_int)>early_slope_edge and ang_int>0:
angels=np.linspace(90,12,n_tot_angles)
queue_of_all_params = Queue()
processes = []
nh = np.linspace(0, len(angels), num_cores + 1)
for i in range(num_cores):
angels_per_process = angels[int(nh[i]) : int(nh[i + 1])]
processes.append(Process(target=do_image_rotation, args=(queue_of_all_params, angels_per_process, img_resized, sigma_des)))
for i in range(num_cores):
processes[i].start()
var_res=[]
for i in range(num_cores):
list_all_par = queue_of_all_params.get(True)
angles_for_subprocess = list_all_par[0]
for j in range(len(angles_for_subprocess)):
var_res.append(angles_for_subprocess[j])
for i in range(num_cores):
processes[i].join()
###var_res=[]
###for rot in angels:
###img_rot=rotate_image(img_resized,rot)
#####plt.imshow(img_rot)
#####plt.show()
###img_rot[img_rot!=0]=1
###try:
###var_spectrum=find_num_col_deskew(img_rot,sigma_des,20.3 )
####print(indexer,'indexer')
###except:
###var_spectrum=0
###var_res.append(var_spectrum)
try:
var_res=np.array(var_res)
ang_int=angels[np.argmax(var_res)]#angels_sorted[arg_final]#angels[arg_sort_early[arg_sort[arg_final]]]#angels[arg_fin]
except:
ang_int=0
else: else:
angels=np.linspace(-25,25,int(n_tot_angles/2.)+10) angles = np.linspace(90, 12, n_tot_angles)
indexer=0 angle = get_smallest_skew(img_resized, sigma_des, angles, num_cores=num_cores, plotter=plotter)
queue_of_all_params = Queue()
processes = []
nh = np.linspace(0, len(angels), num_cores + 1)
for i in range(num_cores):
angels_per_process = angels[int(nh[i]) : int(nh[i + 1])]
processes.append(Process(target=do_image_rotation, args=(queue_of_all_params, angels_per_process, img_resized, sigma_des)))
for i in range(num_cores):
processes[i].start()
var_res=[]
for i in range(num_cores):
list_all_par = queue_of_all_params.get(True)
angles_for_subprocess = list_all_par[0]
for j in range(len(angles_for_subprocess)):
var_res.append(angles_for_subprocess[j])
for i in range(num_cores):
processes[i].join()
####var_res=[]
####for rot in angels:
####img_rot=rotate_image(img_resized,rot)
#####plt.imshow(img_rot)
#####plt.show()
####img_rot[img_rot!=0]=1
#####neg_peaks,var_spectrum=self.find_num_col_deskew(img_rot,sigma_des,20.3 )
#####print(var_spectrum,'var_spectrum')
####try:
####var_spectrum=find_num_col_deskew(img_rot,sigma_des,20.3 )
####except:
####var_spectrum=0
####var_res.append(var_spectrum)
try:
var_res=np.array(var_res)
ang_int=angels[np.argmax(var_res)]#angels_sorted[arg_final]#angels[arg_sort_early[arg_sort[arg_final]]]#angels[arg_fin]
except:
ang_int=0
#plt.plot(var_res) else:
#plt.show() angles = np.linspace(-25, 25, int(0.5 * n_tot_angles) + 10)
##plt.plot(mom3_res) angle = get_smallest_skew(img_resized, sigma_des, angles, num_cores=num_cores, plotter=plotter)
##plt.show()
#print(ang_int,'ang_int111')
early_slope_edge=22 early_slope_edge=22
if abs(ang_int)>early_slope_edge and ang_int<0: if abs(angle) > early_slope_edge:
if angle < 0:
angels=np.linspace(-90,-25,int(n_tot_angles/2.)+10) angles = np.linspace(-90, -25, int(0.5 * n_tot_angles) + 10)
else:
queue_of_all_params = Queue() angles = np.linspace(90, 25, int(0.5 * n_tot_angles) + 10)
processes = [] angle = get_smallest_skew(img_resized, sigma_des, angles, num_cores=num_cores, plotter=plotter)
nh = np.linspace(0, len(angels), num_cores + 1)
for i in range(num_cores):
angels_per_process = angels[int(nh[i]) : int(nh[i + 1])]
processes.append(Process(target=do_image_rotation, args=(queue_of_all_params, angels_per_process, img_resized, sigma_des)))
for i in range(num_cores):
processes[i].start()
var_res=[]
for i in range(num_cores):
list_all_par = queue_of_all_params.get(True)
angles_for_subprocess = list_all_par[0]
for j in range(len(angles_for_subprocess)):
var_res.append(angles_for_subprocess[j])
for i in range(num_cores):
processes[i].join()
###var_res=[]
###for rot in angels:
###img_rot=rotate_image(img_resized,rot)
#####plt.imshow(img_rot)
#####plt.show()
###img_rot[img_rot!=0]=1
###try:
###var_spectrum=find_num_col_deskew(img_rot,sigma_des,20.3 )
###except:
###var_spectrum=0
###var_res.append(var_spectrum)
try:
var_res=np.array(var_res)
ang_int=angels[np.argmax(var_res)]#angels_sorted[arg_final]#angels[arg_sort_early[arg_sort[arg_final]]]#angels[arg_fin]
except:
ang_int=0
elif abs(ang_int)>early_slope_edge and ang_int>0:
angels=np.linspace(90,25,int(n_tot_angles/2.)+10)
indexer=0
queue_of_all_params = Queue()
processes = []
nh = np.linspace(0, len(angels), num_cores + 1)
for i in range(num_cores):
angels_per_process = angels[int(nh[i]) : int(nh[i + 1])]
processes.append(Process(target=do_image_rotation, args=(queue_of_all_params, angels_per_process, img_resized, sigma_des)))
for i in range(num_cores):
processes[i].start()
var_res=[]
for i in range(num_cores):
list_all_par = queue_of_all_params.get(True)
angles_for_subprocess = list_all_par[0]
for j in range(len(angles_for_subprocess)):
var_res.append(angles_for_subprocess[j])
for i in range(num_cores):
processes[i].join()
###var_res=[]
###for rot in angels: return angle
###img_rot=rotate_image(img_resized,rot)
#####plt.imshow(img_rot)
#####plt.show()
###img_rot[img_rot!=0]=1
###try:
###var_spectrum=find_num_col_deskew(img_rot,sigma_des,20.3 )
####print(indexer,'indexer')
###except:
###var_spectrum=0
###var_res.append(var_spectrum) def get_smallest_skew(img, sigma_des, angles, num_cores=1, plotter=None):
with Pool(processes=num_cores) as pool:
results = pool.map(partial(do_image_rotation, img=img, sigma_des=sigma_des), angles)
if plotter:
plotter.save_plot_of_rotation_angle(angles, results)
try: try:
var_res=np.array(var_res) var_res = np.array(results)
ang_int=angels[np.argmax(var_res)]#angels_sorted[arg_final]#angels[arg_sort_early[arg_sort[arg_final]]]#angels[arg_fin] angle = angles[np.argmax(var_res)]
except: except:
ang_int=0 angle = 0
return angle
return ang_int

Loading…
Cancel
Save