Pull request #1: added box detection logic to find previous redactions

Merge in RR/table_parsing from box_detection to master

* commit 'ed0c38e32d7add1f194ad28d848aa8b85c2a1e58':
  added box detection logic to find previous redactions
This commit is contained in:
Matthias Bisping 2022-02-03 16:29:33 +01:00 committed by Julius Unverfehrt
commit 6c05f1bb45
5 changed files with 67 additions and 18 deletions

View File

View File

@ -0,0 +1,42 @@
from itertools import count
import cv2
import imutils
import numpy as np
import pdf2image
from matplotlib import pyplot as plt
def parse(image: np.array):
gray = ~cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 1)
thresh = cv2.threshold(blurred, 253, 255, cv2.THRESH_BINARY)[1]
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
for c in cnts:
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.04 * peri, True)
yield cv2.boundingRect(approx)
def annotate_boxes(image, rects):
for rect in rects:
(x, y, w, h) = rect
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
return image
def annotate_boxes_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)
asd = parse(page)
page = annotate_boxes(page, asd)
fig, ax = plt.subplots(1, 1)
fig.set_size_inches(20, 20)
ax.imshow(page)
plt.show()

View File

@ -2,3 +2,4 @@ opencv-python~=4.5.5.62
numpy~=1.22.1
pdf2image~=1.16.0
matplotlib~=3.5.1
imutils==0.5.4

24
scripts/annotate.py Normal file
View File

@ -0,0 +1,24 @@
import argparse
from table_parsing.table_parsig import annotate_tables_in_pdf
from box_detection.box_detection import annotate_boxes_in_pdf
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("pdf_path")
parser.add_argument("page_index", type=int)
parser.add_argument("--object", choices=["table", "box"], default="table")
args = parser.parse_args()
return args
if __name__ == "__main__":
args = parse_args()
if args.object == "table":
annotate_tables_in_pdf(args.pdf_path, page_index=args.page_index)
elif args.object == "box":
annotate_boxes_in_pdf(args.pdf_path, page_index=args.page_index)

View File

@ -1,18 +0,0 @@
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)