Merge in RR/vidocp from layout_detection_version_3 to master
Squashed commit of the following:
commit 262b1c14c0b8b164221d39fd286b20914d1a8e6a
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Sat Feb 5 22:56:10 2022 +0100
comment
commit 975dcdaae2b0e9bfcb075fe1c87adc48175c0d93
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Sat Feb 5 22:50:41 2022 +0100
applied black
commit 49ba3b5f318a1b5d6bb39c0b53de5e237a87da96
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Sat Feb 5 22:48:44 2022 +0100
improved layout parsing logic: filtering of included rects
commit d78ac24c10793f72b569c3c827834400b730888a
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Sat Feb 5 22:36:49 2022 +0100
improved layout parsing logic: filtering of overlaps, no sub-text regions
81 lines
1.6 KiB
Python
81 lines
1.6 KiB
Python
import cv2
|
|
from matplotlib import pyplot as plt
|
|
|
|
|
|
def show_mpl(image):
|
|
|
|
fig, ax = plt.subplots(1, 1)
|
|
fig.set_size_inches(20, 20)
|
|
ax.imshow(image)
|
|
plt.show()
|
|
|
|
|
|
def show_cv2(image):
|
|
|
|
cv2.imshow("", image)
|
|
cv2.waitKey(0)
|
|
|
|
|
|
def copy_and_normalize_channels(image):
|
|
|
|
image = image.copy()
|
|
try:
|
|
image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
|
|
except cv2.error:
|
|
pass
|
|
|
|
return image
|
|
|
|
|
|
def draw_contours(image, contours):
|
|
|
|
image = copy_and_normalize_channels(image)
|
|
|
|
for cont in contours:
|
|
cv2.drawContours(image, cont, -1, (0, 255, 0), 4)
|
|
|
|
return image
|
|
|
|
|
|
def draw_rectangles(image, rectangles, color=None):
|
|
|
|
image = copy_and_normalize_channels(image)
|
|
|
|
if not color:
|
|
color = (0, 255, 0)
|
|
|
|
for rect in rectangles:
|
|
x, y, w, h = rect
|
|
cv2.rectangle(image, (x, y), (x + w, y + h), color, 2)
|
|
|
|
return image
|
|
|
|
|
|
def draw_stats(image, stats, annotate=False):
|
|
|
|
image = copy_and_normalize_channels(image)
|
|
|
|
keys = ["x", "y", "w", "h"]
|
|
|
|
def annotate_stat(x, y, w, h):
|
|
|
|
for i, (s, v) in enumerate(zip(keys, [x, y, w, h])):
|
|
anno = f"{s} = {v}"
|
|
xann = int(x + 5)
|
|
yann = int(y + h - (20 * (i + 1)))
|
|
cv2.putText(image, anno, (xann, yann), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
|
|
|
|
def draw_stat(stat):
|
|
|
|
x, y, w, h, area = stat
|
|
|
|
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
|
|
|
if annotate:
|
|
annotate_stat(x, y, w, h)
|
|
|
|
for stat in stats[2:]:
|
|
draw_stat(stat)
|
|
|
|
return image
|