Skip to content

Commit a1eb0b5

Browse files
committed
Clearing local Storage
1 parent 5c0c35d commit a1eb0b5

File tree

3 files changed

+220
-0
lines changed

3 files changed

+220
-0
lines changed

Lane Line Detection [OPEN CV]/gui.py

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import tkinter as tk
2+
from tkinter import *
3+
import cv2
4+
from PIL import Image, ImageTk
5+
import os
6+
import numpy as np
7+
8+
9+
global last_frame1 #creating global variable
10+
last_frame1 = np.zeros((480, 640, 3), dtype=np.uint8)
11+
global last_frame2 #creating global variable
12+
last_frame2 = np.zeros((480, 640, 3), dtype=np.uint8)
13+
global cap1
14+
global cap2
15+
cap1 = cv2.VideoCapture("./test2.mp4")
16+
cap2 = cv2.VideoCapture("./test2.mp4")
17+
18+
def show_vid():
19+
if not cap1.isOpened():
20+
print("cant open the camera1")
21+
flag1, frame1 = cap1.read()
22+
frame1 = cv2.resize(frame1,(600,500))
23+
if flag1 is None:
24+
print ("Major error!")
25+
elif flag1:
26+
global last_frame1
27+
last_frame1 = frame1.copy()
28+
pic = cv2.cvtColor(last_frame1, cv2.COLOR_BGR2RGB)
29+
img = Image.fromarray(pic)
30+
imgtk = ImageTk.PhotoImage(image=img)
31+
lmain.imgtk = imgtk
32+
lmain.configure(image=imgtk)
33+
lmain.after(10, show_vid)
34+
35+
36+
def show_vid2():
37+
if not cap2.isOpened():
38+
print("cant open the camera2")
39+
flag2, frame2 = cap2.read()
40+
frame2 = cv2.resize(frame2,(600,500))
41+
if flag2 is None:
42+
print ("Major error2!")
43+
elif flag2:
44+
global last_frame2
45+
last_frame2 = frame2.copy()
46+
pic2 = cv2.cvtColor(last_frame2, cv2.COLOR_BGR2RGB)
47+
img2 = Image.fromarray(pic2)
48+
img2tk = ImageTk.PhotoImage(image=img2)
49+
lmain2.img2tk = img2tk
50+
lmain2.configure(image=img2tk)
51+
lmain2.after(10, show_vid2)
52+
53+
if __name__ == '__main__':
54+
root=tk.Tk()
55+
img = ImageTk.PhotoImage(Image.open("logo.png"))
56+
heading = Label(root,image=img, text="Lane-Line Detection")
57+
# heading.configure(background='#CDCDCD',foreground='#364156')
58+
heading.pack()
59+
heading2=Label(root,text="Lane-Line Detection",pady=20, font=('arial',45,'bold'))
60+
heading2.configure(foreground='#364156')
61+
heading2.pack()
62+
lmain = tk.Label(master=root)
63+
lmain2 = tk.Label(master=root)
64+
65+
lmain.pack(side = LEFT)
66+
lmain2.pack(side = RIGHT)
67+
root.title("Lane-line detection")
68+
root.geometry("1250x900+100+10")
69+
70+
exitbutton = Button(root, text='Quit',fg="red",command= root.destroy).pack(side = BOTTOM,)
71+
show_vid()
72+
show_vid2()
73+
root.mainloop()
74+
cap.release()
7.06 KB
Loading

Lane Line Detection [OPEN CV]/main.py

+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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

Comments
 (0)