Merge in RR/cv-analysis from new_pyinfra to master
Squashed commit of the following:
commit f7a01a90aad1c402ac537de5bdf15df628ad54df
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Wed Jul 27 10:40:59 2022 +0200
fix typo
commit ff4d549fac5b612c2d391ae85823c5eca1e91916
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Wed Jul 27 10:34:04 2022 +0200
adjust build scripts for new pyinfra
commit ecd70f60d46406d8b6cc7f36a1533d706c917ca8
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Wed Jul 27 09:42:55 2022 +0200
simplify logging by using default configurations
commit 20193c14c940eed2b0a7a72058167e26064119d0
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Tue Jul 26 17:16:57 2022 +0200
tidy-up, refactor config logic to not dependent on external files
commit d8069cd4d404a570bb04a04278161669d1c83332
Author: Isaac Riley <Isaac.Riley@iqser.com>
Date: Tue Jul 26 15:14:59 2022 +0200
update pyinfra
commit c3bc11037cca9baf016043ab997c566f5b4a2586
Author: Isaac Riley <Isaac.Riley@iqser.com>
Date: Tue Jul 26 15:09:14 2022 +0200
repair tests
commit 6f4e4f2863ee16ae056c1d432f663858c5f10221
Author: Isaac Riley <Isaac.Riley@iqser.com>
Date: Tue Jul 26 14:52:38 2022 +0200
updated server logic to work with new pyinfra; update scripts for pyinfra as submodule
commit 2a18dba81de5ee84d0bdf0e77f478693e8d8aef4
Author: Isaac Riley <Isaac.Riley@iqser.com>
Date: Tue Jul 26 14:10:41 2022 +0200
formatting
commit d87ce9328de9aa2341228af9b24473d5e583504e
Author: Isaac Riley <Isaac.Riley@iqser.com>
Date: Tue Jul 26 14:10:11 2022 +0200
make server logic compatible with new pyinfra
91 lines
2.7 KiB
Python
91 lines
2.7 KiB
Python
import textwrap
|
|
|
|
import cv2
|
|
import numpy as np
|
|
import pytest
|
|
from lorem_text import lorem
|
|
|
|
from cv_analysis.figure_detection.figure_detection_pipeline import (
|
|
make_figure_detection_pipeline,
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def page_with_images(random_image, background):
|
|
page_image = paste_image(background, random_image, (200, 200))
|
|
return np.array(page_image)
|
|
|
|
|
|
@pytest.fixture
|
|
def page_with_text(background, font_scale, font_style, text_types):
|
|
cursor = (50, 50)
|
|
image = background
|
|
body_height = image.shape[0] // 3
|
|
if "header" in text_types:
|
|
image = paste_text(image, cursor, font_scale, font_style, y_stop=70)
|
|
if "body" in text_types:
|
|
cursor = (image.shape[1] // 2, 70)
|
|
image = paste_text(image, cursor, font_scale, font_style, y_stop=body_height)
|
|
cursor = (50, body_height + 70)
|
|
image = paste_text(image, cursor, font_scale, font_style, y_stop=body_height * 2)
|
|
if "caption" in text_types:
|
|
cursor = (image.shape[1] // 2, image.shape[0] - 100)
|
|
image = paste_text(image, cursor, font_scale, font_style, y_stop=body_height * 3)
|
|
return image
|
|
|
|
|
|
@pytest.fixture
|
|
def page_with_images_and_text(page_with_images, page_with_text):
|
|
return np.fmin(page_with_text, page_with_images)
|
|
|
|
|
|
@pytest.fixture
|
|
def background(background_color):
|
|
return np.ones((3508, 2480, 3), dtype="uint8") * background_color
|
|
|
|
|
|
@pytest.fixture
|
|
def random_image(image_size):
|
|
return np.random.rand(*image_size, 3) * 255
|
|
|
|
|
|
@pytest.fixture
|
|
def figure_detection_pipeline():
|
|
return make_figure_detection_pipeline()
|
|
|
|
|
|
def paste_text(image: np.ndarray, cursor, font_scale, font_style, y_stop):
|
|
def paste_text_at_cursor(x_start, y_start, y_stop):
|
|
# TODO: adjust incorrect right margin
|
|
text = lorem.paragraphs(1) * 200
|
|
(dx, dy), base = cv2.getTextSize(text, fontFace=font_style, fontScale=font_scale, thickness=1)
|
|
dy += base
|
|
# char_width = dx // len(text)
|
|
text = textwrap.fill(text=text, width=(dx // page_width))
|
|
for i, line in enumerate(text.split("\n")):
|
|
y = y_start + i * dy
|
|
if y > y_stop:
|
|
break
|
|
cv2.putText(
|
|
image,
|
|
line,
|
|
org=(x_start, y),
|
|
fontFace=font_style,
|
|
fontScale=font_scale,
|
|
color=(0, 0, 0),
|
|
thickness=1,
|
|
lineType=cv2.LINE_AA,
|
|
)
|
|
|
|
x_start, y_start = cursor
|
|
page_width = image.shape[1]
|
|
paste_text_at_cursor(x_start, y_start, y_stop)
|
|
return image
|
|
|
|
|
|
def paste_image(page_image, image, coords):
|
|
h, w = image.shape[:2]
|
|
x, y = coords
|
|
page_image[x : x + h, y : y + w] = image
|
|
return page_image
|