-
Notifications
You must be signed in to change notification settings - Fork 0
/
img_augmentations.py
78 lines (62 loc) · 3 KB
/
img_augmentations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import os
from PIL import Image
import numpy as np
import albumentations as A
if not os.path.exists("./landcover.ai.v1/augmented"):
os.makedirs("./landcover.ai.v1/augmented/image")
os.makedirs("./landcover.ai.v1/augmented/label")
print(f"Folders created.")
train_data_dir = "landcover.ai.v1/train/image"
train_label_dir = "landcover.ai.v1/train/label"
with open("landcover.ai.v1/train.txt") as f:
train_split = f.read().splitlines()
for s in train_split:
pillow_image = Image.open(f"{train_data_dir}/{s}.tif")
image = np.array(pillow_image)
pillow_mask = Image.open(f"{train_label_dir}/{s}.tif")
mask = np.array(pillow_mask)
hflip = A.HorizontalFlip(p=1)
vflip = A.VerticalFlip(p=1)
hue = A.HueSaturationValue(p=1)
gray = A.ToGray(p=1)
brightness = A.RandomBrightnessContrast(p=1)
### apply hflip
transformed = hflip(image=image, mask=mask)
transformed_image = transformed["image"]
transformed_mask = transformed["mask"]
pil_transformed_image = Image.fromarray(transformed_image)
pil_transformed_mask = Image.fromarray(transformed_mask)
pil_transformed_image.save(f"./landcover.ai.v1/augmented/image/{s}_hflip.tif")
pil_transformed_mask.save(f"./landcover.ai.v1/augmented/label/{s}_hflip.tif")
###apply vflip
transformed = vflip(image=image, mask=mask)
transformed_image = transformed["image"]
transformed_mask = transformed["mask"]
pil_transformed_image = Image.fromarray(transformed_image)
pil_transformed_mask = Image.fromarray(transformed_mask)
pil_transformed_image.save(f"./landcover.ai.v1/augmented/image/{s}_vflip.tif")
pil_transformed_mask.save(f"./landcover.ai.v1/augmented/label/{s}_vflip.tif")
### apply hue
transformed = hue(image=image, mask=mask)
transformed_image = transformed["image"]
transformed_mask = transformed["mask"]
pil_transformed_image = Image.fromarray(transformed_image)
pil_transformed_mask = Image.fromarray(transformed_mask)
pil_transformed_image.save(f"./landcover.ai.v1/augmented/image/{s}_hue.tif")
pil_transformed_mask.save(f"./landcover.ai.v1/augmented/label/{s}_hue.tif")
### apply gray
transformed = gray(image=image, mask=mask)
transformed_image = transformed["image"]
transformed_mask = transformed["mask"]
pil_transformed_image = Image.fromarray(transformed_image)
pil_transformed_mask = Image.fromarray(transformed_mask)
pil_transformed_image.save(f"./landcover.ai.v1/augmented/image/{s}_gray.tif")
pil_transformed_mask.save(f"./landcover.ai.v1/augmented/label/{s}_gray.tif")
### apply brightness
transformed = brightness(image=image, mask=mask)
transformed_image = transformed["image"]
transformed_mask = transformed["mask"]
pil_transformed_image = Image.fromarray(transformed_image)
pil_transformed_mask = Image.fromarray(transformed_mask)
pil_transformed_image.save(f"./landcover.ai.v1/augmented/image/{s}_bright.tif")
pil_transformed_mask.save(f"./landcover.ai.v1/augmented/label/{s}_bright.tif")