Merge in RR/vidocp from refactoring to master
Squashed commit of the following:
commit 36a62a13e51148d2420cb12930e84d78629db6b0
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Sun Feb 6 14:54:53 2022 +0100
refactoring
commit e652da1fa88a048f9a5211b4e8c0b96074fb5849
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Sun Feb 6 14:53:17 2022 +0100
refactoring
commit d9567da428c81f9cd7971a657281df0a90166810
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Sun Feb 6 14:47:18 2022 +0100
refactoring
commit 9d30009dceec0357db6499bfaffae8ce97718ee0
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Sun Feb 6 14:45:53 2022 +0100
refactoring
commit e8863d67aaaff138fb088c4e496a91b6354cc059
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Sun Feb 6 14:42:45 2022 +0100
refactoring
commit 89a99d3586db4fbafa743a45bdd02eaf0c1f341f
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Sun Feb 6 14:39:49 2022 +0100
refactoring
commit aa66b6865b00b0490b9e7695a6bae386e6f96723
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Sun Feb 6 14:31:21 2022 +0100
refactoring
commit 98d77cb522a08821c3a13ae2cffbe7239c654762
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Sun Feb 6 14:27:55 2022 +0100
refactoring
commit fed3a7e4f1b8b7ca4e14f9e495459c26490fb50b
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Sun Feb 6 14:26:16 2022 +0100
refactoring
commit 504cafbd5d4bba183d9943b36c60548aae34e402
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Sun Feb 6 14:25:44 2022 +0100
renaming
commit c9780a57e5a048529d36958ba678eddb11759cef
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Sun Feb 6 14:24:41 2022 +0100
removed obsolete import
commit d555e86475e82024f8e1a5fc5b0ac70faa091ee1
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Sun Feb 6 14:24:04 2022 +0100
refactored figure detection once
72 lines
2.0 KiB
Python
72 lines
2.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 vidocp.utils.display import show_mpl
|
|
from vidocp.utils.draw import draw_rectangles
|
|
from vidocp.utils.post_processing import remove_overlapping, remove_included, has_no_parent
|
|
|
|
|
|
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()
|
|
|
|
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
|
blur = cv2.GaussianBlur(gray, (7, 7), 0)
|
|
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
|
|
|
|
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
|
|
dilate = cv2.dilate(thresh, kernel, iterations=4)
|
|
|
|
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)
|
|
|
|
_, image = cv2.threshold(image, 254, 255, cv2.THRESH_BINARY)
|
|
image = ~image
|
|
|
|
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
|
rects = find_segments(image)
|
|
# <- End of meta detection
|
|
|
|
rects = remove_included(rects)
|
|
rects = remove_overlapping(rects)
|
|
|
|
return rects
|
|
|
|
|
|
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)
|
|
|
|
rects = parse_layout(page)
|
|
page = draw_rectangles(page, rects)
|
|
|
|
show_mpl(page)
|