-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode
More file actions
123 lines (108 loc) · 3.79 KB
/
code
File metadata and controls
123 lines (108 loc) · 3.79 KB
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
import cv2
import numpy as np
left_fit_history = []
right_fit_history = []
def canny(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5, 5), 0)
edges = cv2.Canny(blur, 50, 150)
return edges
def region_of_interest(image):
height = image.shape[0]
polygons = np.array([
[(100, height), (1200, height), (600, 250)]
])
mask = np.zeros_like(image)
cv2.fillPoly(mask, polygons, 255)
masked = cv2.bitwise_and(image, mask)
return masked
def average_lines(image, lines):
left_lines = []
right_lines = []
for line in lines:
x1, y1, x2, y2 = line.reshape(4)
if x1 == x2: # avoid division by zero
continue
slope = (y2 - y1) / (x2 - x1)
intercept = y1 - slope * x1
if slope < 0:
left_lines.append((slope, intercept))
else:
right_lines.append((slope, intercept))
left_fit = np.average(left_lines, axis=0) if left_lines else None
right_fit = np.average(right_lines, axis=0) if right_lines else None
return left_fit, right_fit
def make_coordinates(image, fit):
if fit is None:
return None
slope, intercept = fit
y1 = image.shape[0]
y2 = int(y1 * (3/5))
x1 = int((y1 - intercept) / slope)
x2 = int((y2 - intercept) / slope)
return np.array([x1, y1, x2, y2])
def smooth_lines(new_fit, history, n=5):
if new_fit is not None:
history.append(new_fit)
if len(history) > n:
history.pop(0)
if history:
return np.mean(history, axis=0)
else:
return None
def display_lines(image, lines):
line_image = np.zeros_like(image)
if lines is not None:
for line in lines:
if line is not None:
x1, y1, x2, y2 = line
cv2.line(line_image, (x1, y1), (x2, y2), (255, 0, 0), 10)
return line_image
def fill_lane(image, left_line, right_line):
if left_line is None or right_line is None:
return np.zeros_like(image)
lane_area = np.zeros_like(image)
points = np.array([
[
(left_line[0], left_line[1]),
(left_line[2], left_line[3]),
(right_line[2], right_line[3]),
(right_line[0], right_line[1])
]
])
cv2.fillPoly(lane_area, points, (0, 255, 0)) # Fill between lanes with green
return lane_area
def process_frame(frame):
global left_fit_history, right_fit_history
canny_image = canny(frame)
cropped_image = region_of_interest(canny_image)
lines = cv2.HoughLinesP(cropped_image, 2, np.pi/180, 100, np.array([]), minLineLength=40, maxLineGap=5)
if lines is not None:
left_fit, right_fit = average_lines(frame, lines)
# Smooth the lines
left_fit = smooth_lines(left_fit, left_fit_history)
right_fit = smooth_lines(right_fit, right_fit_history)
left_line = make_coordinates(frame, left_fit)
right_line = make_coordinates(frame, right_fit)
line_image = display_lines(frame, [left_line, right_line])
lane_area = fill_lane(frame, left_line, right_line)
# Overlay lane area first, then lane lines
combo_image = cv2.addWeighted(frame, 0.8, lane_area, 0.3, 1)
combo_image = cv2.addWeighted(combo_image, 1, line_image, 1, 0)
return combo_image
else:
return frame
def main():
cap = cv2.VideoCapture('C:/Users/anjus/Downloads/BB_a0e6b9fa-7a55-4662-96d6-43334b6ff017.mp4') # replace with 0 for webcam
while(cap.isOpened()):
ret, frame = cap.read()
if not ret:
break
processed_frame = process_frame(frame)
cv2.imshow('Advanced Lane Detection', processed_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()