<jupyter_start><jupyter_text>Lets retrieve the dataset from drive<jupyter_code># get file names of frames
col_frames = os.listdir('/content/drive/My Drive/SDC/IMG')
# col_frames.sort(key=lambda f: int(re.sub('\D', '', f)))
col_frames.sort()
# print(col_frames)
# load frames
col_images=[]
Images=[]
for i in col_frames[:1500]:
    img = cv2.imread('/content/drive/My Drive/SDC/IMG/'+i)
    Images.append(img)
    col_images.append(img[65:140,0:320])# 65:140,0:320 is height and width pixel values where road is only visible not sky, hills, etc.
col_images=np.array(col_images)
Images=np.array(Images)
print(Images.shape)
print(col_images.shape)<jupyter_output>(1500, 160, 320, 3)
(1500, 75, 320, 3)<jupyter_text>Lets retrieve the labels for images<jupyter_code>import numpy as np

# write your code here
import csv
with open('/content/drive/My Drive/SDC/driving_log.csv') as file:
  csvreader=csv.reader(file)
  x=[]
  for row in csvreader:
    x.append(float(row[3]))

Y_data=np.array(x[:1500])
print(Y_data.shape)
print(Y_data)<jupyter_output>(1500,)
[0. 0. 0. ... 0. 0. 0.]<jupyter_text>Because most of the time the steering angle is 0 the y labels are mostly zero thus we need to remove this bias from the data. So we will take only few random iamges with y label = 0<jupyter_code>X=col_images[Y_data!=0]
Y=Y_data[Y_data!=0]
A=col_images[Y_data==0]
B=Y_data[Y_data==0]
number_of_rows = B.shape[0]
random_indices = np.random.choice(number_of_rows, size=150, replace=False)
X=np.concatenate((X,A[random_indices, :]),axis=0)
Y=np.concatenate((Y,B[random_indices]),axis=0)
print(X.shape)
print(Y.shape)<jupyter_output>(455, 75, 320, 3)
(455,)<jupyter_text>Steering angle y is between -1 to 1<jupyter_code># mean=Y.mean(axis=0)
# st=Y.std(axis=0)
# print(mean)
# print(st)
# Y=(Y-mean)/st
print(min(Y),max(Y))
print(X.shape)
print(Y.shape)
# specify frame index
idx = 1
cv2_imshow(X[idx])<jupyter_output><empty_output><jupyter_text>Lets prepare the Deep Learning model<jupyter_code>from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten,MaxPooling2D
#create model
model = Sequential()
#add model layers
model.add(Conv2D(128, kernel_size=3, activation="relu", input_shape=(75,320,3)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, kernel_size=3, activation="relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, kernel_size=3, activation="relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, kernel_size=3, activation="relu"))
model.add(Flatten())
model.add(Dense(50, activation="tanh"))
model.add(Dense(10, activation="tanh"))
model.add(Dense(1, activation="tanh"))
model.summary()
model.compile(loss = "mean_squared_error", optimizer='adam')
model.fit(X, Y, epochs=200,shuffle=True)
test=X[101] #101 is the index of the image we are testing, you can put any value upto 455
cv2_imshow(test)
test = np.expand_dims(test, axis=0) # to make it look like a batch we added a new dimension so that we can easily use model.predict()
test.shape
model.predict(test)[0][0]   # predicts the steering angle
test=X[100:105]  # lets see the same over a batch of 5 images
print(model.predict(test))
print(Y[100:105])  # actual steering angles when user was driving the car while collecting the dataset<jupyter_output>[[-0.545344  ]
 [-0.23799975]
 [ 0.4086814 ]
 [ 0.1610203 ]
 [-0.5353438 ]]
[-0.7843707  -0.04646092  0.804567    0.09961647 -0.5205054 ]<jupyter_text>Let import our test data (images + steering angle) and see how well it predicts the steering angle<jupyter_code># get file names of frames
col_frames = os.listdir('/content/drive/My Drive/SDC/testing/IMG')
# col_frames.sort(key=lambda f: int(re.sub('\D', '', f)))
col_frames.sort()
# print(col_frames)
# load frames
test=[]
for i in col_frames[:1500]:
    img = cv2.imread('/content/drive/My Drive/SDC/IMG/'+i)
    test.append(img)
test=np.array(test)
print(test.shape)
# write your code here
import csv
with open('/content/drive/My Drive/SDC/testing/driving_log.csv') as file:
  csvreader=csv.reader(file)
  x=[]
  for row in csvreader:
    x.append(float(row[3]))

Y_data_test=np.array(x[:1500])
print(Y_data_test.shape)
print(Y_data_test)
predict=list(model.predict(test)[:,0])
print(len(predict))<jupyter_output>1500<jupyter_text>Now lets start predicting on test data and then visualize the results using a arrow showing the direction, we need to turn and saving it in a video, frame by frame.<jupyter_code>import math
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter('/content/drive/My Drive/SDC/output7.mp4',fourcc, 15, (320,160))
cnt=0
for pred,img1,y in zip(predict,test,Y_data_test):
  img=img1.copy()
  start_point=(150,150)
  end_point=(int(150+pred*40),110)
  thickness = 5
  img = cv2.arrowedLine(img, start_point, end_point,(0,255,0),thickness, tipLength = 0.5)
  # font
  font = cv2.FONT_HERSHEY_SIMPLEX
  # org
  org1 = (200, 60) 
  org2 = (200, 40) 
  # fontScale
  fontScale = 0.5
  # Blue color in BGR
  color1 = (0, 255, 0)
  color2 = (0, 0, 255)
  # Line thickness of 2 px
  thickness = 2
  # Using cv2.putText() method
  cv2.putText(img, str(pred), org1, font, 
                    fontScale, color1, thickness, cv2.LINE_AA)
  # Using cv2.putText() method
  cv2.putText(img, str(y), org2, font, 
                    fontScale, color2, thickness, cv2.LINE_AA)


  cv2_imshow(img)
  # cv2.imwrite('/content/drive/My Drive/SDC/detected_dl/'+str(cnt)+'.png',img)
  
  # except TypeError: 
  #   cv2.imwrite('detected/'+str(cnt)+'.png',img)
  out.write(img)
  cnt+= 1

out.release()
cv2.destroyAllWindows()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14834483979465393, 'fraction_numerical': 0.05947955390334572, 'mean_word_length': 5.369785794813979, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '180313', 'license': 'Apache-2.0', 'n_tokens_mistral': 2374, 'n_tokens_neox': 2159, 'n_words': 500, 'path': 'SDC_CNN.ipynb', 'repo_name': 'Abhi10398/Self-Driving-Car-Prototype', 'size': '5649'}