-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_augmentor.py
70 lines (53 loc) · 2.16 KB
/
image_augmentor.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
import cv2
import numpy as np
from imgaug import augmenters as ima
from matplotlib import image as mpimg
class ImageAugmentor:
def __init__(self):
self.top_of_image = 60
self.bottom_of_image = 135
def image_preprocess(self, image):
image = image[self.top_of_image:self.bottom_of_image, :, :]
image = cv2.cvtColor(image, cv2.COLOR_RGB2YUV)
kernel_size = (3, 3)
image = cv2.GaussianBlur(image, kernel_size, 0)
target_size = (200, 66) # per NVidia model recommendations
image = cv2.resize(image, target_size)
# normalize the image
image = image / 255
return image
def zoom(self, image):
zoom = ima.Affine(scale=(1, 1.3))
image = zoom.augment_image(image)
return image
def pan(self, image):
pan = ima.Affine(translate_percent={"x": (-0.1, 0.1), "y": (-0.1, 0.1)})
image = pan.augment_image(image)
return image
def randomly_alter_brightness(self, image):
brightness = ima.Multiply((0.2, 1.2))
image = brightness.augment_image(image)
return image
def flip(self, image, steering_angle):
HORIZONTAL_FLIP = 1
image = cv2.flip(image, HORIZONTAL_FLIP)
steering_angle = -steering_angle
return image, steering_angle
def random_augment(self, image, steering_angle):
augmentation_types = []
# we don't want any one transform used more than 50% of the time
FREQUENCY_THRESHOLD = 0.5
image = mpimg.imread(image)
if np.random.rand() < FREQUENCY_THRESHOLD:
image = self.pan(image)
augmentation_types.append("Pan")
if np.random.rand() < FREQUENCY_THRESHOLD:
image = self.zoom(image)
augmentation_types.append("Zoom")
if np.random.rand() < FREQUENCY_THRESHOLD:
image = self.randomly_alter_brightness(image)
augmentation_types.append("Brightness")
if np.random.rand() < FREQUENCY_THRESHOLD:
image, steering_angle = self.flip(image, steering_angle)
augmentation_types.append("Flip")
return image, steering_angle, augmentation_types