From cf29204a9e50200064ec235016aa97f7ce4847a1 Mon Sep 17 00:00:00 2001 From: llocarnini Date: Wed, 2 Feb 2022 16:35:35 +0100 Subject: [PATCH] corrected function for detecting external edges --- table_parsing/table_parsig.py | 47 +++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/table_parsing/table_parsig.py b/table_parsing/table_parsig.py index 73a01bb..1efe198 100644 --- a/table_parsing/table_parsig.py +++ b/table_parsing/table_parsig.py @@ -18,7 +18,7 @@ def parse(image: np.array): img_bin_h = cv2.morphologyEx(img_bin, cv2.MORPH_OPEN, kernel_h) img_bin_v = cv2.morphologyEx(img_bin, cv2.MORPH_OPEN, kernel_v) - # print([cv2.countNonZero(row) for row in img_bin_v]) + #print(np.nonzero(img_bin_v)) img_bin_final = img_bin_h | img_bin_v @@ -79,43 +79,70 @@ def annotate_image(image, stats): return image +# def find_and_close_edges(img_bin_final): +# contours, hierarchy = cv2.findContours(img_bin_final, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) +# +# for cnt in contours: +# missing_external_edges = True +# left = tuple(cnt[cnt[:, :, 0].argmin()][0]) +# right = tuple(cnt[cnt[:, :, 0].argmax()][0]) +# top = tuple(cnt[cnt[:, :, 1].argmin()][0]) +# bottom = tuple(cnt[cnt[:, :, 1].argmax()][0]) +# topleft = [left[0] + 1, top[1]] +# # print(cnt, left, top, topleft) +# bottomright = [right[0] - 1, bottom[1]] +# for arr in cnt: +# if np.array_equal(arr, np.array([bottomright])) or np.array_equal(arr, np.array([topleft])): +# missing_external_edges = False +# break +# +# if missing_external_edges and (bottomright[0]-topleft[0])*(bottomright[1]-topleft[1]) >= 50000: +# topleft[0] -= 1 +# bottomright[0] += 1 +# cv2.rectangle(img_bin_final, tuple(topleft), tuple(bottomright), (255,255,255) , 2) +# #print("missing cell detectet rectangle drawn") +# +# return img_bin_final + def find_and_close_edges(img_bin_final): - contours, hierarchy = cv2.findContours(img_bin_final, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) + contours, hierarchy = cv2.findContours(img_bin_final, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) + for cnt in contours: missing_external_edges = True left = tuple(cnt[cnt[:, :, 0].argmin()][0]) right = tuple(cnt[cnt[:, :, 0].argmax()][0]) top = tuple(cnt[cnt[:, :, 1].argmin()][0]) bottom = tuple(cnt[cnt[:, :, 1].argmax()][0]) - topleft = [left[0] + 1, top[1]] + topleft = [left[0], top[1]] # print(cnt, left, top, topleft) - bottomright = [right[0] - 1, bottom[1]] + bottomright = [right[0], bottom[1]] for arr in cnt: if np.array_equal(arr, np.array([bottomright])) or np.array_equal(arr, np.array([topleft])): missing_external_edges = False break - if missing_external_edges and (bottomright[0]-topleft[0])*(bottomright[1]-topleft[1])>= 50000: - topleft[0] -= 1 - bottomright[0] += 1 + if missing_external_edges and (bottomright[0]-topleft[0])*(bottomright[1]-topleft[1]) >= 50000: cv2.rectangle(img_bin_final, tuple(topleft), tuple(bottomright), (255,255,255) , 2) - print("missing cell detectet rectangle drawn") + #print("missing cell detectet rectangle drawn") return img_bin_final +def find_and_close_internal_gaps(img_bin_final): + contours, hierarchy = cv2.findContours(img_bin_final, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) + def parse_tables_in_pdf(pages): return zip(map(parse, pages), count()) def annotate_tables_in_pdf(pdf_path, page_index=1): - timeit() + #timeit() page = pdf2image.convert_from_path(pdf_path, first_page=page_index + 1, last_page=page_index + 1)[0] page = np.array(page) _, stats = parse(page) page = annotate_image(page, stats) - print(timeit()) + #print(timeit()) fig, ax = plt.subplots(1, 1) fig.set_size_inches(20, 20) ax.imshow(page)