got changes to table parsing from other branch
This commit is contained in:
parent
4ac1cce0e8
commit
41e5f55ea7
@ -11,12 +11,15 @@ from cv_analysis.utils.display import show_mpl
|
||||
from cv_analysis.utils.draw import draw_rectangles
|
||||
from cv_analysis.utils.post_processing import xywh_to_vecs, xywh_to_vec_rect, adjacent1d
|
||||
from cv_analysis.utils.deskew import deskew_histbased
|
||||
from cv_analysis.utils.filters import is_large_enough
|
||||
from cv_analysis.utils.visual_logging import vizlogger
|
||||
from cv_analysis.layout_parsing import parse_layout
|
||||
|
||||
|
||||
def add_external_contours(image, img):
|
||||
contours, _ = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
|
||||
def add_external_contours(image, contour_source_image):
|
||||
contours, _ = cv2.findContours(contour_source_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
|
||||
contours = filter(partial(is_large_enough, min_area=5000), contours)
|
||||
|
||||
for cnt in contours:
|
||||
x, y, w, h = cv2.boundingRect(cnt)
|
||||
cv2.rectangle(image, (x, y), (x + w, y + h), 255, 1)
|
||||
@ -24,6 +27,16 @@ def add_external_contours(image, img):
|
||||
return image
|
||||
|
||||
|
||||
def extend_lines():
|
||||
#TODO
|
||||
pass
|
||||
|
||||
|
||||
def make_table_block_mask():
|
||||
#TODO
|
||||
pass
|
||||
|
||||
|
||||
def apply_motion_blur(image: np.array, angle, size=80):
|
||||
"""Solidifies and slightly extends detected lines.
|
||||
|
||||
@ -48,7 +61,7 @@ def apply_motion_blur(image: np.array, angle, size=80):
|
||||
return blurred
|
||||
|
||||
|
||||
def isolate_vertical_and_horizontal_components(img_bin, bounding_rects):
|
||||
def isolate_vertical_and_horizontal_components(img_bin):
|
||||
"""Identifies and reinforces horizontal and vertical lines in a binary image.
|
||||
|
||||
Args:
|
||||
@ -65,19 +78,20 @@ def isolate_vertical_and_horizontal_components(img_bin, bounding_rects):
|
||||
img_bin_h = cv2.morphologyEx(img_bin, cv2.MORPH_OPEN, kernel_h)
|
||||
vizlogger.debug(img_bin_h, "tables01_isolate01_img_bin_h.png")
|
||||
img_bin_v = cv2.morphologyEx(img_bin, cv2.MORPH_OPEN, kernel_v)
|
||||
vizlogger.debug(img_bin_v, "tables02_isolate02_img_bin_v.png")
|
||||
|
||||
img_lines_raw = img_bin_v | img_bin_h
|
||||
vizlogger.debug(img_lines_raw, "tables02_isolate02_img_bin_v.png")
|
||||
|
||||
kernel_h = np.ones((1, 30), np.uint8)
|
||||
kernel_v = np.ones((30, 1), np.uint8)
|
||||
img_bin_h = cv2.dilate(img_bin_h, kernel_h, iterations=2)
|
||||
vizlogger.debug(img_bin_h, "tables03_isolate03_dilate_h.png")
|
||||
img_bin_v = cv2.dilate(img_bin_v, kernel_v, iterations=2)
|
||||
vizlogger.debug(img_bin_v, "tables04_isolate04_dilate_v.png")
|
||||
vizlogger.debug(img_bin_v | img_bin_h, "tables04_isolate04_dilate_v.png")
|
||||
|
||||
img_bin_h = apply_motion_blur(img_bin_h, 0)
|
||||
vizlogger.debug(img_bin_h, "tables09_isolate05_blur_h.png")
|
||||
img_bin_v = apply_motion_blur(img_bin_v, 90)
|
||||
vizlogger.debug(img_bin_v, "tables10_isolate06_blur_v.png")
|
||||
vizlogger.debug(img_bin_v | img_bin_h, "tables10_isolate06_blur_v.png")
|
||||
|
||||
img_bin_final = img_bin_h | img_bin_v
|
||||
vizlogger.debug(img_bin_final, "tables11_isolate07_final.png")
|
||||
@ -86,20 +100,14 @@ def isolate_vertical_and_horizontal_components(img_bin, bounding_rects):
|
||||
vizlogger.debug(img_bin_final, "tables10_isolate12_threshold.png")
|
||||
img_bin_final = cv2.dilate(img_bin_final, np.ones((1, 1), np.uint8), iterations=1)
|
||||
vizlogger.debug(img_bin_final, "tables11_isolate13_dilate.png")
|
||||
|
||||
img_bin_final = disconnect_non_existing_cells(img_bin_final, bounding_rects)
|
||||
vizlogger.debug(img_bin_final, "tables12_isolate14_disconnect.png")
|
||||
|
||||
# add contours before lines are extended by blurring
|
||||
img_bin_final = add_external_contours(img_bin_final, img_lines_raw)
|
||||
vizlogger.debug(img_bin_final, "tables11_isolate14_contours_added.png")
|
||||
|
||||
return img_bin_final
|
||||
|
||||
|
||||
def disconnect_non_existing_cells(img_bin, bounding_rects):
|
||||
for rect in bounding_rects:
|
||||
x, y, w, h = rect
|
||||
img_bin = cv2.rectangle(img_bin, (x, y), (x + w, y + h), (0, 0, 0), 5)
|
||||
return img_bin
|
||||
|
||||
|
||||
def has_table_shape(rects):
|
||||
assert isinstance(rects, list)
|
||||
|
||||
@ -156,7 +164,10 @@ def parse_table(image: np.array, show=False):
|
||||
image = preprocess(image)
|
||||
|
||||
table_layout_boxes = find_table_layout_boxes(image)
|
||||
image = isolate_vertical_and_horizontal_components(image, table_layout_boxes)
|
||||
|
||||
image = isolate_vertical_and_horizontal_components(image)
|
||||
#image = add_external_contours(image, image)
|
||||
#vizlogger.debug(image, "external_contours_added.png")
|
||||
|
||||
_, _, stats, _ = cv2.connectedComponentsWithStats(~image, connectivity=8, ltype=cv2.CV_32S)
|
||||
|
||||
@ -177,7 +188,13 @@ def annotate_tables_in_pdf(pdf_path, page_index=0, deskew=False, show=False):
|
||||
|
||||
stats = parse_table(page)
|
||||
page = draw_rectangles(page, stats, annotate=True)
|
||||
|
||||
if show:
|
||||
show_mpl(page)
|
||||
vizlogger.debug(page, "tables15_final_output.png")
|
||||
|
||||
|
||||
def tables_in_image(cropped_image):
|
||||
table_rects = parse_table(cropped_image)
|
||||
|
||||
if len(table_rects) > 0:
|
||||
return True, table_rects
|
||||
else:
|
||||
return False, None
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user