cv-analysis-service/cv_analysis/layout_parsing.py
2022-04-26 16:01:57 +02:00

140 lines
4.1 KiB
Python

from itertools import compress
from itertools import starmap
from operator import __and__
import cv2
import numpy as np
from pdf2image import pdf2image
from cv_analysis.utils.display import show_mpl
from cv_analysis.utils.draw import draw_rectangles
from cv_analysis.utils.post_processing import remove_overlapping, remove_included, has_no_parent
from cv_analysis.utils.visual_logging import vizlogger
def is_likely_segment(rect, min_area=100):
return cv2.contourArea(rect, False) > min_area
def find_segments(image):
contours, hierarchies = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
mask1 = map(is_likely_segment, contours)
mask2 = map(has_no_parent, hierarchies[0])
mask = starmap(__and__, zip(mask1, mask2))
contours = compress(contours, mask)
rectangles = (cv2.boundingRect(c) for c in contours)
return rectangles
def parse_layout(image: np.array):
image = image.copy()
image_ = image.copy()
if len(image_.shape) > 2:
image_ = cv2.cvtColor(image_, cv2.COLOR_BGR2GRAY)
vizlogger.debug(image_, "layout01_start.png")
image_ = cv2.GaussianBlur(image_, (7, 7), 0)
vizlogger.debug(image_, "layout02_blur.png")
thresh = cv2.threshold(image_, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
vizlogger.debug(image_, "layout03_theshold.png")
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
vizlogger.debug(kernel, "layout04_kernel.png")
dilate = cv2.dilate(thresh, kernel, iterations=4)
vizlogger.debug(dilate, "layout05_dilate.png")
rects = list(find_segments(dilate))
# -> Run meta detection on the previous detections TODO: refactor
for rect in rects:
x, y, w, h = rect
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 0), -1)
cv2.rectangle(image, (x, y), (x + w, y + h), (255, 255, 255), 7)
vizlogger.debug(image, "layout06_rectangles.png")
_, image = cv2.threshold(image, 254, 255, cv2.THRESH_BINARY)
vizlogger.debug(image, "layout07_threshold.png")
image = ~image
vizlogger.debug(image, "layout08_inverse.png")
if len(image.shape) > 2:
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
vizlogger.debug(image, "layout09_convertcolor.png")
rects = find_segments(image)
# <- End of meta detection
rects = remove_included(rects)
rects = remove_overlapping(rects)
return list(rects)
def annotate_layout_in_pdf(pdf_path, page_index=1, show=False):
page = pdf2image.convert_from_path(pdf_path, first_page=page_index + 1, last_page=page_index + 1)[0]
page = np.array(page)
rects = parse_layout(page)
page = draw_rectangles(page, rects)
vizlogger.debug(page, "layout10_output.png")
if show:
show_mpl(page)
"""
def find_layout_boxes(image: np.array):
if len(image.shape) > 2:
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = cv2.GaussianBlur(image, (5, 5), 1)
image = cv2.threshold(image, 253, 255, cv2.THRESH_BINARY)[1]
img_bin = ~image
line_min_width = 10
kernel_h = np.ones((10, line_min_width), np.uint8)
kernel_v = np.ones((line_min_width, 10), 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
contours = cv2.findContours(img_bin_final, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = imutils.grab_contours(contours)
for c in contours:
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.04 * peri, True)
yield cv2.boundingRect(approx)
def annotate_layout_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_layout_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)
layout_boxes = find_layout_boxes(page)
page = annotate_layout_boxes(page, layout_boxes)
fig, ax = plt.subplots(1, 1)
fig.set_size_inches(20, 20)
ax.imshow(page)
plt.show()
"""