-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlane_detection.py
172 lines (124 loc) · 4.71 KB
/
lane_detection.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import cv2
import numpy as np
import matplotlib.pyplot as plt
class LaneDetection(object):
"""docstring for LaneDetection"""
def __init__(self):
pass
"""docstring for image: bgr to gray conversion"""
def channel_conversion(self, img):
conv_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
return conv_img
"""docstring for image noise reduction"""
def feature_extraction(self, img):
# image blurrring, parameters need to tune
blur = cv2.GaussianBlur(img, (5,5), 0)
# edge detection by canny
canny = cv2.Canny(blur, 50, 150)
return canny
"""docstring for ROI"""
def region_of_interest(self, img):
height = img.shape[0]
width = img.shape[1]
#print(width, height)
region_of_interest_vertices = [
(0, height), (width / 2, height / 2), (width, height)
]
triangle = np.array([region_of_interest_vertices], np.int32)
masked_image = self.mask_image(triangle, img)
roi = cv2.bitwise_and(img, masked_image)
return roi
"""docstring for roi masking"""
def mask_image(self, shape, img):
mask = np.zeros_like(img)
cv2.fillPoly(mask, shape, 255)
return mask
"""docstring for hough transform"""
def hough_transform(self, img):
lines = cv2.HoughLinesP(img, 2, np.pi/180, 100, np.array([]), minLineLength=40, maxLineGap=5)
return lines
def display_line(self, img, lines):
line_image = np.zeros_like(img)
if lines is not None:
for line in lines:
x1, y1, x2, y2 = line.reshape(4)
cv2.line(line_image, (x1, y1), (x2, y2), (255, 0, 0), 5)
return line_image
def line_coordinates(self, img, line_parameteres):
line_slope = line_parameteres[0]
line_intercept = line_parameteres[1]
y1 = img.shape[0]
y2 = int(y1 * (3 / 5))
x1 = int((y1 - line_intercept) / line_slope)
x2 = int((y2 - line_intercept) / line_slope)
return np.array([x1, y1, x2, y2])
def line_average_slope_intercept(self, img, lines):
left_fit = []
right_fit = []
for line in lines:
x1,y1,x2,y2 = line.reshape(4)
parameters = np.polyfit((x1,x2),(y1,y2),1)
slope = parameters[0]
intercept = parameters[1]
if slope < 0:
left_fit.append((slope,intercept))
else:
right_fit.append((slope, intercept))
if len(left_fit) and len(right_fit):
left_fit_average = np.average(left_fit, axis=0)
right_fit_average = np.average(right_fit, axis=0)
left_line = self.line_coordinates(img, left_fit_average)
right_line = self.line_coordinates(img, right_fit_average)
#print(left_line)
#print(right_line)
return np.array([left_line, right_line])
"""docstring for lane detection"""
def lane_detection(self, img):
# step 2 channel conversion
copy_img = np.copy(img)
gray = self.channel_conversion(copy_img)
# step 3 noise reduction
canny = self.feature_extraction(gray)
# step 4 find ROI
cropped_image = self.region_of_interest(canny)
# step 5 apply Hough transform
lines = self.hough_transform(cropped_image)
# print(lines)
line_average = self.line_average_slope_intercept(copy_img, lines)
#line_image = self.display_line(copy_img, lines)
line_image = self.display_line(copy_img, line_average)
lane = cv2.addWeighted(img, 0.8, line_image, 1, 0)
return lane
def feed_from_video(detection):
cap = cv2.VideoCapture('videos/Lane.mp4')
frame_count = 1
total_frame = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
print(total_frame)
while total_frame > frame_count:
#while cap.isOpened():
print(frame_count)
frame_count += 1
ret, frame = cap.read()
lane = detection.lane_detection(frame)
cv2.imshow("lane detection", lane)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if(frame_count == total_frame):
frame_count = 1
cap.set(cv2.CAP_PROP_POS_FRAMES, 1)
cap.release()
cv2.destroyAllWindows()
def feed_from_image(detection):
# step 1 read image
img = cv2.imread("images/test_image.jpg")
img = cv2.resize(img, (320, 320))
# step 6 display line
lane = detection.lane_detection(img)
# plt.imshow(line_image)
# plt.show()
cv2.imshow("lane detection", lane)
cv2.waitKey(0)
if __name__ == "__main__":
lane_detection = LaneDetection()
#feed_from_image(lane_detection)
feed_from_video(lane_detection)