-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomAugmentations.py
More file actions
37 lines (27 loc) · 1.07 KB
/
CustomAugmentations.py
File metadata and controls
37 lines (27 loc) · 1.07 KB
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
import cv2
from skimage import util, transform
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import random
class CustomAugmentations(ImageDataGenerator):
def __init__(self, **kwargs):
super().__init__(preprocessing_function=self.augment, **kwargs)
random.seed()
def augment(self, image):
if (random.randint(0, 1) == 0):
return image
return self.add_rotation(self.add_blur(self.add_noise(image)))
def add_noise(self, image):
if (random.randint(0, 1) == 0):
return image
return util.random_noise(image, mode='gaussian', clip=True, mean=0, var=3)
def add_blur(self, image):
if (random.randint(0, 1) == 0):
return image
kernel_size = 5
sigma = random.uniform(2.5, 3.5)
return cv2.GaussianBlur(image ,(kernel_size, kernel_size), sigma)
def add_rotation(self, image):
if (random.randint(0, 1) == 0):
return image
angle = random.uniform(-10, 10)
return transform.rotate(image, angle)