import csv
import os

import cv2
import numpy as np
from keras.layers import Flatten, Dense, Lambda, Cropping2D
from keras.models import Sequential

full_path_csv = os.path.abspath('data' + os.sep + 'driving_log.csv')
full_path_img = os.path.abspath('data' + os.sep + 'IMG') + os.sep

# Reading the contents in the CSV file
lines = []
with open(full_path_csv) as csv_file:
    reader = csv.reader(csv_file)
    for line in reader:
        lines.append(line)

# Reading the images (center, left, right) and its measurements
images = []
measurements = []
for line in lines:
    for camera in range(3):
        source_path = line[camera]
        file_name = source_path.split('/')[-1]
        current_path = full_path_img + file_name
        image = cv2.imread(current_path)
        images.append(image)
        measurement = float(line[3])
        measurements.append(measurement)

# Augmenting the image data and its measurement. Flipping them vertically.
augmented_images = []
augmented_measurements = []
for image, measurement in zip(images, measurements):
    augmented_images.append(image)
    augmented_measurements.append(measurement)
    augmented_images.append(cv2.flip(image, 1))  # Flipping them vertically
    augmented_measurements.append(measurement * -1.0)

x_train = np.array(augmented_images)
y_train = np.array(augmented_measurements)

model = Sequential()
model.add(Lambda(lambda x: x / 255.0 - 0.5, input_shape=(160, 320, 3)))  # normalise data
model.add(Cropping2D(cropping=((70, 25), (0, 0))))
model.add(Flatten())
model.add(Dense(1))

model.compile(loss='mse', optimizer='adam')
model.fit(x_train, y_train, validation_split=0.2, shuffle=True, nb_epoch=7)

model.save('model.h5')

@@ -4,52 +4,14 @@
 ort 
-csv%0Aimport os%0A%0Aimport cv2%0Aimport numpy as np
+pickle
 %0Afro
@@ -108,158 +108,28 @@
 al%0A%0A
-full_path_csv = os.path.abspath('data' + os.sep + 'driving_log.csv')%0Afull_path_img = os.path.abspath('data' + os.sep + 'IMG') + os.sep%0A%0A# Read
+%22%22%22%0A2. Creat
 ing the 
 cont
@@ -128,174 +128,25 @@
 the 
-contents in the CSV file%0Alines = %5B%5D%0Awith open(full_path_csv) as csv_file:%0A    reader = csv.reader(csv_file)%0A    for line in reader:%0A        lines.append(line)
+model%0A%22%22%22
 %0A%0A# Read
 ing 
@@ -145,1239 +145,161 @@
 Read
-ing
  the 
-images (center, left, right) and its measurements%0Aimages = %5B%5D%0Ameasurements = %5B%5D%0Afor line in lines:%0A    for camera in range(3):%0A        source_path = line%5Bcamera%5D%0A        file_name = source_path.split('/')%5B-1%5D%0A        current_path = full_path_img + file_name%0A        image = cv2.imread(current_path)%0A        images.append(image)%0A        measurement = float(line%5B3%5D)%0A        measurements.append(measurement)%0A%0A# Augmenting the image data and its measurement. Flipping them vertically.%0Aaugmented_images = %5B%5D%0Aaugmented_measurements = %5B%5D%0Afor image, measurement in zip(images, measurements):%0A    augmented_images.append(image)%0A    augmented_measurements.append(measurement)%0A    augmented_images.append(cv2.flip(image, 1))  # Flipping them vertically%0A    augmented_measurements.append(measurement * -1.0)%0A%0Ax_train = np.array(augmented_images)%0Ay_train = np.array(augmented_measurements)%0A%0Amodel = Sequential()%0Amodel.add(Lambda(lambda x: x / 255.0 - 0.5, input_shape=(160, 320, 3)))  # normalise data%0Amodel.add(Cropping2D(cropping=((70, 25), (0, 0))))%0Amodel.add(Flatten())%0Amodel.add(Dense(1))%0A%0Amodel.compile(loss='mse', optimizer='adam')%0Amodel.fit(x_train, y_train, validation_split=0.2, shuffle=True, nb_epoch=7)%0A%0Amodel.save('model.h5')
+pickled data.%0Awith open('data.p', mode='rb') as f:%0A    data = pickle.load(f)%0A%0Ax_train, y_train = data%5B'x_train'%5D, data%5B'y_train'%5D%0A%0A# Create the model.%0A
 %0A

read pickled data and model removed