Skip to content
Snippets Groups Projects
Commit 6b990991 authored by kpp55's avatar kpp55
Browse files

Merge branch 'master' of...

parents 32fbfd9c c9a27b75
Branches
No related tags found
No related merge requests found
#/!usr/bin/python #!/usr/bin/python
import numpy as np import numpy as np
#import rospy #import rospy
...@@ -35,7 +35,7 @@ def detect_edge(pic): ...@@ -35,7 +35,7 @@ def detect_edge(pic):
# pic: original image to apply the pre-set region of interest too # pic: original image to apply the pre-set region of interest too
def ROI(pic): def ROI(pic):
height = pic.shape[0] height = pic.shape[0]
triangle = np.array([[(100, height), (600, height), (350, 100)]]) triangle = np.array([[(250, height), (1100, height), (550, 250)]])
mask = np.zeros_like(pic) mask = np.zeros_like(pic)
cv2.fillPoly(mask, triangle, 255) cv2.fillPoly(mask, triangle, 255)
roi = cv2.bitwise_and(pic, mask) roi = cv2.bitwise_and(pic, mask)
...@@ -46,7 +46,7 @@ def ROI(pic): ...@@ -46,7 +46,7 @@ def ROI(pic):
# pic: original image to apply the pre-set region of interest too # pic: original image to apply the pre-set region of interest too
def ROI_real(pic): def ROI_real(pic):
height = pic.shape[0] height = pic.shape[0]
triangle = np.array([[(0, height), (620, height), (430, 250), (150, 250)]]) triangle = np.array([[(0, height), (145, 300), (475, 300), (600, height)]], dtype=np.int32)
mask = np.zeros_like(pic) mask = np.zeros_like(pic)
cv2.fillPoly(mask, triangle, 255) cv2.fillPoly(mask, triangle, 255)
roi = cv2.bitwise_and(pic, mask) roi = cv2.bitwise_and(pic, mask)
...@@ -56,7 +56,7 @@ def ROI_real(pic): ...@@ -56,7 +56,7 @@ def ROI_real(pic):
# params # params
# edge_pic: the image with the edge detection performed # edge_pic: the image with the edge detection performed
def getLines(edge_pic): def getLines(edge_pic):
return cv2.HoughLinesP(edge_pic, 1, np.pi / 180, 100, maxLineGap=80, minLineLength=20) return cv2.HoughLinesP(edge_pic, 1, np.pi / 180, 50, maxLineGap=80, minLineLength=10)
# Apply the passed in lines the the original picture # Apply the passed in lines the the original picture
# params # params
...@@ -82,6 +82,35 @@ def applyLines(original_pic, lines): ...@@ -82,6 +82,35 @@ def applyLines(original_pic, lines):
return original_pic return original_pic
def find_middle(leftPoints, rightPoints):
middle_lines = [[],[]]
if(leftPoints[1] is None or rightPoints[1] is None
):
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Caught the empty list~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
return 0
else:
print(leftPoints[1])
print(rightPoints[1])
for x in range(150):
#print("Right x point: " + str(rightPoints[0][x]))
#print("Left x point: " + str(leftPoints[0][x]))
#print("Right Y point: " + str(rightPoints[1][x]))
#print("Left Y point: " + str(leftPoints[1][x]))
midPoint = (rightPoints[0][149-x] + leftPoints[0][x]) / 2
#print("midPoint X: " + str(midPoint))
#print("midPoint Y: " + str(leftPoints[1][x]))
middle_lines[1].append(leftPoints[1][x])
middle_lines[0].append(midPoint)
return middle_lines
# Find the two average lines given the set of lines # Find the two average lines given the set of lines
# params # params
# pic: the original image # pic: the original image
...@@ -168,13 +197,13 @@ def find_average_lane(pic, lines): ...@@ -168,13 +197,13 @@ def find_average_lane(pic, lines):
for line in lines: for line in lines:
x1, y1, x2, y2 = line[0] x1, y1, x2, y2 = line[0]
parameters = np.polyfit((x1, x2), (y1, y2), 1) parameters = np.polyfit((x1, x2), (y1, y2), 1)
print("Slope, intercept") #print("Slope, intercept")
print(parameters) #print(parameters)
slope = parameters[0] slope = parameters[0]
intercept = parameters[1] intercept = parameters[1]
if(slope < 0): if(slope < 0):
print("Left insert") #print("Left insert")
left_lines.append((slope, intercept)) left_lines.append((slope, intercept))
left_lines_points[0].append(x1) left_lines_points[0].append(x1)
left_lines_points[0].append(x2) left_lines_points[0].append(x2)
...@@ -182,7 +211,7 @@ def find_average_lane(pic, lines): ...@@ -182,7 +211,7 @@ def find_average_lane(pic, lines):
left_lines_points[1].append(y2) left_lines_points[1].append(y2)
else: else:
print("Right insert") #print("Right insert")
right_lines.append((slope, intercept)) right_lines.append((slope, intercept))
right_lines_points[0].append(x1) right_lines_points[0].append(x1)
right_lines_points[0].append(x2) right_lines_points[0].append(x2)
...@@ -190,34 +219,34 @@ def find_average_lane(pic, lines): ...@@ -190,34 +219,34 @@ def find_average_lane(pic, lines):
right_lines_points[1].append(y2) right_lines_points[1].append(y2)
if not left_lines: if not left_lines:
print("Left is empty") #print("Left is empty")
left_line = [0, 0, 0, 0] left_line = [0, 0, 0, 0]
else: else:
left_average = np.average(left_lines, axis=0) left_average = np.average(left_lines, axis=0)
left_line = make_coordinates(pic, left_average) left_line = make_coordinates(pic, left_average)
print("Left Line: ") #print("Left Line: ")
print(left_line) #print(left_line)
if not right_lines: if not right_lines:
print("Right is emtpy") #print("Right is emtpy")
right_line = [0, 0, 0, 0] right_line = [0, 0, 0, 0]
else: else:
right_average = np.average(right_lines, axis=0) right_average = np.average(right_lines, axis=0)
right_line = make_coordinates(pic, right_average) right_line = make_coordinates(pic, right_average)
print("Right line : ") #print("Right line : ")
print(right_line) #print(right_line)
print("Left fit") #print("Left fit")
print(left_line) #print(left_line)
print("\nRight fit") #print("\nRight fit")
print(right_line) #print(right_line)
return np.array([left_line, right_line]) return np.array([left_line, right_line])
def make_coordinates(image, line_parameters): def make_coordinates(image, line_parameters):
print(line_parameters) #print(line_parameters)
slope, intercept = line_parameters slope, intercept = line_parameters
y1 = image.shape[0] y1 = image.shape[0]
y2 = int(y1*(1/2)) y2 = int(y1*(1/2))
...@@ -257,27 +286,29 @@ def detectDeparture(left, car, right): ...@@ -257,27 +286,29 @@ def detectDeparture(left, car, right):
#video = cv2.VideoCapture("test2.mp4") video = cv2.VideoCapture("test2.mp4")
#video = cv2.VideoCapture("highway.mp4") #video = cv2.VideoCapture("highway.mp4")
video = cv2.VideoCapture(1) #video = cv2.VideoCapture(1)
plt.ion() plt.ion()
while True: while True:
ret, frame = video.read() ret, frame = video.read()
# gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# frame = cv2.imread("../frame.jpeg")
frame_edge = detect_edge(frame) frame_edge = detect_edge(frame)
if not ret: if not ret:
video = cv2.VideoCapture(0) video = cv2.VideoCapture(1)
continue continue
new_img = formatImg(frame) new_img = formatImg(frame)
wEdges = detect_edge(new_img) wEdges = detect_edge(new_img)
cropped = ROI_real(wEdges) #cropped = ROI_real(wEdges)
cropped = ROI(wEdges)
lines = getLines(cropped) lines = getLines(cropped)
...@@ -287,14 +318,18 @@ while True: ...@@ -287,14 +318,18 @@ while True:
else: else:
Rpoints, Lpoints = find_poly_lane(new_img, lines) Rpoints, Lpoints = find_poly_lane(new_img, lines)
Mpoints = find_middle(Lpoints, Rpoints)
#(type(Rpoints[0][0])) #(type(Rpoints[0][0]))
plt.cla() plt.cla()
plt.clf() plt.clf()
plt.scatter(Rpoints[0], Rpoints[1]) plt.scatter(Rpoints[0], Rpoints[1])
plt.scatter(Lpoints[0], Lpoints[1]) plt.scatter(Lpoints[0], Lpoints[1])
plt.scatter(Mpoints[0], Mpoints[1])
plt.scatter(310, 300) #plt.scatter(310, 300)
plt.imshow(frame, zorder=0) plt.imshow(frame, zorder=0)
...@@ -305,9 +340,9 @@ while True: ...@@ -305,9 +340,9 @@ while True:
lane = applyLines(frame, lines) lane = applyLines(frame, lines)
cv2.imshow("edges", wEdges) #cv2.imshow("edges", wEdges)
cv2.imshow("cropped", cropped) #cv2.imshow("cropped", cropped)
cv2.imshow("frame", lane) #cv2.imshow("frame", lane)
key = cv2.waitKey(25) key = cv2.waitKey(25)
if key == 27: if key == 27:
...@@ -315,27 +350,3 @@ while True: ...@@ -315,27 +350,3 @@ while True:
video.release() video.release()
cv2.destroyAllWindows() cv2.destroyAllWindows()
'''
# Load image
img = cv2.imread('lol_image.jpg')
new_img = formatImg(img)
wEdges = detect_edge(new_img)
cropped = ROI(wEdges)
lines = getLines(cropped)
#lanes = applyLines(img, lines)
average_lines = find_average_lane(img, lines)
average_lanes = applyLines(img, average_lines)
cv2.imshow("edges", wEdges)
#cv2.imshow("with lines", lanes)
cv2.imshow("Average", average_lanes)
cv2.waitKey(0)
cv2.destroyAllWindows()
'''
\ No newline at end of file
from keras.models import load_model
from keras.preprocessing import image
import numpy as np
import os
from keras.applications.resnet50 import preprocess_input, decode_predictions
from keras.applications.resnet50 import ResNet50
# image folder
#folder_path = 'data/test/'
img_path = 'data/test/stop_1405371717.avi_image7.png'
# path to model
#model_path = 'resnet_stop.h5'
model = ResNet50(weights='imagenet')
model.summary()
# dimensions of images
img_width, img_height = 224, 224
# load the trained model
#model = load_model(model_path)
model = load_model(model)
model.compile(loss='sparse_categorical_crossentropy',optimizer='sgd',metrics=['accuracy'])
"""
#images = []
for img in os.listdir(folder_path):
img = image.load_img(folder_path + img, target_size=(img_width, img_height))
img = image.img_to_array(img)
img = np.expand_dims(img, axis=0)
img = preprocess_input(img)
preds = model.predict(img)
print('Predicted:', decode_predictions(preds, top=3)[0])
"""
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
preds = model.predict(x)
print('Predicted:', decode_predictions(preds, top=3)[0])
from keras import applications
from keras.preprocessing.image import ImageDataGenerator
from keras import optimizers
from keras.models import Sequential, Model
from keras.layers import Dropout, Flatten, Dense, GlobalAveragePooling2D
from keras import backend as k
from keras.callbacks import ModelCheckpoint, LearningRateScheduler, TensorBoard, EarlyStopping
img_width, img_height = 224, 224
train_data_dir = "data/train"
validation_data_dir = "data/valid"
nb_train_samples = 745
nb_validation_samples = 181
batch_size = 16
epochs = 5
model = applications.ResNet50(include_top=False, weights='imagenet', input_shape=(img_width, img_height, 3),classes=1001)
model.summary()
# Freeze the layers which you don't want to train.
for layer in model.layers[:]:
layer.trainable = False
# Adding custom Layer
# We only add
x = model.output
x = Flatten()(x)
# Adding even more custom layers
# x = Dense(1024, activation="relu")(x)
# x = Dropout(0.5)(x)
# x = Dense(1024, activation="relu")(x)
predictions = Dense(2, activation="softmax")(x)
# creating the final model
model_final = Model(model.input, predictions)
# compile the model
model_final.compile(loss = "sparse_categorical_crossentropy", optimizer="sgd", metrics=["accuracy"])
#optimizer = optimizers.SGD(lr=0.0001, momentum=0.9), metrics=["accuracy"])
# Initiate the train and test generators with data Augumentation
train_datagen = ImageDataGenerator(
rescale = 1./255,
horizontal_flip = True,
fill_mode = "nearest",
zoom_range = 0.1,
width_shift_range = 0.1,
height_shift_range=0.1,
rotation_range=10)
test_datagen = ImageDataGenerator(
rescale = 1./255,
horizontal_flip = True,
fill_mode = "nearest",
zoom_range = 0.1,
width_shift_range = 0.1,
height_shift_range=0.1,
rotation_range=10)
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size = (img_height, img_width),
batch_size = batch_size,
class_mode = "categorical",shuffle=True)
validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size = (img_height, img_width),
class_mode = "categorical", shuffle=True)
# Save the model according to the conditions
checkpoint = ModelCheckpoint("resnet_stop.h5", monitor='val_acc', verbose=1, save_best_only=True, save_weights_only=False, mode='auto', period=1)
early = EarlyStopping(monitor='val_acc', min_delta=0, patience=10, verbose=1, mode='auto')
# Train the model
model_final.fit_generator(
train_generator,
steps_per_epoch = nb_train_samples/batch_size,
epochs = epochs,
validation_data = validation_generator,
validation_steps = nb_validation_samples/batch_size,
callbacks = [checkpoint, early])
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment