forked from udacity/CarND-Behavioral-Cloning-P3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
augment.py
63 lines (50 loc) · 2.28 KB
/
augment.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
import random
import numpy as np
from .data import load_image
# change brightness of image by amount
def adjust_brightness(img_steering, amount):
img, steering = img_steering
result = img.copy().astype(np.int16)
result[:,:,2] += amount
result = np.clip(result, 0, 255).astype(np.uint8)
return result, steering
# add random noise to an image with noise values ranging from 0 to maximum
def add_noise(img_steering, maximum=10):
img, steering = img_steering
noise = np.random.randint(0, high=maximum, size=img.shape)
result = img.copy().astype(np.int16)
result = np.clip(np.rint(result + noise), 0, 255).astype(np.uint8)
return result, steering
# flip image horizontally
def flip(img, steering):
return np.fliplr(img.copy()), -steering
# calculate steering for left/right camera images
def get_side_image(left: bool, image, center_steering, steering_correction=0.25):
steering = (center_steering + steering_correction) if left else (center_steering - steering_correction)
return image, steering
# augment data by using left/right images and adding noise, changing brightness of center/left/right images
# and flipping center images
def augment(row, center_steering, threshold=0.05, droprate=0.7):
images = []
steerings = []
def add(i, s):
images.append(i)
steerings.append(s)
amount = int(random.uniform(20, 50))
drop = random.uniform(0.0, 1.0)
center = load_image(row['center'])
left, left_steering = get_side_image(True, load_image(row['left']), center_steering)
right, right_steering = get_side_image(False, load_image(row['right']), center_steering)
work_on = [(left, left_steering), (right, right_steering)]
if abs(center_steering) >= threshold or drop >= droprate:
work_on.append((center, center_steering))
for image, steering in work_on:
add(image, steering)
add(*adjust_brightness((image, steering), amount))
add(*adjust_brightness((image, steering), -amount))
add(*add_noise((image, steering)))
add(*flip(center, center_steering))
add(*flip(*adjust_brightness((center, center_steering), amount)))
add(*flip(*adjust_brightness((center, center_steering), -amount)))
add(*flip(*add_noise((center, center_steering))))
return images, steerings