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: else:
prediction_bin = self.do_prediction(True, img_resized, self.model_bin, n_batch_inference=5) 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]
prediction_bin = (prediction_bin[:,:]==0)*1 prediction_bin = (prediction_bin[:,:]==0)*1
prediction_bin = prediction_bin*255 prediction_bin = prediction_bin*255
@ -2266,7 +2266,7 @@ class Eynollah:
else: else:
img_bin = np.copy(img_resized) 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_bin)
textline_mask_tot_ea = self.run_textline(img_resized, num_col_classifier) 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.imshwo(self.image_page_org_size)
#plt.show() #plt.show()
if not skip_layout_and_reading_order: 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 not self.dir_in:
if num_col_classifier == 1 or num_col_classifier >= 2: 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_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) ###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.imshow(prediction_regions_org[:,:,0])
#plt.show() #plt.show()
@ -2395,7 +2395,7 @@ class Eynollah:
#plt.imshow(textline_mask_tot_ea) #plt.imshow(textline_mask_tot_ea)
#plt.show() #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 return text_regions_p_true, erosion_hurts, polygons_lines_xml, textline_mask_tot_ea, img_bin
else: else:
img_bin = resize_image(img_bin,img_height_h, img_width_h ) img_bin = resize_image(img_bin,img_height_h, img_width_h )
@ -3368,7 +3368,7 @@ class Eynollah:
if self.tables: if self.tables:
regions_without_separators_d[table_prediction_n[:,:] == 1] = 1 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') #print(time.time()-t_0_box,'time box in 1')
if self.tables: if self.tables:
regions_without_separators[table_prediction ==1 ] = 1 regions_without_separators[table_prediction ==1 ] = 1
if np.abs(slope_deskew) < SLOPE_THRESHOLD: if np.abs(slope_deskew) < SLOPE_THRESHOLD:
@ -3381,7 +3381,7 @@ class Eynollah:
if np.abs(slope_deskew) >= SLOPE_THRESHOLD: 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) _, _, 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) self.logger.info("num_col_classifier: %s", num_col_classifier)
if num_col_classifier >= 3: if num_col_classifier >= 3:
@ -3391,36 +3391,41 @@ class Eynollah:
else: else:
regions_without_separators_d = regions_without_separators_d.astype(np.uint8) 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)
print(time.time()-t_0_box,'time box in 3') #print(time.time()-t_0_box,'time box in 3')
t1 = time.time() t1 = time.time()
if np.abs(slope_deskew) < SLOPE_THRESHOLD: 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, 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 boxes_d = None
self.logger.debug("len(boxes): %s", len(boxes)) 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) if self.tables:
text_regions_p_tables[:,:][(table_prediction[:,:] == 1)] = 10 text_regions_p_tables = np.copy(text_regions_p)
pixel_line = 3 text_regions_p_tables[:,:][(table_prediction[:,:] == 1)] = 10
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) pixel_line = 3
img_revised_tab2, contoures_tables = self.check_iou_of_bounding_box_and_contour_for_tables(img_revised_tab2,table_prediction, 10, num_col_classifier) 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: 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_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 boxes = None
self.logger.debug("len(boxes): %s", len(boxes_d)) self.logger.debug("len(boxes): %s", len(boxes_d))
text_regions_p_tables = np.copy(text_regions_p_1_n) if self.tables:
text_regions_p_tables =np.round(text_regions_p_tables) text_regions_p_tables = np.copy(text_regions_p_1_n)
text_regions_p_tables[:,:][(text_regions_p_tables[:,:] != 3) & (table_prediction_n[:,:] == 1)] = 10 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 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 = 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,_ = 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 = 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 = 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 = 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') #print(time.time()-t_0_box,'time box in 4')
self.logger.info("detecting boxes took %.1fs", time.time() - t1) self.logger.info("detecting boxes took %.1fs", time.time() - t1)
if self.tables: if self.tables:
@ -3452,7 +3457,7 @@ class Eynollah:
pixel_img = 10 pixel_img = 10
contours_tables = return_contours_of_interested_region(text_regions_p, pixel_img, min_area_mar) 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') 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 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() t0 = time.time()
if self.dir_in: if self.dir_in:
self.reset_file_name_dir(os.path.join(self.dir_in,img_name)) 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) 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) 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() t1 = time.time()
if not self.skip_layout_and_reading_order: if not self.skip_layout_and_reading_order:
if self.light_version: 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) 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 or num_col_classifier ==2:
if num_col_classifier == 1: 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 ) 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: else:
slope_deskew, slope_first = 0, 0#self.run_deskew(textline_mask_tot_ea) slope_deskew, slope_first = self.run_deskew(textline_mask_tot_ea)
print("text region early -2,5 in %.1fs", time.time() - t0) #print("text region early -2,5 in %.1fs", time.time() - t0)
#self.logger.info("Textregion detection took %.1fs ", time.time() - t1t) #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 = \ 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.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) #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) 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: else:
text_regions_p_1 ,erosion_hurts, polygons_lines_xml = self.get_regions_from_xy_2models(img_res, is_image_enhanced, num_col_classifier) 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) self.logger.info("Textregion detection took %.1fs ", time.time() - t1)
@ -4795,7 +4800,7 @@ class Eynollah:
continue continue
else: else:
return pcgts return pcgts
print("text region early in %.1fs", time.time() - t0) #print("text region early in %.1fs", time.time() - t0)
t1 = time.time() t1 = time.time()
if not self.light_version: if not self.light_version:
textline_mask_tot_ea = self.run_textline(image_page) 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 ) 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) 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 ## birdan sora chock chakir
t1 = time.time() t1 = time.time()
if not self.full_layout: if not self.full_layout:
@ -4852,7 +4857,7 @@ class Eynollah:
if np.abs(slope_deskew) >= SLOPE_THRESHOLD: if np.abs(slope_deskew) >= SLOPE_THRESHOLD:
text_only_d = ((text_regions_p_1_n[:, :] == 1)) * 1 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 ###min_con_area = 0.000005
if np.abs(slope_deskew) >= SLOPE_THRESHOLD: if np.abs(slope_deskew) >= SLOPE_THRESHOLD:
contours_only_text, hir_on_text = return_contours_of_image(text_only) contours_only_text, hir_on_text = return_contours_of_image(text_only)
@ -4974,19 +4979,20 @@ class Eynollah:
else: else:
pass 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: if self.light_version:
contours_only_text_parent = self.dilate_textregions_contours(contours_only_text_parent) 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) 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 = 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) #txt_con_org = self.dilate_textregions_contours(txt_con_org)
#contours_only_text_parent = self.dilate_textregions_contours(contours_only_text_parent) #contours_only_text_parent = self.dilate_textregions_contours(contours_only_text_parent)
else: else:
txt_con_org = get_textregion_contours_in_org_image(contours_only_text_parent, self.image, slope_first) 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_text, _ = get_text_region_boxes_by_given_contours(contours_only_text_parent)
boxes_marginals, _ = get_text_region_boxes_by_given_contours(polygons_of_marginals) 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 ## birdan sora chock chakir
if not self.curved_line: if not self.curved_line:
if self.light_version: 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 = 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, 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) 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 self.full_layout:
if np.abs(slope_deskew) >= SLOPE_THRESHOLD: 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) 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) self.logger.info("Job done in %.1fs", time.time() - t0)
if not self.dir_in: if not self.dir_in:
return pcgts return pcgts
print("text region early 7 in %.1fs", time.time() - t0) #print("text region early 7 in %.1fs", time.time() - t0)
else: 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) _ ,_, _, 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 import imutils
from scipy.signal import find_peaks from scipy.signal import find_peaks
from scipy.ndimage import gaussian_filter1d from scipy.ndimage import gaussian_filter1d
import time
from .is_nan import isNaN from .is_nan import isNaN
from .contour import (contours_in_same_horizon, from .contour import (contours_in_same_horizon,
find_new_features_of_contours, 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 return peaks_neg_tot
def find_number_of_columns_in_document(region_pre_p, num_col_classifier, tables, pixel_lines, contours_h=None): 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=( (region_pre_p[:,:,:]==pixel_lines))*1
separators_closeup[0:110,:,:]=0 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_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=np.copy(separators_closeup)
separators_closeup_n=separators_closeup_n.astype(np.uint8) 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=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[:,:,0]
separators_closeup_n_binary[:,:][separators_closeup_n_binary[:,:]!=0]=1 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=np.repeat(separators_closeup_n_binary[:, :, np.newaxis], 3, axis=2)
gray_early=gray_early.astype(np.uint8) gray_early=gray_early.astype(np.uint8)
#print(gray_early.shape,'burda')
imgray_e = cv2.cvtColor(gray_early, cv2.COLOR_BGR2GRAY) imgray_e = cv2.cvtColor(gray_early, cv2.COLOR_BGR2GRAY)
#print('burda2')
ret_e, thresh_e = cv2.threshold(imgray_e, 0, 255, 0) 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) 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) 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 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_e=np.array(range(len(contours_line_e)))
args_hor_e=args_e[(dist_ye<=50) & (dist_xe>=3*dist_ye)] 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=[] cnts_hor_e=[]
for ce in args_hor_e: for ce in args_hor_e:
cnts_hor_e.append(contours_line_e[ce]) cnts_hor_e.append(contours_line_e[ce])
#print(len(slope_linese),'lieee')
figs_e=np.zeros(thresh_e.shape) figs_e=np.zeros(thresh_e.shape)
figs_e=cv2.fillPoly(figs_e,pts=cnts_hor_e,color=(1,1,1)) 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)) 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 = cv2.bitwise_not(separators_closeup_n_binary)
gray=gray.astype(np.uint8) gray=gray.astype(np.uint8)
#plt.imshow(gray)
#plt.show()
bw = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, \ bw = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, \
cv2.THRESH_BINARY, 15, -2) cv2.THRESH_BINARY, 15, -2)
##plt.imshow(bw[:,:])
##plt.show()
horizontal = np.copy(bw) horizontal = np.copy(bw)
vertical = 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.dilate(horizontal,kernel,iterations = 2)
horizontal = cv2.erode(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)) horizontal=cv2.fillPoly(horizontal,pts=cnts_hor_e,color=(255,255,255))
###
#plt.imshow(horizontal)
#plt.show()
rows = vertical.shape[0] rows = vertical.shape[0]
verticalsize = rows // 30 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, verticalStructure)
vertical = cv2.dilate(vertical,kernel,iterations = 1) 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) 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[:,:][vertical[:,:]!=0]=1
separators_closeup_new[:,:][horizontal[:,:]!=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=np.repeat(vertical[:, :, np.newaxis], 3, axis=2)
vertical=vertical.astype(np.uint8) 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) imgray = cv2.cvtColor(vertical, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray, 0, 255, 0) ret, thresh = cv2.threshold(imgray, 0, 255, 0)
contours_line_vers,hierarchy=cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) 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) 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=np.array( range(len(slope_lines) ))
args_ver=args[slope_lines==1] args_ver=args[slope_lines==1]
dist_x_ver=dist_x[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 len_y=separators_closeup.shape[0]/3.0
#plt.imshow(horizontal)
#plt.show()
horizontal=np.repeat(horizontal[:, :, np.newaxis], 3, axis=2) horizontal=np.repeat(horizontal[:, :, np.newaxis], 3, axis=2)
horizontal=horizontal.astype(np.uint8) horizontal=horizontal.astype(np.uint8)
imgray = cv2.cvtColor(horizontal, cv2.COLOR_BGR2GRAY) 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 matrix_of_lines_ch[len(cy_main_hor):,9]=1
if contours_h is not None: 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) 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])) 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 ] 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) 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=[] peaks_neg_fin_fin=[]
for itiles in args_big_parts: 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] 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: try:
num_col, peaks_neg_fin = find_num_col(regions_without_separators_tile, num_col_classifier, tables, multiplier=7.0) num_col, peaks_neg_fin = find_num_col(regions_without_separators_tile, num_col_classifier, tables, multiplier=7.0)
except: 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=peaks_neg_fin[peaks_neg_fin<=(vertical.shape[1]-500)]
peaks_neg_fin_fin=peaks_neg_fin[:] 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 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 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] h_o = img.shape[0]
w_o = img.shape[1] 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 = np.zeros(img.shape)
img_copy = cv2.fillPoly(img_copy, pts=[cnts[i]], color=(1, 1, 1)) 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) 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) img_copy = img_copy.astype(np.uint8)
imgray = cv2.cvtColor(img_copy, cv2.COLOR_BGR2GRAY) 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 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): def return_contours_of_interested_textline(region_pre_p, pixel):
# pixels of images are identified by 5 # pixels of images are identified by 5

@ -3,7 +3,8 @@ import cv2
from scipy.signal import find_peaks from scipy.signal import find_peaks
from scipy.ndimage import gaussian_filter1d from scipy.ndimage import gaussian_filter1d
import os import os
from multiprocessing import Process, Queue, cpu_count
from multiprocessing import Pool
from .rotate import rotate_image from .rotate import rotate_image
from .contour import ( from .contour import (
return_parent_contours, return_parent_contours,
@ -1569,8 +1570,21 @@ 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 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: if main_page and plotter:
plotter.save_plot_of_textline_density(img_patch_org) 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() #plt.show()
angels=np.array([-45, 0 , 45 , 90 , ])#np.linspace(-12,12,100)#np.array([0 , 45 , 90 , -45]) 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=[] 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: try:
var_res=np.array(var_res) 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] 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) 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=[] var_res=[]
for rot in angels: for i in range(num_cores):
img_rot=rotate_image(img_resized,rot) list_all_par = queue_of_all_params.get(True)
##plt.imshow(img_rot) angles_for_subprocess = list_all_par[0]
##plt.show() for j in range(len(angles_for_subprocess)):
img_rot[img_rot!=0]=1 var_res.append(angles_for_subprocess[j])
try:
var_spectrum=find_num_col_deskew(img_rot,sigma_des,20.3 ) for i in range(num_cores):
except: processes[i].join()
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
##try:
##var_spectrum=find_num_col_deskew(img_rot,sigma_des,20.3 )
##except:
##var_spectrum=0
##var_res.append(var_spectrum)
try: try:
var_res=np.array(var_res) 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] 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]) 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=[] 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: for i in range(num_cores):
img_rot=rotate_image(img_resized,rot) processes[i].join()
#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) ##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: 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 early_slope_edge=11
if abs(ang_int)>early_slope_edge and ang_int<0: if abs(ang_int)>early_slope_edge and ang_int<0:
angels=np.linspace(-90,-12,n_tot_angles) 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=[] var_res=[]
for rot in angels: for i in range(num_cores):
img_rot=rotate_image(img_resized,rot) list_all_par = queue_of_all_params.get(True)
##plt.imshow(img_rot) angles_for_subprocess = list_all_par[0]
##plt.show() for j in range(len(angles_for_subprocess)):
img_rot[img_rot!=0]=1 var_res.append(angles_for_subprocess[j])
try:
var_spectrum=find_num_col_deskew(img_rot,sigma_des,20.3 ) for i in range(num_cores):
except: processes[i].join()
var_spectrum=0 ##var_res=[]
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
##try:
##var_spectrum=find_num_col_deskew(img_rot,sigma_des,20.3 )
##except:
##var_spectrum=0
##var_res.append(var_spectrum)
try: try:
var_res=np.array(var_res) 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] 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: elif abs(ang_int)>early_slope_edge and ang_int>0:
angels=np.linspace(90,12,n_tot_angles) 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=[] var_res=[]
for rot in angels: for i in range(num_cores):
img_rot=rotate_image(img_resized,rot) list_all_par = queue_of_all_params.get(True)
##plt.imshow(img_rot) angles_for_subprocess = list_all_par[0]
##plt.show() for j in range(len(angles_for_subprocess)):
img_rot[img_rot!=0]=1 var_res.append(angles_for_subprocess[j])
try:
var_spectrum=find_num_col_deskew(img_rot,sigma_des,20.3 ) for i in range(num_cores):
#print(indexer,'indexer') 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
###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: try:
var_res=np.array(var_res) 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] 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 ang_int=0
else: else:
angels=np.linspace(-25,25,int(n_tot_angles/2.)+10) angels=np.linspace(-25,25,int(n_tot_angles/2.)+10)
var_res=[]
indexer=0 indexer=0
for rot in angels:
img_rot=rotate_image(img_resized,rot) queue_of_all_params = Queue()
#plt.imshow(img_rot) processes = []
#plt.show() nh = np.linspace(0, len(angels), num_cores + 1)
img_rot[img_rot!=0]=1
#neg_peaks,var_spectrum=self.find_num_col_deskew(img_rot,sigma_des,20.3 ) for i in range(num_cores):
#print(var_spectrum,'var_spectrum') angels_per_process = angels[int(nh[i]) : int(nh[i + 1])]
try: processes.append(Process(target=do_image_rotation, args=(queue_of_all_params, angels_per_process, img_resized, sigma_des)))
var_spectrum=find_num_col_deskew(img_rot,sigma_des,20.3 )
except: for i in range(num_cores):
var_spectrum=0 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
#####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: try:
var_res=np.array(var_res) 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] 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) 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: for i in range(num_cores):
img_rot=rotate_image(img_resized,rot) angels_per_process = angels[int(nh[i]) : int(nh[i + 1])]
##plt.imshow(img_rot) processes.append(Process(target=do_image_rotation, args=(queue_of_all_params, angels_per_process, img_resized, sigma_des)))
##plt.show()
img_rot[img_rot!=0]=1 for i in range(num_cores):
try: processes[i].start()
var_spectrum=find_num_col_deskew(img_rot,sigma_des,20.3 )
except: var_res=[]
var_spectrum=0 for i in range(num_cores):
var_res.append(var_spectrum) 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: try:
var_res=np.array(var_res) 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: elif abs(ang_int)>early_slope_edge and ang_int>0:
angels=np.linspace(90,25,int(n_tot_angles/2.)+10) 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 i in range(num_cores):
for rot in angels: angels_per_process = angels[int(nh[i]) : int(nh[i + 1])]
img_rot=rotate_image(img_resized,rot) processes.append(Process(target=do_image_rotation, args=(queue_of_all_params, angels_per_process, img_resized, sigma_des)))
##plt.imshow(img_rot)
##plt.show() for i in range(num_cores):
img_rot[img_rot!=0]=1 processes[i].start()
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) 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: try:
var_res=np.array(var_res) 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] 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