cv-analysis-service/test/fixtures/figure_detection.py
Julius Unverfehrt e7b28f5bda Pull request #18: Remove pil
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
2022-07-21 13:25:00 +02:00

93 lines
2.8 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, n_images, background):
page_image = paste_image(background, random_image, (200, 200))
# if n_images == 2: # TODO: Adjust image paste position, might be out of bounds
# page_image = paste_image(page_image, random_image, (1000, 2600))
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