from pathlib import Path

import numpy as np
import skimage.io
from matplotlib import pyplot as plt
from segmentation_models import get_preprocessing

from tools.model import configs
from tools.model import input_preprocessing
from utils import image as image_utils


BACKBONE_INPUT_PREPROCESSING = get_preprocessing(configs.BACKBONE)

INPUT_PREPROCESSOR = input_preprocessing.InputPreprocessor(
    configs.INPUT_SIZE, configs.INPUT_CHANNELS, configs.CLASSES_NUMBER,
    configs.AUGMENTATIONS, BACKBONE_INPUT_PREPROCESSING)


def show_augmented_image(path: Path):
    image = skimage.io.imread(str(path), as_gray=True)

    image = INPUT_PREPROCESSOR.preprocess_image(image)[0].astype(np.float32)
    print('Image:', image.dtype, image.min(), image.max(), image.shape)
    n = 64
    image_array = np.zeros((n, *INPUT_PREPROCESSOR.image_input_size, INPUT_PREPROCESSOR.image_input_channels))

    for i in range(n):
        aug_image, mask = INPUT_PREPROCESSOR.augmentate_image_mask(image, None)
        # Normalize once again image to [0, 1] after augmentation
        aug_image = image_utils.normalized_image(aug_image)

        aug_image = aug_image * 255
        aug_image = np.stack((aug_image,) * INPUT_PREPROCESSOR.image_input_channels, axis=-1)

        image_array[i] = aug_image

        # skimage.io.imshow(aug_image.astype(np.uint8))
        # skimage.io.show()

    plot_n_images(image_array, n)
    skimage.io.show()


def plot_n_images(image_array, n):
    """ plot first n images
    n has to be a square number
    """
    first_n_images = image_array[:n, :]

    grid_size = int(np.sqrt(n))
    fig, ax_array = plt.subplots(nrows=grid_size, ncols=grid_size, sharex=True, sharey=True, figsize=(8, 8))
    for r in range(grid_size):
        for c in range(grid_size):
            ax_array[r, c].imshow(first_n_images[grid_size * r + c].astype(np.uint8))
            plt.xticks(np.array([]))
            plt.yticks(np.array([]))


if __name__ == '__main__':
    show_augmented_image(Path(r'D:\Temp\model_rotate_test\9.png'))

from pathlib import Path

import numpy as np
import skimage.io
from matplotlib import pyplot as plt
from segmentation_models import get_preprocessing

from tools.model import configs
from tools.model import input_preprocessing
from utils import image as image_utils


BACKBONE_INPUT_PREPROCESSING = get_preprocessing(configs.BACKBONE)

INPUT_PREPROCESSOR = input_preprocessing.InputPreprocessor(
    configs.INPUT_SIZE, configs.INPUT_CHANNELS, configs.CLASSES_NUMBER,
    configs.AUGMENTATIONS, BACKBONE_INPUT_PREPROCESSING)


def show_augmented_image(path: Path):
    image = skimage.io.imread(str(path), as_gray=True)

    image = INPUT_PREPROCESSOR.preprocess_image(image)[0].astype(np.float32)
    print('Image:', image.dtype, image.min(), image.max(), image.shape)
    n = 64
    image_array = np.zeros((n, *INPUT_PREPROCESSOR.image_input_size, INPUT_PREPROCESSOR.image_input_channels))

    for i in range(n):
        aug_image, mask = INPUT_PREPROCESSOR.augmentate_image_mask(image, None)
        # Normalize once again image to [0, 1] after augmentation
        aug_image = image_utils.normalized_image(aug_image)

        aug_image = aug_image * 255
        aug_image = np.stack((aug_image,) * INPUT_PREPROCESSOR.image_input_channels, axis=-1)

        image_array[i] = aug_image

        # skimage.io.imshow(aug_image.astype(np.uint8))
        # skimage.io.show()

    plot_n_images(image_array, n)
    skimage.io.show()


def plot_n_images(image_array, n):
    """ plot first n images
    n has to be a square number
    """
    first_n_images = image_array[:n, :]

    grid_size = int(np.sqrt(n))
    fig, ax_array = plt.subplots(nrows=grid_size, ncols=grid_size, sharex=True, sharey=True, figsize=(8, 8))
    for r in range(grid_size):
        for c in range(grid_size):
            ax_array[r, c].imshow(first_n_images[grid_size * r + c].astype(np.uint8))
            plt.xticks(np.array([]))
            plt.yticks(np.array([]))


if __name__ == '__main__':
    show_augmented_image(Path(r'D:\Temp\model_rotate_test\9.png'))

# Normalize once again image to [0, 1] after augmentation # skimage.io.imshow(aug_image.astype(np.uint8)) # skimage.io.show() plot first n images     n has to be a square number