From 4772e3037c57a242d111932290206e4ac91f41f0 Mon Sep 17 00:00:00 2001 From: Matthias Bisping Date: Mon, 16 Jan 2023 11:16:27 +0100 Subject: [PATCH] Remove obsolete code --- test/fixtures/page_generation/page.py | 223 ++++++++++---------------- 1 file changed, 88 insertions(+), 135 deletions(-) diff --git a/test/fixtures/page_generation/page.py b/test/fixtures/page_generation/page.py index d97cf31..64e61c7 100644 --- a/test/fixtures/page_generation/page.py +++ b/test/fixtures/page_generation/page.py @@ -1,45 +1,12 @@ import random -from typing import Tuple +from typing import Tuple, Union import albumentations as A +import cv2 as cv import numpy as np import pytest from PIL import Image, ImageOps - -# transform = A.Compose( -# [ -# # brightness and contrast transforms -# A.OneOf( -# [ -# A.RandomGamma(p=0.2), -# A.RandomBrightnessContrast(p=0.2, brightness_limit=0.05, contrast_limit=0.05), -# ], -# p=0.5, -# ), -# # color transforms -# A.SomeOf( -# [ -# A.ColorJitter(p=1), -# A.RGBShift(p=1, r_shift_limit=0.3, g_shift_limit=0.3, b_shift_limit=0.3), -# A.ChannelShuffle(p=1), -# ], -# p=1.0, -# n=3, # 3 => all -# ), -# # # blurring and sharpening transforms -# # A.OneOf( -# # [ -# # A.GaussianBlur(p=0.05), -# # A.MotionBlur(p=0.05, blur_limit=21), -# # A.Sharpen(p=0.05), -# # ], -# # p=0.0, -# # ), -# ] -# ) - from PIL.Image import Transpose - # # transform = A.Compose( # [ @@ -96,8 +63,42 @@ from PIL.Image import Transpose # ], # p=0.5, # ) +from funcy import juxt, compose, identity + from cv_analysis.locations import TEST_PAGE_TEXTURES_DIR +# transform = A.Compose( +# [ +# # brightness and contrast transforms +# A.OneOf( +# [ +# A.RandomGamma(p=0.2), +# A.RandomBrightnessContrast(p=0.2, brightness_limit=0.05, contrast_limit=0.05), +# ], +# p=0.5, +# ), +# # color transforms +# A.SomeOf( +# [ +# A.ColorJitter(p=1), +# A.RGBShift(p=1, r_shift_limit=0.3, g_shift_limit=0.3, b_shift_limit=0.3), +# A.ChannelShuffle(p=1), +# ], +# p=1.0, +# n=3, # 3 => all +# ), +# # # blurring and sharpening transforms +# # A.OneOf( +# # [ +# # A.GaussianBlur(p=0.05), +# # A.MotionBlur(p=0.05, blur_limit=21), +# # A.Sharpen(p=0.05), +# # ], +# # p=0.0, +# # ), +# ] +# ) + transform = A.Compose( [ # A.ColorJitter(p=1), @@ -158,8 +159,8 @@ def color_name(request): @pytest.fixture( params=[ - "smooth", - "coarse", + # "smooth", + # "coarse", "neutral", ] ) @@ -186,19 +187,6 @@ def random_flip(image): return image -# @pytest.fixture -# def color(color_name): -# return { -# "brown": (0.5, 0.3, 0.2), -# "yellow": (0.5, 0.5, 0.0), -# "sepia": (173, 155, 109), -# "gray": (0.3, 0.3, 0.3), -# "white": (0.0, 0.0, 0.0), -# "light_red": (0.5, 0.0, 0.0), -# "light_blue": (0.0, 0.0, 0.5), -# }[color_name] - - @pytest.fixture def color(color_name): return { @@ -211,47 +199,54 @@ def color(color_name): }[color_name] -# @pytest.fixture -# def texture_fn(texture_name, size): -# if texture_name == "smooth": -# fn = blur -# elif texture_name == "coarse": -# fn = compose(overlay, juxt(blur, sharpen)) -# else: -# fn = identity -# -# return fn -# -# -# def blur(image: np.ndarray): -# return cv.blur(image, (3, 3)) -# -# -# def sharpen(image: np.ndarray): -# return cv.filter2D(image, -1, np.array([[-1, -1, -1], [-1, 6, -1], [-1, -1, -1]])) -# -# -# def overlay(images, mode=np.sum): -# assert mode in [np.sum, np.max] -# images = np.stack(list(images)) -# image = mode(images, axis=0) -# image = (image / image.max() * 255).astype(np.uint8) -# return image -# -# -# @pytest.fixture -# def texture(texture_fn, size, color): -# noise_arr = np.random.rand(*size) * 255 -# noise_arr = color_shift_noise(noise_arr, color) -# -# noise_arr = zero_out_below_threshold(noise_arr, 0.4) -# noise_arr = texture_fn(noise_arr) -# assert noise_arr.max() <= 255 -# -# noise_img = Image.fromarray(noise_arr) -# # noinspection PyTypeChecker -# assert np.equal(noise_arr, np.array(noise_img)).all() -# return noise_img +@pytest.fixture +def texture_fn(texture_name, size): + if texture_name == "smooth": + fn = blur + elif texture_name == "coarse": + fn = compose(overlay, juxt(blur, sharpen)) + else: + fn = identity + + return normalize_image_function(fn) + + +def blur(image: np.ndarray): + return cv.blur(image, (3, 3)) + + +def normalize_image_format_to_array(image: Union[np.ndarray, Image.Image]): + if isinstance(image, Image.Image): + return np.array(image) + return image + + +def normalize_image_format_to_pil(image: Union[np.ndarray, Image.Image]): + if isinstance(image, np.ndarray): + return Image.fromarray(image) + return image + + +def normalize_image_function(func): + def inner(image): + image = normalize_image_format_to_array(image) + image = func(image) + image = normalize_image_format_to_pil(image) + return image + + return inner + + +def sharpen(image: np.ndarray): + return cv.filter2D(image, -1, np.array([[-1, -1, -1], [-1, 6, -1], [-1, -1, -1]])) + + +def overlay(images, mode=np.sum): + assert mode in [np.sum, np.max] + images = np.stack(list(images)) + image = mode(images, axis=0) + image = (image / image.max() * 255).astype(np.uint8) + return image @pytest.fixture @@ -271,25 +266,6 @@ def tint_image(src, color="#FFFFFF"): return result -# def color_shift_noise(noise: np.ndarray, color: Color): -# """Creates a 3-tensor from a 2-tensor by stacking the 2-tensor three times weighted by the color tuple.""" -# assert noise.ndim == 2 -# assert isinstance(color, tuple) -# assert max(color) <= 255 -# assert noise.max() <= 255 -# -# color = np.array(color) -# weights = color / color.sum() -# assert max(weights) <= 1 -# -# alpha_channel = np.ones(noise.shape) * 255 -# colored_noise = np.stack([noise * weight for weight in weights] + [alpha_channel], axis=-1).astype(np.uint8) -# -# assert colored_noise.shape == (*noise.shape, 4) -# -# return colored_noise - - def color_shift_array(image: np.ndarray, color: Color): """Creates a 3-tensor from a 2-tensor by stacking the 2-tensor three times weighted by the color tuple.""" assert image.ndim == 3 @@ -302,9 +278,6 @@ def color_shift_array(image: np.ndarray, color: Color): weights = color / color.sum() / 10 assert max(weights) <= 1 - # alpha_channel = np.ones(image.shape) * 255 - # colored_noise = np.stack([image * weight for weight in weights] + [alpha_channel], axis=-1).astype(np.uint8) - # colored = np.column_stack([image * weights] + [alpha_channel]).astype(np.uint8) colored = (image * weights).astype(np.uint8) assert colored.shape == image.shape @@ -312,18 +285,6 @@ def color_shift_array(image: np.ndarray, color: Color): return colored -# def zero_out_below_threshold(texture, threshold): -# assert texture.max() <= 255 -# -# texture[:, :, 3] = 100 -# -# threshold = int(texture[:, :, 0:3].sum(axis=2).max() * threshold) -# threshold_mask = texture[:, :, 0:3].sum(axis=2) >= threshold -# texture[~threshold_mask] = [0, 0, 0, 50] -# -# return texture - - @pytest.fixture def size(dpi, orientation): if orientation == "portrait": @@ -336,15 +297,6 @@ def size(dpi, orientation): return size -# @pytest.fixture -# def blank_page(size, texture) -> np.ndarray: -# """Creates a blank page with a given orientation and dpi.""" -# page = Image.fromarray(np.zeros((*size, 4), dtype=np.uint8) * 255) -# page = superimpose_texture_with_transparency(page, texture) -# # page = transform(image=page)["image"] -# return page - -# def superimpose_texture_with_transparency(page: Image, texture: Image) -> Image: """Superimposes a noise image with transparency onto a page image.""" assert page.mode == "RGB" @@ -355,7 +307,8 @@ def superimpose_texture_with_transparency(page: Image, texture: Image) -> Image: @pytest.fixture -def blank_page(texture) -> np.ndarray: +def blank_page(texture, texture_fn) -> np.ndarray: """Creates a blank page with a given orientation and dpi.""" page = random_flip(texture) + page = texture_fn(page) return page