# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
"""
![](https://albumentations.ai/assets/img/custom/albumentations_card.png)albumentations.ai
"""
"""
#Script by WONGI PARK https://www.kaggle.com/kalelpark/compare-albumentations-and-imagedatagenerator
"""
import matplotlib.pyplot as plt

import cv2
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import albumentations as alb
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import cv2
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import albumentations as alb

org_img = cv2.cvtColor(cv2.imread('../input/walkormopping/dataset/train/mopping/102.jpg'), cv2.COLOR_BGR2RGB)

# for Albumentations
def Alb_Augmentations(org_img = None, label = None, suptitle = 'Albumentation', albs = None):
    img_list = [org_img]
    label_list = ['orginal']
    for _ in range(3):
        aug_img = albs(image = org_img)['image']
        img_list.append(aug_img)
        label_list.append(label)
    fig, ax = plt.subplots(figsize = (22, 5), nrows = 1, ncols = 4)
    for i in range(4):
        ax[i].imshow(img_list[i])
        ax[i].set_title(label_list[i])
    
    plt.suptitle(f'{suptitle}')
    
# ImageDataGenerator
def Idg_Augmentations(image, data_generator, label = None, suptitle = 'ImageDataGenerator'):
    img = np.expand_dims(image, axis = 0)
    data_generator.fit(img)
    img_gen = data_generator.flow(img)
    
    fig, ax = plt.subplots(figsize = (22, 5), nrows = 1, ncols = 4)
    for i in range(4):
        if i == 0:
            ax[i].imshow(image)
            ax[i].set_title('Original')
        else:
            img_batch = next(img_gen)
            img_batch = np.squeeze(img_batch).astype('int')
            ax[i].imshow(img_batch)
            ax[i].set_title(label)

    plt.suptitle(f'{suptitle}')
"""
#Horizontal Flip, Vertical Flip

P : 확률(기본 = 0.5)
"""
#Code by WONGI PARK https://www.kaggle.com/kalelpark/compare-albumentations-and-imagedatagenerator

# Albumentation
albs = alb.HorizontalFlip(p = 0.5)
Alb_Augmentations(org_img=org_img, label = 'Horizontal', albs = albs)

# ImageDataGenerator
data_generator = ImageDataGenerator(horizontal_flip=True)
Idg_Augmentations(image=org_img, data_generator=data_generator, label = 'Horizontal')
#Code by WONGI PARK https://www.kaggle.com/kalelpark/compare-albumentations-and-imagedatagenerator

# Albumentation
albs = alb.VerticalFlip(p = 0.5)
Alb_Augmentations(org_img=org_img, label = 'Vertical', albs = albs)

# ImageDataGenerator
data_generator = ImageDataGenerator(vertical_flip=True)
Idg_Augmentations(image=org_img, data_generator=data_generator, label = 'Vertical')
"""
#Random Rotation

fill_mode : same border_mode (위와 동일합니다.)
"""
#Code by WONGI PARK https://www.kaggle.com/kalelpark/compare-albumentations-and-imagedatagenerator

# Albumentation
albs = alb.Rotate(limit= 150, p = 0.8, border_mode = cv2.BORDER_CONSTANT)
Alb_Augmentations(org_img=org_img, label = 'Rotation', albs = albs)

# ImageDataGenerator
data_generator = ImageDataGenerator(rotation_range=150 ,fill_mode = 'constant')
Idg_Augmentations(image=org_img, data_generator=data_generator, label = 'Rotation')
"""
#Shift and Scale

shift_limit : Image Move (Image가 이동합니다.)

ImageDataGenerator

width_shift_range : Image moves left or right (왼쪽 또는 오른쪽으로 움직입니다.)
height_shift_range :Image moves up or down (위, 아래로 움직입니다.)
zoom_range : equal scale_limit (위의 scale_limit과 동일합니다.)

if you wan't move, delete width_shift_range, height_shift_range

(만약 이동을 원하지 않는다면, width_shift_range, height_shift_range 제거 해주시면 됩니다.)
"""
#Code by WONGI PARK https://www.kaggle.com/kalelpark/compare-albumentations-and-imagedatagenerator

# Albumentation
albs = alb.ShiftScaleRotate(shift_limit=(0.4, 0.6), scale_limit= (0.4, 0.9), p = 0.8, rotate_limit = 0)
Alb_Augmentations(org_img=org_img, label = 'Shift and Scale', albs = albs)

# ImageDataGenerator
data_generator = ImageDataGenerator(width_shift_range=0.4, height_shift_range=0.4, zoom_range= [0.4, 1.5])
Idg_Augmentations(image=org_img, data_generator=data_generator, label = 'Shift and Scale')
"""
#Crop

Only albumentations has Crop (Albumentations 만 Crop 기능을 가지고 있습니다.)
"""
#Code by WONGI PARK https://www.kaggle.com/kalelpark/compare-albumentations-and-imagedatagenerator

# Albumentation
albs = alb.Crop(x_min = 50, y_min = 50, x_max = 150, y_max = 150, p = 0.8)
Alb_Augmentations(org_img=org_img, label = 'Crop', albs = albs) #I reduced the original was 100/100/300/300

albs = alb.Compose([
    alb.Crop(x_min = 50, y_min = 50, x_max = 150, y_max = 150, p = 0.8),
    alb.Resize(621, 934)
])
Alb_Augmentations(org_img=org_img, label = 'Crop', albs = albs)
#Code by WONGI PARK https://www.kaggle.com/kalelpark/compare-albumentations-and-imagedatagenerator

# Albumentation
albs = alb.Compose([
    alb.CenterCrop(height = 50, width = 150, p = 0.8),#Original was 100/300 I reduced them
    alb.Resize(621, 934)
])
Alb_Augmentations(org_img=org_img, label = 'CenterCrop', albs = albs)
# Albumentation
albs = alb.Compose([
    alb.RandomCrop(height = 50, width = 150, p = 0.8),
    alb.Resize(621, 934)
])
Alb_Augmentations(org_img=org_img, label = 'RandomCrop', albs = albs)
"""
#RandomBrightnessContest

brightness_limit : Set a Scope(range) in tuple. (튜플 내에 범위를 정해야 합니다.)
"""
#Code by WONGI PARK https://www.kaggle.com/kalelpark/compare-albumentations-and-imagedatagenerator

# Albumentation
albs = alb.RandomBrightnessContrast(brightness_limit=(-0.4, 0.4), contrast_limit=(-0.4, 0.4), p=0.8)
Alb_Augmentations(org_img=org_img, label = 'RandomBrightnessContrast', albs = albs)

# ImageDataGenerator
data_generator = ImageDataGenerator(brightness_range=(0.1, 0.8))
Idg_Augmentations(image=org_img, data_generator=data_generator, label = 'Bright')
"""
#ChannelShuffle

ImageDataGenerator

channel_shfit_range : RGB +- value in range (원본 RGB 값에서 범위 내의 값을 +- 합니다)
"""
#Code by WONGI PARK https://www.kaggle.com/kalelpark/compare-albumentations-and-imagedatagenerator

# Albumentation
albs = alb.ChannelShuffle(p=0.8)
Alb_Augmentations(org_img=org_img, label = 'ChannelShuffle', albs = albs)

# ImageDataGenerator
data_generator = ImageDataGenerator(channel_shift_range = 60 )
Idg_Augmentations(image=org_img, data_generator=data_generator, label = 'channel_shift_range')
"""
#HueSaturationValue

Albumentation

HueSaturationValue

hue_shift_limit : Set a Scope(shift range) in tuple. (튜플 내에 변환할 범위를 정해야 합니다.)
"""
#Code by WONGI PARK https://www.kaggle.com/kalelpark/compare-albumentations-and-imagedatagenerator

# Albumentation
albs = alb.HueSaturationValue(p=0.8)
Alb_Augmentations(org_img=org_img, label = 'HueSaturationValue', albs = albs)
"""
#RGBShift

r_shift_limit : Set a Scope(red shift range) in tuple. (튜플 내에 변환할 범위를 정해야 합니다.)
"""
#Code by WONGI PARK https://www.kaggle.com/kalelpark/compare-albumentations-and-imagedatagenerator

# Albumentation
albs = alb.RGBShift(p=0.8)
Alb_Augmentations(org_img=org_img, label = 'RGBShift', albs = albs)
"""
#ColorJitter

Change value Bright, Hue, Saturation and Contest (밝기, 색상, 채도, 대비의 값을 변환합니다.)
"""
#Code by WONGI PARK https://www.kaggle.com/kalelpark/compare-albumentations-and-imagedatagenerator

# Albumentation
albs = alb.ColorJitter(p=0.8)
Alb_Augmentations(org_img=org_img, label = 'ColorJitter', albs = albs)
"""
#Gaussian Noise

Addtion Guassian Noise (Gaussion 노이즈 추가)
"""
#Code by WONGI PARK https://www.kaggle.com/kalelpark/compare-albumentations-and-imagedatagenerator

# Albumentation
albs = alb.GaussNoise(p=0.8, var_limit= (500, 1000))
Alb_Augmentations(org_img=org_img, label = 'GaussNoise', albs = albs)
"""
#Coarse Dropout

addtion Noise Color (몇몇 부분의 색상을 검정으로 변환합니다.)
"""
#Code by WONGI PARK https://www.kaggle.com/kalelpark/compare-albumentations-and-imagedatagenerator

# Albumentation
albs = alb.CoarseDropout(p=0.8, max_holes = 50)
Alb_Augmentations(org_img=org_img, label = 'CoarseDropout', albs = albs)
"""
#CLAHE

Make contrast sharper. (명암대비가 선명해집니다.)
"""
#Code by WONGI PARK https://www.kaggle.com/kalelpark/compare-albumentations-and-imagedatagenerator

# Albumentation
albs = alb.CLAHE(p=0.8)
Alb_Augmentations(org_img=org_img, label = 'CLAHE', albs = albs)
"""
#Oneof

adaptive one contents in Oneof (Oneof 내의 한가지를 적용합니다.)
"""
#Code by WONGI PARK https://www.kaggle.com/kalelpark/compare-albumentations-and-imagedatagenerator

# Albumentation
albs = alb.OneOf([
    alb.Rotate(limit=(50, 150), p = 1, border_mode= cv2.BORDER_CONSTANT),
    alb.HorizontalFlip(p = 1),
    alb.ChannelShuffle(p = 1),
    alb.RGBShift(p = 1)
])
Alb_Augmentations(org_img=org_img, label = 'Oneof', albs = albs)
"""
#Compose

it's similar ImageDataGenerator (ImageDataGenerator 와 비슷하다고 보면 됩니다.)
"""
#Code by WONGI PARK https://www.kaggle.com/kalelpark/compare-albumentations-and-imagedatagenerator

# Albumentation
albs = alb.Compose([
    alb.Rotate(limit=(50, 150), p = 1, border_mode= cv2.BORDER_CONSTANT),
    alb.HorizontalFlip(p = 1),
    alb.ChannelShuffle(p = 1),
    alb.RGBShift(p = 1),
    alb.CLAHE(p = 1),
    alb.GaussNoise(p = 1, var_limit = (50, 100)),
    alb.CoarseDropout(p = 1, max_holes = 50)
])
Alb_Augmentations(org_img=org_img, label = 'Compose', albs = albs)

# ImageDataGenerator
data_generator = ImageDataGenerator(vertical_flip=True,
                                    horizontal_flip=True,
                                    shear_range=True,
                                    rotation_range= 150,
                                    zoom_range = 0.7
                                   )
Idg_Augmentations(image=org_img, data_generator=data_generator, label = 'ImageDataGenerator')
"""
#Acknowledgement

WONGI PARK https://www.kaggle.com/kalelpark/compare-albumentations-and-imagedatagenerator
"""