Merge in RR/cv-analysis from add_table_parsing_fixtures to master
Squashed commit of the following:
commit cfc89b421b61082c8e92e1971c9d0bf4490fa07e
Merge: a7ecb05 73c66a8
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Mon Jul 11 12:19:01 2022 +0200
Merge branch 'master' of ssh://git.iqser.com:2222/rr/cv-analysis into add_table_parsing_fixtures
commit a7ecb05b7d8327f0c7429180f63a380b61b06bc3
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Mon Jul 11 12:02:07 2022 +0200
refactor
commit 466f217e5a9ee5c54fd38c6acd28d54fc38ff9bb
Author: llocarnini <lillian.locarnini@iqser.com>
Date: Mon Jul 11 10:24:14 2022 +0200
deleted unused imports and unused lines of code
commit c58955c8658d0631cdd1c24c8556d399e3fd9990
Author: llocarnini <lillian.locarnini@iqser.com>
Date: Mon Jul 11 10:16:01 2022 +0200
black reformatted files
commit f8bcb10a00ff7f0da49b80c1609b17997411985a
Author: llocarnini <lillian.locarnini@iqser.com>
Date: Tue Jul 5 15:15:00 2022 +0200
reformat files
commit 432e8a569fd70bd0745ce0549c2bfd2f2e907763
Author: llocarnini <lillian.locarnini@iqser.com>
Date: Tue Jul 5 15:08:22 2022 +0200
added better test for generic pages with table WIP as thicker lines create inconsistent results.
added test for patchy tables which does not work yet
commit 2aac9ebf5c76bd963f8c136fe5dd4c2d7681b469
Author: llocarnini <lillian.locarnini@iqser.com>
Date: Mon Jul 4 16:56:29 2022 +0200
added new fixtures for table_parsing_test.py
commit 37606cac0301b13e99be2c16d95867477f29e7c4
Author: llocarnini <lillian.locarnini@iqser.com>
Date: Fri Jul 1 16:02:44 2022 +0200
added separate file for table parsing fixtures, where fixtures for generic tables were added. WIP tests for generic table fixtures
102 lines
3.0 KiB
Python
102 lines
3.0 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.structures import Rectangle
|
|
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(map(Rectangle.from_xywh, rects))
|
|
|
|
|
|
# def annotate_layout_in_pdf(page, return_rects=False, 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)
|
|
|
|
# if return_rects:
|
|
# return rects, page
|
|
# elif show:
|
|
# page = draw_rectangles(page, rects)
|
|
# vizlogger.debug(page, "layout10_output.png")
|
|
# show_mpl(page)
|
|
# else:
|
|
# page = draw_rectangles(page, rects)
|
|
# return page
|