cv-analysis-service/test/fixtures/figure_detection.py
Julius Unverfehrt fc8a9e15f8 Pull request #12: Diff font sizes on page
Merge in RR/cv-analysis from diff-font-sizes-on-page to master

Squashed commit of the following:

commit d1b32a3e8fadd45d38040e1ba96672ace240ae29
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date:   Thu Jun 30 14:43:30 2022 +0200

    add tests for figure detection first iteration

commit c38a7701afaad513320f157fe7188b3f11a682ac
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date:   Thu Jun 30 14:26:08 2022 +0200

    update text tests with new test cases

commit ccc0c1a177c7d69c9575ec0267a492c3eef008e3
Author: llocarnini <lillian.locarnini@iqser.com>
Date:   Wed Jun 29 23:09:24 2022 +0200

    added fixture for different scaled text on page and parameter for different font style

commit 5f36a634caad2849e673de7d64abb5b6c3a6055f
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date:   Tue Jun 28 17:03:52 2022 +0200

    add pdf2pdf annotate script for figure detection

commit 7438c170371e166e82ab19f9dfdf1bddd89b7bb3
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date:   Tue Jun 28 16:24:52 2022 +0200

    optimize algorithm

commit 93bf8820f856d3815bab36b13c0df189c45d01e0
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date:   Tue Jun 28 16:11:15 2022 +0200

    black

commit 59c639eec7d3f9da538b0ad6cd6215456c92eb58
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date:   Tue Jun 28 16:10:39 2022 +0200

    add tests for figure detection pipeline

commit bada688d88231843e9d299d255d9c4e0d5ca9788
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date:   Tue Jun 28 13:34:36 2022 +0200

    refactor tests

commit 614388a18b46d670527727c11f63e8174aed3736
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date:   Tue Jun 28 13:34:14 2022 +0200

    introduce pipeline logic for figure detection

commit 7195f892d543294829aebe80e260b4395b89cb36
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date:   Tue Jun 28 11:58:41 2022 +0200

    update reqs

commit 4408e7975853196c5e363dd2ddf62e15fe6f4944
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date:   Tue Jun 28 11:56:16 2022 +0200

    add figure detection test

commit 5ff472c2d96238ca2bc1d2368d3d02e62db98713
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date:   Tue Jun 28 11:56:09 2022 +0200

    add figure detection test

commit 66c1307e57c84789d64cb8e41d8e923ac98eebde
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date:   Tue Jun 28 10:36:50 2022 +0200

    refactor draw boxes to work as intended on inversed image

commit 00a39050d051ae43b2a8f2c4efd6bfbd2609dead
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date:   Tue Jun 28 10:36:11 2022 +0200

    refactor module structure

commit f8af01894c387468334a332e75f7dbf545a91f86
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date:   Mon Jun 27 17:07:47 2022 +0200

    add: figure detection now agnostic to input image background color, refactor tests

commit 3bc63da783bced571d53b29b6d82648c9f93e886
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date:   Mon Jun 27 14:31:15 2022 +0200

    add text removal tests

commit 6e794a7cee3fd7633aa5084839775877b0f8794c
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date:   Mon Jun 27 12:12:27 2022 +0200

    figure detection tests WIP

commit f8b20d4c9845de6434142e3dab69ce467fbc7a75
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date:   Fri Jun 24 15:39:37 2022 +0200

    add tests for figure_detection WIP

commit f2a52a07a5e261962214dff40ba710c93993f6fb
Author: llocarnini <lillian.locarnini@iqser.com>
Date:   Fri Jun 24 14:28:44 2022 +0200

    added third test case "figure_and_text"

commit 8f45c88278cdcd32a121ea8269c8eca816bffd0b
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date:   Fri Jun 24 13:25:17 2022 +0200

    add tests for figure_detection
2022-06-30 14:50:58 +02:00

94 lines
2.9 KiB
Python

import textwrap
import cv2
import numpy as np
import pytest
from PIL import Image
from lorem_text import lorem
from funcy import first
from cv_analysis.figure_detection.figure_detection_pipeline import make_figure_detection_pipeline
from cv_analysis.utils.display import show_mpl
@pytest.fixture
def page_with_images(random_image, n_images, background):
page_image = Image.fromarray(background.astype("uint8")).convert("RGB")
page_image = paste_image(page_image, random_image, (200, 200))
if n_images == 2:
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):
image = Image.fromarray(image.astype("uint8")).convert("RGBA")
page_image.paste(image, coords)
return page_image