0% found this document useful (0 votes)
99 views4 pages

Tkinter App

This document contains code to create a graphical user interface for an automated lane detection program. It loads a pre-trained Keras model, allows the user to upload an input video, runs lane detection on the video using the model, and outputs the result. The interface allows the user to load the model, select an input video, run lane detection to draw lines on the video, and open the output video.

Uploaded by

AlexandruVlad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views4 pages

Tkinter App

This document contains code to create a graphical user interface for an automated lane detection program. It loads a pre-trained Keras model, allows the user to upload an input video, runs lane detection on the video using the model, and outputs the result. The interface allows the user to load the model, select an input video, run lane detection to draw lines on the video, and open the output video.

Uploaded by

AlexandruVlad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

import numpy as np

import cv2
from moviepy.editor import VideoFileClip
from sklearn.externals._pilutil import imresize
from tensorflow.keras.models import load_model
from tkinter import *
from tkinter import filedialog
from PIL import ImageTk, Image
from os import startfile
from os import path

# Class to average lanes with


class Lanes:
def __init__(self):
self.recent_fit = []
self.avg_fit = []

class App:
# Create the tkinter interface
master = Tk()
# Create lanes object
lanes = Lanes()

# Load Keras model


model = load_model('full_CNN_model.h5')
# Where is the input video
input_video = 'buc_video.mp4'

# Where to save the output video


output_video = 'output_buc.mp4'

def __init__(self):
self.master.title('Automated Lane Detection')
self.master.iconbitmap(

r'C:\Users\Alex\Desktop\Facultate\proiect_diploma\lane_detection\laneDetection_deep
2\interface\ico.ico')
self.master.geometry('640x420')

def main(self):
def resizer(e):
global bg1, resized_bg, new_bg
bg1 = Image.open('interface/background.jpg')

resized_bg = bg1.resize((e.width, e.height), Image.ANTIALIAS)


new_bg = ImageTk.PhotoImage(resized_bg)
canvas.create_image(0, 0, image=new_bg, anchor='nw')
frame = Frame(self.master)
frame.pack()

bg = ImageTk.PhotoImage(file='interface/background.jpg')
canvas = Canvas(self.master, width=640, height=420)
canvas.pack(fill='both', expand=True)
canvas.create_image(0, 0, image=bg, anchor='nw')

loadModelButton = Button(self.master, text='Load model',


command=self.load_model)
loadModelButton.place(x=20, y=10)

uploadVideoButton = Button(self.master, text='Upload video',


command=self.upload_video)
uploadVideoButton.place(x=100, y=10)

drawLinesButton = Button(self.master, text='Draw lines',


command=self.draw_lines)
drawLinesButton.place(x=190, y=10)

openOutputButton = Button(self.master, text='open output video',


command=self.open_output)
openOutputButton.place(x=450, y=10)

self.master.bind('<Configure>', resizer)
self.master.mainloop()

def open_output(self):
if path.exists(self.output_video):
startfile(self.output_video)
else:
var = StringVar()
label = Label(self.master, textvariable=var, font='Helvetica 15 bold')
var.set('The file does not exist!')
label.place(x=self.master.winfo_width() / 4,
y=self.master.winfo_height() - 100)

label.after(2000, label.destroy)

def load_model(self):
file = filedialog.askopenfilename()
var = StringVar()
label = Label(self.master, textvariable=var, font='Helvetica 15 bold')

if file.endswith('.h5'):
var.set('Model loaded successfully!')
label.place(x=self.master.winfo_width() / 4,
y=self.master.winfo_height() - 100)
self.model = load_model(file)
else:
var.set('Model extension incorrect!')
label.place(x=self.master.winfo_width() / 4,
y=self.master.winfo_height() - 100)

label.after(2000, label.destroy)

def upload_video(self):
file = filedialog.askopenfilename()
var = StringVar()
label = Label(self.master, textvariable=var, font='Helvetica 15 bold')

if file.endswith('.mp4'):
var.set('Video uploaded successfully!')
label.place(x=self.master.winfo_width() / 4,
y=self.master.winfo_height() - 100)
self.input_video = file
else:
var.set('The video has incorrect format!\nExpected: ".mp4"')
label.place(x=self.master.winfo_width() / 4,
y=self.master.winfo_height() - 100)

label.after(3000, label.destroy)

def draw_lines(self):
var1 = StringVar()
var2 = StringVar()
label1 = Label(self.master, textvariable=var1, font='Helvetica 15 bold')
label2 = Label(self.master, textvariable=var2, font='Helvetica 10 bold')
var1.set('Please wait. The video is being processed')
label1.place(x=self.master.winfo_width() / 4, y=self.master.winfo_height()
- 100)

# Location of the input video


clip1 = VideoFileClip(self.input_video)
# Create the clip
vid_clip = clip1.fl_image(self.road_lines)
vid_clip.write_videofile(self.output_video, audio=False)

label1.destroy()

var2.set('Done! The video is ready.\nYou can open it by pressing "open


output video" or open "{}"'.format(
self.output_video))
label2.place(x=self.master.winfo_width() / 6, y=self.master.winfo_height()
- 100)
label2.after(5000, label2.destroy)

def road_lines(self, image):


""" Takes in a road image, re-sizes for the model,
predicts the lane to be drawn from the model in G color,
recreates an RGB image of a lane and merges with the
original road image.
"""

small_img = imresize(image, (80, 160, 3))


small_img = np.array(small_img)
# small_img = adjust_contrast(small_img)
small_img = small_img[None, :, :, :]

# plt.imshow(image)
# plt.show()

# Make prediction with neural network (un-normalize value by multiplying by


255)
prediction = self.model.predict(small_img)[0] * 255

# Add lane prediction to list for averaging


self.lanes.recent_fit.append(prediction)
# Only using last five for average
if len(self.lanes.recent_fit) > 5:
self.lanes.recent_fit = self.lanes.recent_fit[1:]

# Calculate average detection


self.lanes.avg_fit = np.mean(np.array([i for i in self.lanes.recent_fit]),
axis=0)

# Generate fake R & B color dimensions, stack with G


blanks = np.zeros_like(self.lanes.avg_fit).astype(np.uint8)
lane_drawn = np.dstack((blanks, self.lanes.avg_fit, blanks))

# Re-size to match the original image


lane_image = imresize(lane_drawn, image.shape)

# Merge the lane drawing onto the original image


result = cv2.addWeighted(image, 1, lane_image, 1, 0)

return result

app = App()
app.main()

You might also like