Merge in RR/vidocp from poly_to_rects_segmentation to master
Squashed commit of the following:
commit 3dffe067ef0bb4796eab22007eb6970b29f47822
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Sat Feb 5 16:10:28 2022 +0100
readme updated
commit 448517205259134a8427b48d86d0d5331b726487
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Sat Feb 5 16:09:35 2022 +0100
restructured dirs
commit 058c2971631c71d520b1a94ea75e249f9234ad87
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Sat Feb 5 15:57:08 2022 +0100
renaming
commit 4e64a3d07f1dad76775955639157ec7b60e6ad38
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Sat Feb 5 15:46:03 2022 +0100
readme updated
commit 728bedb13a2769b4652fd674ef26988efebcc7dc
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Sat Feb 5 15:33:42 2022 +0100
added DVC
commit e2d5594afd6683d8207007d3a85d178dd0a3e546
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Sat Feb 5 14:49:09 2022 +0100
renaming
64 lines
1.8 KiB
Python
64 lines
1.8 KiB
Python
from functools import partial
|
|
|
|
import cv2
|
|
import numpy as np
|
|
import pdf2image
|
|
from iteration_utilities import starfilter, first
|
|
from matplotlib import pyplot as plt
|
|
|
|
|
|
def is_filled(hierarchy):
|
|
# See https://stackoverflow.com/questions/60095520/how-to-distinguish-filled-circle-contour-and-unfilled-circle-contour-in-opencv
|
|
return hierarchy[3] <= 0 and hierarchy[2] == -1
|
|
|
|
|
|
def is_boxy(contour):
|
|
epsilon = 0.01 * cv2.arcLength(contour, True)
|
|
approx = cv2.approxPolyDP(contour, epsilon, True)
|
|
return len(approx) <= 10
|
|
|
|
|
|
def is_large_enough(contour, min_area):
|
|
return cv2.contourArea(contour, False) > min_area
|
|
|
|
|
|
def is_likely_redaction(contour, hierarchy, min_area):
|
|
return is_filled(hierarchy) and is_boxy(contour) and is_large_enough(contour, min_area)
|
|
|
|
|
|
def find_redactions(image: np.array, min_normalized_area=200000):
|
|
|
|
min_normalized_area /= 200 # Assumes 200 DPI PDF -> image conversion resolution
|
|
|
|
gray = ~cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
|
blurred = cv2.GaussianBlur(gray, (5, 5), 1)
|
|
thresh = cv2.threshold(blurred, 252, 255, cv2.THRESH_BINARY)[1]
|
|
|
|
contours, hierarchies = cv2.findContours(thresh.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
|
|
|
|
contours = map(
|
|
first, starfilter(partial(is_likely_redaction, min_area=min_normalized_area), zip(contours, hierarchies[0]))
|
|
)
|
|
return contours
|
|
|
|
|
|
def annotate_poly(image, contours):
|
|
for cont in contours:
|
|
cv2.drawContours(image, cont, -1, (0, 255, 0), 4)
|
|
|
|
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)
|
|
|
|
redaction_contours = find_redactions(page)
|
|
page = annotate_poly(page, redaction_contours)
|
|
|
|
fig, ax = plt.subplots(1, 1)
|
|
fig.set_size_inches(20, 20)
|
|
ax.imshow(page)
|
|
plt.show()
|