doing more multiprocessing in order to make the process faster

pull/138/head^2
vahidrezanezhad 2 months ago
parent 3ef4eac24c
commit f93fa12441

@ -2253,7 +2253,7 @@ class Eynollah:
else:
prediction_bin = self.do_prediction(True, img_resized, self.model_bin, n_batch_inference=5)
print("inside bin ", time.time()-t_bin)
#print("inside bin ", time.time()-t_bin)
prediction_bin=prediction_bin[:,:,0]
prediction_bin = (prediction_bin[:,:]==0)*1
prediction_bin = prediction_bin*255
@ -2266,7 +2266,7 @@ class Eynollah:
else:
img_bin = np.copy(img_resized)
print("inside 1 ", time.time()-t_in)
#print("inside 1 ", time.time()-t_in)
###textline_mask_tot_ea = self.run_textline(img_bin)
textline_mask_tot_ea = self.run_textline(img_resized, num_col_classifier)
@ -2281,7 +2281,7 @@ class Eynollah:
#plt.imshwo(self.image_page_org_size)
#plt.show()
if not skip_layout_and_reading_order:
print("inside 2 ", time.time()-t_in)
#print("inside 2 ", time.time()-t_in)
if not self.dir_in:
if num_col_classifier == 1 or num_col_classifier >= 2:
@ -2309,7 +2309,7 @@ class Eynollah:
prediction_regions_org = self.do_prediction_new_concept(True, img_bin, self.model_region, n_batch_inference=3)
###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)
#print("inside 3 ", time.time()-t_in)
#plt.imshow(prediction_regions_org[:,:,0])
#plt.show()
@ -2395,7 +2395,7 @@ class Eynollah:
#plt.imshow(textline_mask_tot_ea)
#plt.show()
print("inside 4 ", time.time()-t_in)
#print("inside 4 ", time.time()-t_in)
return text_regions_p_true, erosion_hurts, polygons_lines_xml, textline_mask_tot_ea, img_bin
else:
img_bin = resize_image(img_bin,img_height_h, img_width_h )
@ -3368,7 +3368,7 @@ class Eynollah:
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)
print(time.time()-t_0_box,'time box in 1')
#print(time.time()-t_0_box,'time box in 1')
if self.tables:
regions_without_separators[table_prediction ==1 ] = 1
if np.abs(slope_deskew) < SLOPE_THRESHOLD:
@ -3381,7 +3381,7 @@ class Eynollah:
if np.abs(slope_deskew) >= SLOPE_THRESHOLD:
_, _, matrix_of_lines_ch_d, splitter_y_new_d, _ = find_number_of_columns_in_document(np.repeat(text_regions_p_1_n[:, :, np.newaxis], 3, axis=2), num_col_classifier, self.tables, pixel_lines)
print(time.time()-t_0_box,'time box in 2')
#print(time.time()-t_0_box,'time box in 2')
self.logger.info("num_col_classifier: %s", num_col_classifier)
if num_col_classifier >= 3:
@ -3391,36 +3391,41 @@ class Eynollah:
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)
print(time.time()-t_0_box,'time box in 3')
#print(time.time()-t_0_box,'time box in 3')
t1 = time.time()
if np.abs(slope_deskew) < SLOPE_THRESHOLD:
boxes, peaks_neg_tot_tables = return_boxes_of_images_by_order_of_reading_new(splitter_y_new, regions_without_separators, matrix_of_lines_ch, num_col_classifier, erosion_hurts, self.tables, self.right2left)
boxes_d = None
self.logger.debug("len(boxes): %s", len(boxes))
#print(time.time()-t_0_box,'time box in 3.1')
text_regions_p_tables = np.copy(text_regions_p)
text_regions_p_tables[:,:][(table_prediction[:,:] == 1)] = 10
pixel_line = 3
img_revised_tab2 = self.add_tables_heuristic_to_layout(text_regions_p_tables, boxes, 0, splitter_y_new, peaks_neg_tot_tables, text_regions_p_tables , num_col_classifier , 0.000005, pixel_line)
img_revised_tab2, contoures_tables = self.check_iou_of_bounding_box_and_contour_for_tables(img_revised_tab2,table_prediction, 10, num_col_classifier)
if self.tables:
text_regions_p_tables = np.copy(text_regions_p)
text_regions_p_tables[:,:][(table_prediction[:,:] == 1)] = 10
pixel_line = 3
img_revised_tab2 = self.add_tables_heuristic_to_layout(text_regions_p_tables, boxes, 0, splitter_y_new, peaks_neg_tot_tables, text_regions_p_tables , num_col_classifier , 0.000005, pixel_line)
#print(time.time()-t_0_box,'time box in 3.2')
img_revised_tab2, contoures_tables = self.check_iou_of_bounding_box_and_contour_for_tables(img_revised_tab2,table_prediction, 10, num_col_classifier)
#print(time.time()-t_0_box,'time box in 3.3')
else:
boxes_d, peaks_neg_tot_tables_d = return_boxes_of_images_by_order_of_reading_new(splitter_y_new_d, regions_without_separators_d, matrix_of_lines_ch_d, num_col_classifier, erosion_hurts, self.tables, self.right2left)
boxes = None
self.logger.debug("len(boxes): %s", len(boxes_d))
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
if self.tables:
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
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, 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)
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, 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)
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])
print(time.time()-t_0_box,'time box in 4')
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])
#print(time.time()-t_0_box,'time box in 4')
self.logger.info("detecting boxes took %.1fs", time.time() - t1)
if self.tables:
@ -3452,7 +3457,7 @@ class Eynollah:
pixel_img = 10
contours_tables = return_contours_of_interested_region(text_regions_p, pixel_img, min_area_mar)
print(time.time()-t_0_box,'time box in 5')
#print(time.time()-t_0_box,'time box in 5')
self.logger.debug('exit run_boxes_no_full_layout')
return 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
@ -4742,16 +4747,16 @@ class Eynollah:
t0 = time.time()
if self.dir_in:
self.reset_file_name_dir(os.path.join(self.dir_in,img_name))
print("text region early -11 in %.1fs", time.time() - t0)
#print("text region early -11 in %.1fs", time.time() - t0)
img_res, is_image_enhanced, num_col_classifier, num_column_is_classified = self.run_enhancement(self.light_version)
self.logger.info("Enhancing took %.1fs ", time.time() - t0)
print("text region early -1 in %.1fs", time.time() - t0)
#print("text region early -1 in %.1fs", time.time() - t0)
t1 = time.time()
if not self.skip_layout_and_reading_order:
if self.light_version:
text_regions_p_1 ,erosion_hurts, polygons_lines_xml, textline_mask_tot_ea, img_bin_light = self.get_regions_light_v(img_res, is_image_enhanced, num_col_classifier)
print("text region early -2 in %.1fs", time.time() - t0)
#print("text region early -2 in %.1fs", time.time() - t0)
if num_col_classifier == 1 or num_col_classifier ==2:
if num_col_classifier == 1:
@ -4764,17 +4769,17 @@ class Eynollah:
textline_mask_tot_ea_deskew = resize_image(textline_mask_tot_ea,img_h_new, img_w_new )
slope_deskew, slope_first = 0, 0#self.run_deskew(textline_mask_tot_ea_deskew)
slope_deskew, slope_first = self.run_deskew(textline_mask_tot_ea_deskew)
else:
slope_deskew, slope_first = 0, 0#self.run_deskew(textline_mask_tot_ea)
print("text region early -2,5 in %.1fs", time.time() - t0)
slope_deskew, slope_first = self.run_deskew(textline_mask_tot_ea)
#print("text region early -2,5 in %.1fs", time.time() - t0)
#self.logger.info("Textregion detection took %.1fs ", time.time() - t1t)
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)
#self.logger.info("run graphics %.1fs ", time.time() - t1t)
print("text region early -3 in %.1fs", time.time() - t0)
#print("text region early -3 in %.1fs", time.time() - t0)
textline_mask_tot_ea_org = np.copy(textline_mask_tot_ea)
print("text region early -4 in %.1fs", time.time() - t0)
#print("text region early -4 in %.1fs", time.time() - t0)
else:
text_regions_p_1 ,erosion_hurts, polygons_lines_xml = self.get_regions_from_xy_2models(img_res, is_image_enhanced, num_col_classifier)
self.logger.info("Textregion detection took %.1fs ", time.time() - t1)
@ -4795,7 +4800,7 @@ class Eynollah:
continue
else:
return pcgts
print("text region early in %.1fs", time.time() - t0)
#print("text region early in %.1fs", time.time() - t0)
t1 = time.time()
if not self.light_version:
textline_mask_tot_ea = self.run_textline(image_page)
@ -4837,7 +4842,7 @@ class Eynollah:
image_page_rotated = resize_image(image_page_rotated,org_h_l_m, org_w_l_m )
self.logger.info("detection of marginals took %.1fs", time.time() - t1)
print("text region early 2 marginal in %.1fs", time.time() - t0)
#print("text region early 2 marginal in %.1fs", time.time() - t0)
## birdan sora chock chakir
t1 = time.time()
if not self.full_layout:
@ -4852,7 +4857,7 @@ class Eynollah:
if np.abs(slope_deskew) >= SLOPE_THRESHOLD:
text_only_d = ((text_regions_p_1_n[:, :] == 1)) * 1
print("text region early 2 in %.1fs", time.time() - t0)
#print("text region early 2 in %.1fs", time.time() - t0)
###min_con_area = 0.000005
if np.abs(slope_deskew) >= SLOPE_THRESHOLD:
contours_only_text, hir_on_text = return_contours_of_image(text_only)
@ -4974,19 +4979,20 @@ class Eynollah:
else:
pass
print("text region early 3 in %.1fs", time.time() - t0)
#print("text region early 3 in %.1fs", time.time() - t0)
if self.light_version:
contours_only_text_parent = self.dilate_textregions_contours(contours_only_text_parent)
contours_only_text_parent = self.filter_contours_inside_a_bigger_one(contours_only_text_parent, text_only, marginal_cnts=polygons_of_marginals)
#print("text region early 3.5 in %.1fs", time.time() - t0)
txt_con_org = get_textregion_contours_in_org_image_light(contours_only_text_parent, self.image, slope_first)
#txt_con_org = self.dilate_textregions_contours(txt_con_org)
#contours_only_text_parent = self.dilate_textregions_contours(contours_only_text_parent)
else:
txt_con_org = get_textregion_contours_in_org_image(contours_only_text_parent, self.image, slope_first)
print("text region early 4 in %.1fs", time.time() - t0)
#print("text region early 4 in %.1fs", time.time() - t0)
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)
#print("text region early 5 in %.1fs", time.time() - t0)
## birdan sora chock chakir
if not self.curved_line:
if self.light_version:
@ -5022,7 +5028,7 @@ class Eynollah:
all_found_textline_polygons = small_textlines_to_parent_adherence2(all_found_textline_polygons, textline_mask_tot_ea, num_col_classifier)
all_found_textline_polygons_marginals, boxes_marginals, _, polygons_of_marginals, all_box_coord_marginals, _, slopes_marginals = self.get_slopes_and_deskew_new_curved(polygons_of_marginals, polygons_of_marginals, cv2.erode(textline_mask_tot_ea, kernel=KERNEL, iterations=2), image_page_rotated, boxes_marginals, text_only, num_col_classifier, scale_param, slope_deskew)
all_found_textline_polygons_marginals = small_textlines_to_parent_adherence2(all_found_textline_polygons_marginals, textline_mask_tot_ea, num_col_classifier)
print("text region early 6 in %.1fs", time.time() - t0)
#print("text region early 6 in %.1fs", time.time() - t0)
if self.full_layout:
if np.abs(slope_deskew) >= SLOPE_THRESHOLD:
contours_only_text_parent_d_ordered = self.return_list_of_contours_with_desired_order(contours_only_text_parent_d_ordered, index_by_text_par_con)
@ -5182,7 +5188,7 @@ class Eynollah:
self.logger.info("Job done in %.1fs", time.time() - t0)
if not self.dir_in:
return pcgts
print("text region early 7 in %.1fs", time.time() - t0)
#print("text region early 7 in %.1fs", time.time() - t0)
else:
_ ,_, _, textline_mask_tot_ea, img_bin_light = self.get_regions_light_v(img_res, is_image_enhanced, num_col_classifier, skip_layout_and_reading_order=self.skip_layout_and_reading_order)

@ -7,7 +7,7 @@ import cv2
import imutils
from scipy.signal import find_peaks
from scipy.ndimage import gaussian_filter1d
import time
from .is_nan import isNaN
from .contour import (contours_in_same_horizon,
find_new_features_of_contours,
@ -1342,7 +1342,7 @@ def return_points_with_boundies(peaks_neg_fin, first_point, last_point):
return peaks_neg_tot
def find_number_of_columns_in_document(region_pre_p, num_col_classifier, tables, pixel_lines, contours_h=None):
t_ins_c0 = time.time()
separators_closeup=( (region_pre_p[:,:,:]==pixel_lines))*1
separators_closeup[0:110,:,:]=0
@ -1356,83 +1356,46 @@ def find_number_of_columns_in_document(region_pre_p, num_col_classifier, tables,
separators_closeup_new=np.zeros((separators_closeup.shape[0] ,separators_closeup.shape[1] ))
##_,separators_closeup_n=self.combine_hor_lines_and_delete_cross_points_and_get_lines_features_back(region_pre_p[:,:,0])
separators_closeup_n=np.copy(separators_closeup)
separators_closeup_n=separators_closeup_n.astype(np.uint8)
##plt.imshow(separators_closeup_n[:,:,0])
##plt.show()
separators_closeup_n_binary=np.zeros(( separators_closeup_n.shape[0],separators_closeup_n.shape[1]) )
separators_closeup_n_binary[:,:]=separators_closeup_n[:,:,0]
separators_closeup_n_binary[:,:][separators_closeup_n_binary[:,:]!=0]=1
#separators_closeup_n_binary[:,:][separators_closeup_n_binary[:,:]==0]=255
#separators_closeup_n_binary[:,:][separators_closeup_n_binary[:,:]==-255]=0
#separators_closeup_n_binary=(separators_closeup_n_binary[:,:]==2)*1
#gray = cv2.cvtColor(separators_closeup_n, cv2.COLOR_BGR2GRAY)
###
#print(separators_closeup_n_binary.shape)
gray_early=np.repeat(separators_closeup_n_binary[:, :, np.newaxis], 3, axis=2)
gray_early=gray_early.astype(np.uint8)
#print(gray_early.shape,'burda')
imgray_e = cv2.cvtColor(gray_early, cv2.COLOR_BGR2GRAY)
#print('burda2')
ret_e, thresh_e = cv2.threshold(imgray_e, 0, 255, 0)
#print('burda3')
contours_line_e,hierarchy_e=cv2.findContours(thresh_e,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
#slope_lines_e,dist_x_e, x_min_main_e ,x_max_main_e ,cy_main_e,slope_lines_org_e,y_min_main_e, y_max_main_e, cx_main_e=self.find_features_of_lines(contours_line_e)
slope_linese,dist_xe, x_min_maine ,x_max_maine ,cy_maine,slope_lines_orge,y_min_maine, y_max_maine, cx_maine=find_features_of_lines(contours_line_e)
dist_ye=y_max_maine-y_min_maine
#print(y_max_maine-y_min_maine,'y')
#print(dist_xe,'x')
args_e=np.array(range(len(contours_line_e)))
args_hor_e=args_e[(dist_ye<=50) & (dist_xe>=3*dist_ye)]
#print(args_hor_e,'jidi',len(args_hor_e),'jilva')
cnts_hor_e=[]
for ce in args_hor_e:
cnts_hor_e.append(contours_line_e[ce])
#print(len(slope_linese),'lieee')
figs_e=np.zeros(thresh_e.shape)
figs_e=cv2.fillPoly(figs_e,pts=cnts_hor_e,color=(1,1,1))
#plt.imshow(figs_e)
#plt.show()
###
separators_closeup_n_binary=cv2.fillPoly(separators_closeup_n_binary,pts=cnts_hor_e,color=(0,0,0))
gray = cv2.bitwise_not(separators_closeup_n_binary)
gray=gray.astype(np.uint8)
#plt.imshow(gray)
#plt.show()
bw = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, \
cv2.THRESH_BINARY, 15, -2)
##plt.imshow(bw[:,:])
##plt.show()
horizontal = np.copy(bw)
vertical = np.copy(bw)
@ -1451,16 +1414,7 @@ def find_number_of_columns_in_document(region_pre_p, num_col_classifier, tables,
horizontal = cv2.dilate(horizontal,kernel,iterations = 2)
horizontal = cv2.erode(horizontal,kernel,iterations = 2)
###
#print(np.unique(horizontal),'uni')
horizontal=cv2.fillPoly(horizontal,pts=cnts_hor_e,color=(255,255,255))
###
#plt.imshow(horizontal)
#plt.show()
rows = vertical.shape[0]
verticalsize = rows // 30
@ -1471,35 +1425,21 @@ def find_number_of_columns_in_document(region_pre_p, num_col_classifier, tables,
vertical = cv2.dilate(vertical, verticalStructure)
vertical = cv2.dilate(vertical,kernel,iterations = 1)
# Show extracted vertical lines
horizontal,special_separators=combine_hor_lines_and_delete_cross_points_and_get_lines_features_back_new(vertical,horizontal,num_col_classifier)
#plt.imshow(horizontal)
#plt.show()
#print(vertical.shape,np.unique(vertical),'verticalvertical')
separators_closeup_new[:,:][vertical[:,:]!=0]=1
separators_closeup_new[:,:][horizontal[:,:]!=0]=1
##plt.imshow(separators_closeup_new)
##plt.show()
##separators_closeup_n
vertical=np.repeat(vertical[:, :, np.newaxis], 3, axis=2)
vertical=vertical.astype(np.uint8)
##plt.plot(vertical[:,:,0].sum(axis=0))
##plt.show()
#plt.plot(vertical[:,:,0].sum(axis=1))
#plt.show()
imgray = cv2.cvtColor(vertical, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray, 0, 255, 0)
contours_line_vers,hierarchy=cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
slope_lines,dist_x, x_min_main ,x_max_main ,cy_main,slope_lines_org,y_min_main, y_max_main, cx_main=find_features_of_lines(contours_line_vers)
#print(slope_lines,'vertical')
args=np.array( range(len(slope_lines) ))
args_ver=args[slope_lines==1]
dist_x_ver=dist_x[slope_lines==1]
@ -1512,9 +1452,6 @@ def find_number_of_columns_in_document(region_pre_p, num_col_classifier, tables,
len_y=separators_closeup.shape[0]/3.0
#plt.imshow(horizontal)
#plt.show()
horizontal=np.repeat(horizontal[:, :, np.newaxis], 3, axis=2)
horizontal=horizontal.astype(np.uint8)
imgray = cv2.cvtColor(horizontal, cv2.COLOR_BGR2GRAY)
@ -1582,8 +1519,6 @@ def find_number_of_columns_in_document(region_pre_p, num_col_classifier, tables,
matrix_of_lines_ch[len(cy_main_hor):,9]=1
if contours_h is not None:
slope_lines_head,dist_x_head, x_min_main_head ,x_max_main_head ,cy_main_head,slope_lines_org_head,y_min_main_head, y_max_main_head, cx_main_head=find_features_of_lines(contours_h)
matrix_l_n=np.zeros((matrix_of_lines_ch.shape[0]+len(cy_main_head),matrix_of_lines_ch.shape[1]))
@ -1629,8 +1564,6 @@ def find_number_of_columns_in_document(region_pre_p, num_col_classifier, tables,
args_big_parts=np.array(range(len(splitter_y_new_diff))) [ splitter_y_new_diff>22 ]
regions_without_separators=return_regions_without_separators(region_pre_p)
@ -1640,19 +1573,8 @@ def find_number_of_columns_in_document(region_pre_p, num_col_classifier, tables,
peaks_neg_fin_fin=[]
for itiles in args_big_parts:
regions_without_separators_tile=regions_without_separators[int(splitter_y_new[itiles]):int(splitter_y_new[itiles+1]),:,0]
#image_page_background_zero_tile=image_page_background_zero[int(splitter_y_new[itiles]):int(splitter_y_new[itiles+1]),:]
#print(regions_without_separators_tile.shape)
##plt.imshow(regions_without_separators_tile)
##plt.show()
#num_col, peaks_neg_fin=self.find_num_col(regions_without_separators_tile,multiplier=6.0)
#regions_without_separators_tile=cv2.erode(regions_without_separators_tile,kernel,iterations = 3)
#
try:
num_col, peaks_neg_fin = find_num_col(regions_without_separators_tile, num_col_classifier, tables, multiplier=7.0)
except:
@ -1670,9 +1592,6 @@ def find_number_of_columns_in_document(region_pre_p, num_col_classifier, tables,
peaks_neg_fin=peaks_neg_fin[peaks_neg_fin<=(vertical.shape[1]-500)]
peaks_neg_fin_fin=peaks_neg_fin[:]
#print(peaks_neg_fin_fin,'peaks_neg_fin_fintaza')
return num_col_fin, peaks_neg_fin_fin,matrix_of_lines_ch,splitter_y_new,separators_closeup_n

@ -263,7 +263,7 @@ def get_textregion_contours_in_org_image(cnts, img, slope_first):
return cnts_org
def get_textregion_contours_in_org_image_light(cnts, img, slope_first):
def get_textregion_contours_in_org_image_light_old(cnts, img, slope_first):
h_o = img.shape[0]
w_o = img.shape[1]
@ -278,14 +278,7 @@ def get_textregion_contours_in_org_image_light(cnts, img, slope_first):
img_copy = np.zeros(img.shape)
img_copy = cv2.fillPoly(img_copy, pts=[cnts[i]], color=(1, 1, 1))
# plt.imshow(img_copy)
# plt.show()
# print(img.shape,'img')
img_copy = rotation_image_new(img_copy, -slope_first)
##print(img_copy.shape,'img_copy')
# plt.imshow(img_copy)
# plt.show()
img_copy = img_copy.astype(np.uint8)
imgray = cv2.cvtColor(img_copy, cv2.COLOR_BGR2GRAY)
@ -300,6 +293,70 @@ def get_textregion_contours_in_org_image_light(cnts, img, slope_first):
return cnts_org
def return_list_of_contours_with_desired_order(ls_cons, sorted_indexes):
return [ls_cons[sorted_indexes[index]] for index in range(len(sorted_indexes))]
def do_back_rotation_and_get_cnt_back(queue_of_all_params, contours_par_per_process,indexes_r_con_per_pro, img, slope_first):
contours_textregion_per_each_subprocess = []
index_by_text_region_contours = []
for mv in range(len(contours_par_per_process)):
img_copy = np.zeros(img.shape)
img_copy = cv2.fillPoly(img_copy, pts=[contours_par_per_process[mv]], color=(1, 1, 1))
img_copy = rotation_image_new(img_copy, -slope_first)
img_copy = img_copy.astype(np.uint8)
imgray = cv2.cvtColor(img_copy, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray, 0, 255, 0)
cont_int, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cont_int[0][:, 0, 0] = cont_int[0][:, 0, 0] + np.abs(img_copy.shape[1] - img.shape[1])
cont_int[0][:, 0, 1] = cont_int[0][:, 0, 1] + np.abs(img_copy.shape[0] - img.shape[0])
# print(np.shape(cont_int[0]))
contours_textregion_per_each_subprocess.append(cont_int[0]*6)
index_by_text_region_contours.append(indexes_r_con_per_pro[mv])
queue_of_all_params.put([contours_textregion_per_each_subprocess, index_by_text_region_contours])
def get_textregion_contours_in_org_image_light(cnts, img, slope_first):
num_cores = cpu_count()
queue_of_all_params = Queue()
processes = []
nh = np.linspace(0, len(cnts), num_cores + 1)
indexes_by_text_con = np.array(range(len(cnts)))
h_o = img.shape[0]
w_o = img.shape[1]
img = cv2.resize(img, (int(img.shape[1]/6.), int(img.shape[0]/6.)), interpolation=cv2.INTER_NEAREST)
##cnts = list( (np.array(cnts)/2).astype(np.int16) )
#cnts = cnts/2
cnts = [(i/ 6).astype(np.int32) for i in cnts]
for i in range(num_cores):
contours_par_per_process = cnts[int(nh[i]) : int(nh[i + 1])]
indexes_text_con_per_process = indexes_by_text_con[int(nh[i]) : int(nh[i + 1])]
processes.append(Process(target=do_back_rotation_and_get_cnt_back, args=(queue_of_all_params, contours_par_per_process, indexes_text_con_per_process, img, slope_first)))
for i in range(num_cores):
processes[i].start()
cnts_org = []
all_index_text_con = []
for i in range(num_cores):
list_all_par = queue_of_all_params.get(True)
contours_for_subprocess = list_all_par[0]
indexes_for_subprocess = list_all_par[1]
for j in range(len(contours_for_subprocess)):
cnts_org.append(contours_for_subprocess[j])
all_index_text_con.append(indexes_for_subprocess[j])
for i in range(num_cores):
processes[i].join()
cnts_org = return_list_of_contours_with_desired_order(cnts_org, all_index_text_con)
return cnts_org
def return_contours_of_interested_textline(region_pre_p, pixel):
# pixels of images are identified by 5

@ -3,7 +3,8 @@ import cv2
from scipy.signal import find_peaks
from scipy.ndimage import gaussian_filter1d
import os
from multiprocessing import Process, Queue, cpu_count
from multiprocessing import Pool
from .rotate import rotate_image
from .contour import (
return_parent_contours,
@ -1569,8 +1570,21 @@ def separate_lines_new2(img_path, thetha, num_col, slope_region, plotter=None):
# plt.show()
return img_patch_ineterst_revised
def return_deskew_slop(img_patch_org, sigma_des,n_tot_angles=100, main_page=False, plotter=None):
def do_image_rotation(queue_of_all_params,angels_per_process, img_resized, sigma_des):
angels_per_each_subprocess = []
for mv in range(len(angels_per_process)):
img_rot=rotate_image(img_resized,angels_per_process[mv])
img_rot[img_rot!=0]=1
try:
var_spectrum=find_num_col_deskew(img_rot,sigma_des,20.3 )
except:
var_spectrum=0
angels_per_each_subprocess.append(var_spectrum)
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):
num_cores = cpu_count()
if main_page and plotter:
plotter.save_plot_of_textline_density(img_patch_org)
@ -1604,21 +1618,43 @@ def return_deskew_slop(img_patch_org, sigma_des,n_tot_angles=100, main_page=Fals
#plt.show()
angels=np.array([-45, 0 , 45 , 90 , ])#np.linspace(-12,12,100)#np.array([0 , 45 , 90 , -45])
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)
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)
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]
@ -1628,17 +1664,38 @@ def return_deskew_slop(img_patch_org, sigma_des,n_tot_angles=100, main_page=Fals
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 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)
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]
@ -1652,22 +1709,44 @@ def return_deskew_slop(img_patch_org, sigma_des,n_tot_angles=100, main_page=Fals
angels=np.linspace(-12,12,n_tot_angles)#np.array([0 , 45 , 90 , -45])
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 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 )
for i in range(num_cores):
processes[i].join()
except:
var_spectrum=0
var_res.append(var_spectrum)
##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:
@ -1681,17 +1760,38 @@ def return_deskew_slop(img_patch_org, sigma_des,n_tot_angles=100, main_page=Fals
early_slope_edge=11
if 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 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)
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]
@ -1701,18 +1801,41 @@ def return_deskew_slop(img_patch_org, sigma_des,n_tot_angles=100, main_page=Fals
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 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)
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]
@ -1720,20 +1843,42 @@ def return_deskew_slop(img_patch_org, sigma_des,n_tot_angles=100, main_page=Fals
ang_int=0
else:
angels=np.linspace(-25,25,int(n_tot_angles/2.)+10)
var_res=[]
indexer=0
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)
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]
@ -1751,18 +1896,39 @@ def return_deskew_slop(img_patch_org, sigma_des,n_tot_angles=100, main_page=Fals
angels=np.linspace(-90,-25,int(n_tot_angles/2.)+10)
var_res=[]
queue_of_all_params = Queue()
processes = []
nh = np.linspace(0, len(angels), num_cores + 1)
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)
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)
@ -1773,22 +1939,44 @@ def return_deskew_slop(img_patch_org, sigma_des,n_tot_angles=100, main_page=Fals
elif abs(ang_int)>early_slope_edge and ang_int>0:
angels=np.linspace(90,25,int(n_tot_angles/2.)+10)
indexer=0
var_res=[]
queue_of_all_params = Queue()
processes = []
nh = np.linspace(0, len(angels), num_cores + 1)
indexer=0
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
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.append(var_spectrum)
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]

Loading…
Cancel
Save