added first table parsing function: cell detection by applying horizontal and vertical kernels
This commit is contained in:
parent
0ec786f2b1
commit
622e2f4fd8
@ -1 +1,4 @@
|
||||
scikit-image
|
||||
opencv-python~=4.5.5.62
|
||||
numpy~=1.22.1
|
||||
pdf2image~=1.16.0
|
||||
matplotlib~=3.5.1
|
||||
18
scripts/annotate_table.py
Normal file
18
scripts/annotate_table.py
Normal file
@ -0,0 +1,18 @@
|
||||
import argparse
|
||||
|
||||
from table_parsing.table_parsig import annotate_tables_in_pdf
|
||||
|
||||
|
||||
def parse_args():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("pdf_path")
|
||||
parser.add_argument("page_index", type=int)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
return args
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
args = parse_args()
|
||||
annotate_tables_in_pdf(args.pdf_path, page_index=args.page_index)
|
||||
0
table_parsing/__init__.py
Normal file
0
table_parsing/__init__.py
Normal file
51
table_parsing/table_parsig.py
Normal file
51
table_parsing/table_parsig.py
Normal file
@ -0,0 +1,51 @@
|
||||
from itertools import count
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
import pdf2image
|
||||
from matplotlib import pyplot as plt
|
||||
|
||||
|
||||
def parse(image: np.array):
|
||||
|
||||
gray_scale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
||||
th1, img_bin = cv2.threshold(gray_scale, 150, 225, cv2.THRESH_BINARY)
|
||||
img_bin = ~img_bin
|
||||
|
||||
line_min_width = 4
|
||||
kernel_h = np.ones((1, line_min_width), np.uint8)
|
||||
kernel_v = np.ones((line_min_width, 1), np.uint8)
|
||||
|
||||
img_bin_h = cv2.morphologyEx(img_bin, cv2.MORPH_OPEN, kernel_h)
|
||||
img_bin_v = cv2.morphologyEx(img_bin, cv2.MORPH_OPEN, kernel_v)
|
||||
|
||||
img_bin_final = img_bin_h | img_bin_v
|
||||
|
||||
_, labels, stats, _ = cv2.connectedComponentsWithStats(~img_bin_final, connectivity=8, ltype=cv2.CV_32S)
|
||||
|
||||
return labels, stats
|
||||
|
||||
|
||||
def parse_tables_in_pdf(pages):
|
||||
return zip(map(parse, pages), count())
|
||||
|
||||
|
||||
def annotate_image(image, stats):
|
||||
for x, y, w, h, area in stats[2:]:
|
||||
cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 255), 2)
|
||||
|
||||
return image
|
||||
|
||||
|
||||
def annotate_tables_in_pdf(pdf_path, page_index=1):
|
||||
|
||||
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)
|
||||
|
||||
fig, ax = plt.subplots(1, 1)
|
||||
fig.set_size_inches(20, 20)
|
||||
ax.imshow(page)
|
||||
plt.show()
|
||||
Loading…
x
Reference in New Issue
Block a user