1
+ import matplotlib .pyplot as plt
2
+
3
+ import numpy as np
4
+ import cv2
5
+ import os
6
+ import matplotlib .image as mpimg
7
+ from moviepy .editor import VideoFileClip
8
+ import math
9
+
10
+ def interested_region (img , vertices ):
11
+ if len (img .shape ) > 2 :
12
+ mask_color_ignore = (255 ,) * img .shape [2 ]
13
+ else :
14
+ mask_color_ignore = 255
15
+
16
+ cv2 .fillPoly (np .zeros_like (img ), vertices , mask_color_ignore )
17
+ return cv2 .bitwise_and (img , np .zeros_like (img ))
18
+
19
+ def hough_lines (img , rho , theta , threshold , min_line_len , max_line_gap ):
20
+ lines = cv2 .HoughLinesP (img , rho , theta , threshold , np .array ([]), minLineLength = min_line_len , maxLineGap = max_line_gap )
21
+ line_img = np .zeros ((img .shape [0 ], img .shape [1 ], 3 ), dtype = np .uint8 )
22
+ # print(lines)
23
+ lines_drawn (line_img ,lines )
24
+ return line_img
25
+
26
+ def lines_drawn (img , lines , color = [255 , 0 , 0 ], thickness = 6 ):
27
+ global cache
28
+ global first_frame
29
+ slope_l , slope_r = [],[]
30
+ lane_l ,lane_r = [],[]
31
+
32
+ α = 0.2
33
+
34
+
35
+ for line in lines :
36
+ for x1 ,y1 ,x2 ,y2 in line :
37
+ slope = (y2 - y1 )/ (x2 - x1 )
38
+ if slope > 0.4 :
39
+ slope_r .append (slope )
40
+ lane_r .append (line )
41
+ elif slope < - 0.4 :
42
+ slope_l .append (slope )
43
+ lane_l .append (line )
44
+ #2
45
+ img .shape [0 ] = min (y1 ,y2 ,img .shape [0 ])
46
+
47
+ # to prevent errors in challenge video from dividing by zero
48
+ if ((len (lane_l ) == 0 ) or (len (lane_r ) == 0 )):
49
+ print ('no lane detected' )
50
+ return 1
51
+
52
+ #3
53
+ slope_mean_l = np .mean (slope_l ,axis = 0 )
54
+ slope_mean_r = np .mean (slope_r ,axis = 0 )
55
+ mean_l = np .mean (np .array (lane_l ),axis = 0 )
56
+ mean_r = np .mean (np .array (lane_r ),axis = 0 )
57
+
58
+ if ((slope_mean_r == 0 ) or (slope_mean_l == 0 )):
59
+ print ('dividing by zero' )
60
+ return 1
61
+
62
+ x1_l = int ((img .shape [0 ] - mean_l [0 ][1 ] - (slope_mean_l * mean_l [0 ][0 ]))/ slope_mean_l )
63
+ x2_l = int ((img .shape [0 ] - mean_l [0 ][1 ] - (slope_mean_l * mean_l [0 ][0 ]))/ slope_mean_l )
64
+ x1_r = int ((img .shape [0 ] - mean_r [0 ][1 ] - (slope_mean_r * mean_r [0 ][0 ]))/ slope_mean_r )
65
+ x2_r = int ((img .shape [0 ] - mean_r [0 ][1 ] - (slope_mean_r * mean_r [0 ][0 ]))/ slope_mean_r )
66
+
67
+ #6
68
+ if x1_l > x1_r :
69
+ x1_l = int ((x1_l + x1_r )/ 2 )
70
+ x1_r = x1_l
71
+ y1_l = int ((slope_mean_l * x1_l ) + mean_l [0 ][1 ] - (slope_mean_l * mean_l [0 ][0 ]))
72
+ y1_r = int ((slope_mean_r * x1_r ) + mean_r [0 ][1 ] - (slope_mean_r * mean_r [0 ][0 ]))
73
+ y2_l = int ((slope_mean_l * x2_l ) + mean_l [0 ][1 ] - (slope_mean_l * mean_l [0 ][0 ]))
74
+ y2_r = int ((slope_mean_r * x2_r ) + mean_r [0 ][1 ] - (slope_mean_r * mean_r [0 ][0 ]))
75
+ else :
76
+ y1_l = img .shape [0 ]
77
+ y2_l = img .shape [0 ]
78
+ y1_r = img .shape [0 ]
79
+ y2_r = img .shape [0 ]
80
+
81
+ present_frame = np .array ([x1_l ,y1_l ,x2_l ,y2_l ,x1_r ,y1_r ,x2_r ,y2_r ],dtype = "float32" )
82
+
83
+ if first_frame == 1 :
84
+ next_frame = present_frame
85
+ first_frame = 0
86
+ else :
87
+ prev_frame = cache
88
+ next_frame = (1 - α )* prev_frame + α * present_frame
89
+
90
+ cv2 .line (img , (int (next_frame [0 ]), int (next_frame [1 ])), (int (next_frame [2 ]),int (next_frame [3 ])), color , thickness )
91
+ cv2 .line (img , (int (next_frame [4 ]), int (next_frame [5 ])), (int (next_frame [6 ]),int (next_frame [7 ])), color , thickness )
92
+
93
+ cache = next_frame
94
+
95
+
96
+ # def hough_lines(img, rho, theta, threshold, min_line_len, max_line_gap):
97
+ # lines = cv2.HoughLinesP(img, rho, theta, threshold, np.array([]), minLineLength=min_line_len, maxLineGap=max_line_gap)
98
+ # line_img = np.zeros((img.shape[0], img.shape[1], 3), dtype=np.uint8)
99
+ # lines_drawn(line_img,lines)
100
+ # return line_img
101
+
102
+ def weighted_img (img , initial_img , α = 0.8 , β = 1. , λ = 0. ):
103
+ return cv2 .addWeighted (initial_img , α , img , β , λ )
104
+
105
+
106
+ def process_image (image ):
107
+
108
+ global first_frame
109
+
110
+ gray_image = cv2 .cvtColor (image , cv2 .COLOR_BGR2GRAY )
111
+ img_hsv = cv2 .cvtColor (image , cv2 .COLOR_RGB2HSV )
112
+
113
+
114
+ lower_yellow = np .array ([20 , 100 , 100 ], dtype = "uint8" )
115
+ upper_yellow = np .array ([30 , 255 , 255 ], dtype = "uint8" )
116
+
117
+ mask_yellow = cv2 .inRange (img_hsv , lower_yellow , upper_yellow )
118
+ mask_white = cv2 .inRange (gray_image , 200 , 255 )
119
+ mask_yw = cv2 .bitwise_or (mask_white , mask_yellow )
120
+ mask_yw_image = cv2 .bitwise_and (gray_image , mask_yw )
121
+
122
+ gauss_gray = cv2 .GaussianBlur (mask_yw_image , (5 , 5 ), 0 )
123
+
124
+
125
+ canny_edges = cv2 .Canny (gauss_gray , 50 , 150 )
126
+
127
+ imshape = image .shape
128
+ lower_left = [imshape [1 ]/ 9 ,imshape [0 ]]
129
+ lower_right = [imshape [1 ]- imshape [1 ]/ 9 ,imshape [0 ]]
130
+ top_left = [imshape [1 ]/ 2 - imshape [1 ]/ 8 ,imshape [0 ]/ 2 + imshape [0 ]/ 10 ]
131
+ top_right = [imshape [1 ]/ 2 + imshape [1 ]/ 8 ,imshape [0 ]/ 2 + imshape [0 ]/ 10 ]
132
+ vertices = [np .array ([lower_left ,top_left ,top_right ,lower_right ],dtype = np .int32 )]
133
+ roi_image = interested_region (canny_edges , vertices )
134
+
135
+ theta = np .pi / 180
136
+
137
+ line_image = hough_lines (roi_image , 4 , theta , 30 , 100 , 180 )
138
+ result = weighted_img (line_image , image , α = 0.8 , β = 1. , λ = 0. )
139
+ return result
140
+
141
+ if __name__ == "__main__" :
142
+ first_frame = 1
143
+ white_output = './output.mp4'
144
+ clip1 = VideoFileClip (filename = 'test2.mp4' )
145
+ white_clip = clip1 .fl_image (process_image )
146
+ white_clip .write_videofile (white_output , audio = False )
0 commit comments