Merge in RR/cv-analysis from remove_pil to master
Squashed commit of the following:
commit 83c8d88f3d48404251470176c70979ee75ae068b
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Thu Jul 21 10:51:51 2022 +0200
remove deprecated server tests
commit cebc03b5399ac257a74036b41997201f882f5b74
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Thu Jul 21 10:51:08 2022 +0200
remove deprecated server tests
commit ce2845b0c51f001b7b5b8b195d6bf7e034ec4e39
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Wed Jul 20 17:05:00 2022 +0200
repair tests to work without pillow WIP
commit 023fdab8322f28359a24c63e32635a3d0deccbe4
Author: Isaac Riley <Isaac.Riley@iqser.com>
Date: Wed Jul 20 16:40:36 2022 +0200
fixed typo
commit 33850ca83a175f74789ae6b9bebd057ed84b7fb3
Author: Isaac Riley <Isaac.Riley@iqser.com>
Date: Wed Jul 20 16:38:37 2022 +0200
fixed import from refactored open_img.py
commit dbc6d345f074e538948e2c4f94ebed8a5ef520bc
Author: Isaac Riley <Isaac.Riley@iqser.com>
Date: Wed Jul 20 16:32:42 2022 +0200
removed PIL from production code, now inly in scripts
78 lines
2.3 KiB
Python
78 lines
2.3 KiB
Python
from itertools import compress
|
|
from itertools import starmap
|
|
from operator import __and__
|
|
|
|
import cv2
|
|
import numpy as np
|
|
|
|
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))
|