def mask_tables(page_imge, check=False): try: table_image = cv2.imread(page_imge, 0) page_img = cv2.imread(page_imge) table_image = clean_image(table_image) page_img = clean_image(page_img) image_width, image_height = table_image.shape[1], table_image.shape[0] if check: cv2.imwrite('bg_org.png', table_image) except Exception as e: log_error( "Service TableExtractor Error in loading background html image", app_context.application_context, e) return None, None # To do recognize and mask images berfor finding tables #table_image = mask_image(table_image, img_df, image_width, image_height, app_context.application_context, # margin=0, fill=255) #check = True if check: cv2.imwrite('bg_org_masked.png', table_image) try: tables = TableRepositories(table_image).response['response']['tables'] except Exception as e: log_error("Service TableExtractor Error in finding tables", app_context.application_context, e) return None, None try: rects = RectRepositories(table_image) lines, _ = rects.get_tables_and_lines() except Exception as e: log_error("Service TableExtractor Error in finding lines", app_context.application_context, e) return None, None #line_regions = get_regions(lines, 'LINE') tables_regions = get_regions(tables, 'TABLE') masked_image = mask_image(page_img, tables, image_width, image_height, app_context.application_context, margin=0, fill=255) ### add line regions #return masked_image, line_regions + tables_regions return masked_image, tables_regions
def detect_text_per_file(image_paths, network, text_threshold, low_text_threshold, link_threshold, img_class="single_col"): in_dfs = [] number_of_pages = len(image_paths) if img_class == "double_col": number_of_pages = 1 image_paths = [image_paths] t = time.time() for image_path in image_paths: image = cv2.imread(image_path) image = clean_image(image) # if img_class == "double_col": # image = image_path # else: # image = imgproc.loadImage(image_path) if network: bboxes, polys, score_text = test_net(image, text_threshold, link_threshold, low_text_threshold, args.cuda, args.poly, refine_net) else: bboxes, polys, score_text = test_net(image, text_threshold, link_threshold, low_text_threshold, args.cuda, args.poly, None) column_names = ["x1", "y1", "x4", "y4", "x2", "y2", "x3", "y3"] df = pd.DataFrame(columns=column_names) for index, box in enumerate(bboxes): #for index, box in enumerate(polys): poly = np.array(box).astype(np.int32).reshape((-1)) df.at[index, 'x1'] = int(poly[0]) df.at[index, 'y1'] = int(poly[1]) df.at[index, 'x2'] = int(poly[2]) df.at[index, 'y2'] = int(poly[3]) df.at[index, 'x3'] = int(poly[4]) df.at[index, 'y3'] = int(poly[5]) df.at[index, 'x4'] = int(poly[6]) df.at[index, 'y4'] = int(poly[7]) color = (255, 0, 0) thickness = 2 cv2.rectangle(image, (int(poly[0]), int(poly[1])), (int(poly[4]), int(poly[5])), color, thickness) #cv2.imwrite("/home/naresh/word_compare3/"+str(uuid.uuid4())+".jpg", image) #in_df = convert_to_in_df(df) in_dfs.append(df) time_taken = time.time() - t time_take_per_page = time_taken / number_of_pages message = 'Time taken for text detection is ' + str( time_taken) + '/' + str(number_of_pages) + 'time per page : ' + str( time_take_per_page) log_info(message, app_context.application_context) return in_dfs