i mean see this text " Implementation
5.1 Setup and Installation
5.1.1 Installing Required Libraries
The code begins by importing necessary libraries. TensorFlow (tensorflow)
is used for building and training deep learning models. Matplotlib (matplotlib)
is employed for visualizing training metrics such as accuracy and loss. Pandas
(pandas) and Seaborn (seaborn) are imported for data manipulation and
enhanced visualizations, respectively. Scikit-learn (sklearn) is utilized for model
evaluation metrics like confusion matrices and classification reports.
5.2 Data Loading and Preprocessing
Resizing and Interpolation: Images from the ’train’ and ’valid’ directories are resized to 128x128 pixels using bilinear interpolation to maintain
aspect ratio and ensure uniformity in input size for the neural network.
Color Mode and Batch Processing: Loaded in RGB color mode, images are
processed in batches of 32 during training, optimizing computational efficiency
and enabling simultaneous weight updates based on multiple samples.
1 t r a i n i n g s e t = t f . k e ra s . u t i l s . i m a g e d a t a s e t f r o m d i r e c t o r y (
2 ’ t r a i n ’ ,
3 l a b e l s=” i n f e r r e d ” ,
4 la b el mo d e=” c a t e g o r i c a l ” ,
5 colo r mode=” rgb ” ,
6 b a t c h s i z e =32,
7 i m a g e s i z e =(128 , 128 ) ,
8 s h u f f l e=True ,
9 i n t e r p o l a t i o n=” b i l i n e a r ”
13
10 )
11
12 v a l i d a t i o n s e t = t f . k e ra s . u t i l s .
i m a g e d a t a s e t f r o m d i r e c t o r y (
13 ’ v a l i d ’ ,
14 l a b e l s=” i n f e r r e d ” ,
15 la b el mo d e=” c a t e g o r i c a l ” ,
16 colo r mode=” rgb ” ,
17 b a t c h s i z e =32,
18 i m a g e s i z e =(128 , 128 ) ,
19 s h u f f l e=True ,
20 i n t e r p o l a t i o n=” b i l i n e a r ”
21 )
5.3 Model Definition
Here we are defining the model to extract spatial features from images,
followed by max pooling layers (MaxPool2D) to reduce dimensionality and
extract dominant features. Dropout layers (Dropout) are incorporated to
prevent overfitting by randomly deactivating neurons during training. The
extracted features are flattened using Flatten and passed through dense layers
(Dense) for final classification. The output layer employs a softmax activation
function (softmax) suitable for multi-class classification tasks.
22 cnn = t f . k e ra s . models . S e q u e n ti al ( [
23 t f . k e ra s . l a y e r s . Conv2D ( f i l t e r s =32, k e r n e l s i z e =3,
padding=’ same ’ , a c t i v a t i o n=’ r e l u ’ , i n p u t s ha p e =[128 ,
128 , 3 ] ) ,
24 t f . k e ra s . l a y e r s . Conv2D ( f i l t e r s =32, k e r n e l s i z e =3,
a c t i v a t i o n=’ r e l u ’ ) ,
25 t f . k e ra s . l a y e r s . MaxPool2D ( p o o l s i z e =2, s t r i d e s =2) ,
26
Department of Computer Science and Engineering 14
27 t f . k e ra s . l a y e r s . Conv2D ( f i l t e r s =128, k e r n e l s i z e =3,
padding=’ same ’ , a c t i v a t i o n=’ r e l u ’ ) ,
28 t f . k e ra s . l a y e r s . Conv2D ( f i l t e r s =128, k e r n e l s i z e =3,
a c t i v a t i o n=’ r e l u ’ ) ,
29 t f . k e ra s . l a y e r s . MaxPool2D ( p o o l s i z e =2, s t r i d e s =2) ,
30
31 t f . k e ra s . l a y e r s . Conv2D ( f i l t e r s =256, k e r n e l s i z e =3,
padding=’ same ’ , a c t i v a t i o n=’ r e l u ’ ) ,
32 t f . k e ra s . l a y e r s . Conv2D ( f i l t e r s =256, k e r n e l s i z e =3,
a c t i v a t i o n=’ r e l u ’ ) ,
33 t f . k e ra s . l a y e r s . MaxPool2D ( p o o l s i z e =2, s t r i d e s =2) ,
34
35 t f . k e ra s . l a y e r s . Conv2D ( f i l t e r s =512, k e r n e l s i z e =3,
padding=’ same ’ , a c t i v a t i o n=’ r e l u ’ ) ,
36 t f . k e ra s . l a y e r s . Conv2D ( f i l t e r s =512, k e r n e l s i z e =3,
a c t i v a t i o n=’ r e l u ’ ) ,
37 t f . k e ra s . l a y e r s . MaxPool2D ( p o o l s i z e =2, s t r i d e s =2) ,
38
39 t f . k e ra s . l a y e r s . Dropout ( 0 . 2 5 ) ,
40 t f . k e ra s . l a y e r s . Fla t t e n ( ) ,
41 t f . k e ra s . l a y e r s . Dense ( u ni t s =1500, a c t i v a t i o n=’ r e l u ’ ) ,
42 t f . k e ra s . l a y e r s . Dropout ( 0 . 4 ) ,
43 t f . k e ra s . l a y e r s . Dense ( u ni t s=n um cla s se s , a c t i v a t i o n=’
softmax ’ )
44 ] )
Department of Computer Science and Engineering 15
5.4 Model Training and Evaluation
Here we are training and Evaluating our model through epochs
46 cnn . compile ( o p timi z e r=t f . k e ra s . o p timi z e r s .Adam(
l e a r n i n g r a t e =0.0001) , l o s s=’ c a t e g o r i c a l c r o s s e n t r o p y ’ ,
47 m e t ri c s =[ ’ accu ra c y ’ ] )
48 cnn . summary ( )
49 t r a i n i n g h i s t o r y = cnn . f i t ( x=t r a i n i n g s e t , v a l i d a t i o n d a t a=
v a l i d a t i o n s e t , epoch s =10)
50 t r a i n l o s s , t r a i n a c c = cnn . e v al u a t e ( t r a i n i n g s e t )
51 pr int ( ’ T rai ni ng ac cu rac y : ’ , t r a i n a c c )
52 v a l l o s s , v a l a c c = cnn . e v al u a t e ( v a l i d a t i o n s e t )
53 pr int ( ’ V ali d a ti o n accu rac y : ’ , v a l a c c )
Figure 5.1: Model Training
Department of Computer Science and Engineering 16
Figure 5.2: Model Training
5.5 Visualization of Training History
54 import m a t pl o tli b . p y plo t a s p l t
55 epoch s = range ( 1 , 11 )
56 p l t . pl o t ( epochs , t r a i n i n g h i s t o r y . h i s t o r y [ ’ accu ra c y ’ ] ,
c o l o r=’ red ’ , l a b e l=’ T rai ni ng Accuracy ’ )
57 p l t . pl o t ( epochs , t r a i n i n g h i s t o r y . h i s t o r y [ ’ v al a c c u r a c y ’ ] ,
c o l o r=’ bl u e ’ , l a b e l=’ V ali d a ti o n Accuracy ’ )
58 p l t . x l a b e l ( ’No . of Epochs ’ )
59 p l t . t i t l e ( ’ V i s u a l i z a t i o n of Accuracy R e s ul t ’ )
60 p l t . l eg e n d ( )
61 p l t . show ( )
Figure 5.3: Visualisation of Accuracy Result
Department of Computer Science and Engineering 17
5.6 Model Evaluation Metrics
62 t e s t s e t = t f . k e ra s . u t i l s . i m a g e d a t a s e t f r o m d i r e c t o r y (
63 ’ v a l i d ’ ,
64 l a b e l s=” i n f e r r e d ” ,
65 la b el mo d e=” c a t e g o r i c a l ” ,
66 colo r mode=” rgb ” ,
67 b a t c h s i z e =1,
68 i m a g e s i z e =(128 , 128 ) ,
69 s h u f f l e=Fal se ,
70 i n t e r p o l a t i o n=” b i l i n e a r ”
71 )
72
73 # P r e d i c t c l a s s e s f o r t e s t s e t
74 y p red = cnn . p r e d i c t ( t e s t s e t )
75 p r e d i c t e d c a t e g o r i e s = t f . argmax ( y pred , a x i s =1) . numpy ( )
76 t r u e c a t e g o r i e s = t f . conca t ( [ y for x , y in t e s t s e t ] , a x i s
=0)
77 Y t rue = t f . argmax ( t r u e c a t e g o r i e s , a x i s =1) . numpy ( )
78
79 # Compute c o n f u s i o n m a t r ix and c l a s s i f i c a t i o n r e p o r t
80 cla s s nam e s = v a l i d a t i o n s e t . cl a s s nam e s
81 cm = c o nf u si o n m a t ri x ( Y true , p r e d i c t e d c a t e g o r i e s )
82 pr int ( c l a s s i f i c a t i o n r e p o r t ( Y true , p r e d i c t e d c a t e g o r i e s ,
ta rg e t nam e s=cl a s s nam e s ) )
83
84 # Pl o t c o n f u s i o n m a t r ix
85 p l t . f i g u r e ( f i g s i z e =(10 , 8 ) )
86 s n s . heatmap (cm, annot=True , annot kws={” s i z e ” : 10} , fmt=”d”
, cmap=” Blue s ” , x t i c k l a b e l s=cla s s nam e s ,
87 y t i c k l a b e l s=cla s s n am e s )
88 p l t . x l a b e l ( ’ P r e di c t e d Cla s s ’ , f o n t s i z e =12)
Department of Computer Science and Engineering 18
89 p l t . y l a b e l ( ’ Ac tual Cla s s ’ , f o n t s i z e =12)
90 p l t . t i t l e ( ’ Plan t Di s ea s e P r e di c ti o n Confu sion Matrix ’ ,
f o n t s i z e =15)
91 p l t . show ( )
92 cla s s nam e s = v a l i d a t i o n s e t . cl a s s nam e s
93
94 # Load t h e t r a i n e d model
95 cnn = t f . k e ra s . models . load model ( ’
t r a i n e d p l a n t d i s e a s e m o d e l . k e ra s ’ )
96
97 # Path t o t h e image t o be t e s t e d
98 image pa th = r ”C: \ U se r s \ g o u ri \OneDrive\Documents\Desktop<br/>pla n t d i s e a s e \ c a u l i . jpg ”
99
100 # Read and p r e p r o c e s s t h e image
101 image = t f . k e ra s . p r e p r o c e s s i n g . image . load img ( image path ,
t a r g e t s i z e =(128 , 128 ) )
102 i n p u t a r r = t f . k e ra s . p r e p r o c e s s i n g . image . im g t o a r r a y ( image
)
103 i n p u t a r r = np . a r ra y ( [ i n p u t a r r ] ) # Conver t s i n g l e image
t o a b a t c h
104
105 # Make p r e d i c t i o n s
106 p r e d i c t i o n s = cnn . p r e d i c t ( i n p u t a r r )
107
108 # P r i n t p r e d i c t i o n s
109 pr int ( p r e d i c t i o n s )
110
111 # Get t h e p r e d i c t e d c l a s s i n d e x
112 r e s u l t i n d e x = np . argmax ( p r e d i c t i o n s ) # Return i n d e x o f
max elemen t
113 pr int ( r e s u l t i n d e x )
Department of Computer Science and Engineering 19
114
115 # Get t h e c l a s s name o f t h e p r e d i c t e d c l a s s
116 m o d el p r e di c ti o n = cla s s nam e s [ r e s u l t i n d e x ]
117
118 # D i s pl a y t h e image w i t h t h e p r e d i c t i o n
119 p l t . imshow ( image )
120 p l t . t i t l e ( f ” Di s ea s e Name: { m o d el p r e di c ti o n }” )
121 p l t . x t i c k s ( [ ] )
122 p l t . y t i c k s ( [ ] )
123 p l t . show ( )
124
125 # Make p r e d i c t i o n s on t h e v a l i d a t i o n s e t
126 y p red = cnn . p r e d i c t ( v a l i d a t i o n s e t )
127 p r e d i c t e d c l a s s e s = np . argmax ( y pred , a x i s =1)
128
129 # Get t r u e c l a s s e s
130 t r u e c l a s s e s = np . co n ca t e na t e ( [ y for x , y in v a l i d a t i o n s e t
] , a x i s =0)
131 t r u e c l a s s e s = np . argmax ( t r u e c l a s s e s , a x i s =1)
132
133 # Genera te c l a s s i f i c a t i o n r e p o r t
134 r e p o r t = c l a s s i f i c a t i o n r e p o r t ( t r u e c l a s s e s ,
p r e d i c t e d c l a s s e s , ta rg e t nam e s=cla s s nam e s , o u t p u t di c t
=True )
135
136 # E x t r a c t p r e c i s i o n and r e c a l l v a l u e s
137 p r e c i s i o n v a l u e s = [ r e p o r t [ cla s s nam e ] [ ’ p r e c i s i o n ’ ] for
cla s s nam e in cl a s s nam e s ]
138 r e c a l l v a l u e s = [ r e p o r t [ cla s s nam e ] [ ’ r e c a l l ’ ] for
cla s s nam e in cl a s s nam e s ]
139
140 # Pl o t p r e c i s i o n h i s t og r am f o r each c l a s s
Department of Computer Science and Engineering 20
141 p l t . f i g u r e ( f i g s i z e =(10 , 6 ) )
142 p l t . bar ( cla s s nam e s , p r e c i s i o n v a l u e s , c o l o r=’ s k y bl u e ’ ,
alpha =0.7 , l a b e l=’ P r e c i s i o n ’ )
143 p l t . x l a b e l ( ’ Cla s s ’ )
144 p l t . y l a b e l ( ’ P r e c i s i o n ’ )
145 p l t . t i t l e ( ’ P r e c i s i o n f o r Each Cla s s ’ )
146 p l t . x t i c k s ( r o t a t i o n =45)
147 p l t . ylim ( [ 0 , 1 ] )
148 p l t . t i g h t l a y o u t ( )
149 p l t . l eg e n d ( )
150 p l t . show ( )
151
152 # Pl o t r e c a l l h i s t og r am f o r each c l a s s
153 p l t . f i g u r e ( f i g s i z e =(10 , 6 ) )
154 p l t . bar ( cla s s nam e s , r e c a l l v a l u e s , c o l o r=’ l i g h t c o r a l ’ ,
alpha =0.7 , l a b e l=’ R e c all ’ )
155 p l t . x l a b e l ( ’ Cla s s ’ )
156 p l t . y l a b e l ( ’ R e c all ’ )
157 p l t . t i t l e ( ’ R e c all f o r Each Cla s s ’ )
158 p l t . x t i c k s ( r o t a t i o n =45)
159 p l t . ylim ( [ 0 , 1 ] )
160 p l t . t i g h t l a y o u t ( )
161 p l t . l eg e n d ( )
162 p l t . show ( )
163
164 # P r i n t t h e p r e c i s i o n and r e c a l l v a l u e s f o r r e f e r e n c e
165 for cla s s name , p r e c i s i o n , r e c a l l in z ip ( cla s s nam e s ,
p r e c i s i o n v a l u e s , r e c a l l v a l u e s ) :
166 pr int ( f ’ Cla s s : { cla s s nam e } , P r e c i s i o n : { p r e c i s i o n : . 2 f
} , R e c all : { r e c a l l : . 2 f } ’ )
Department of Computer Science and Engineering 21
Figure 5.4: Model Evaluation Metrics (Confusion Matrix)
Figure 5.5: Model Evaluation Metrics (Prediction by Each Class)
Department of Computer Science and Engineering 22
Figure 5.6: Model Evaluation Metrics (Precision For Each Class)
Figure 5.7: Recall for each class
Department of Computer Science and Engineering 23
CHAPTER 6" how desciptive its about the code i wanted like this but change the structure of language you use in the text but for the code “from tkinter import messagebox
from tkinter import *
from tkinter import simpledialog
import tkinter
from tkinter import filedialog
from tkinter.filedialog import askopenfilename
import cv2 as cv
import numpy as np

import os
from keras.preprocessing.image import load_img
from keras.utils import to_categorical
from keras.preprocessing.image import img_to_array
# from sklearn.model_selection import train_test_split
import pickle
from keras.models import load_model
from keras.applications import VGG16
from keras.layers import Flatten
from keras.layers import Dropout
from keras.layers import Dense
from keras.layers import Input
from keras.models import Model
from keras.optimizers import Adam
from keras.models import model_from_json


main = tkinter.Tk()
main.title(“SSLA Based Traffic Sign and Lane Detection for Autonomous cars”)
main.geometry(“1300x1200”)

global filename
global model

old = None
class_labels = [‘Speed limit (20km/h)’,‘Speed limit (30km/h)’,‘Speed limit (50km/h)’,‘Speed limit (60km/h)’,‘Speed limit (70km/h)’,
‘Speed limit (80km/h)’,‘End of speed limit (80km/h)’,‘Speed limit (100km/h)’,‘Speed limit (120km/h)’,‘No passing’,‘Stop’,‘No Entry’,
‘General caution’,‘Traffic signals’]

def cannyDetection(img):
grayImg = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
blurImg = cv.GaussianBlur(grayImg, (5, 5), 0)
cannyImg = cv.Canny(blurImg, 50, 150)
return cannyImg

def segmentDetection(img):
height = img.shape[0]
polygons = np.array([[(0, height), (800, height), (380, 290)]])
maskImg = np.zeros_like(img)
cv.fillPoly(maskImg, polygons, 255)
segmentImg = cv.bitwise_and(img, maskImg)
return segmentImg

def calculateLines(frame, lines):
left = []
right = []
for line in lines:
x1, y1, x2, y2 = line.reshape(4)
parameters = np.polyfit((x1, x2), (y1, y2), 1)
slope = parameters[0]
y_intercept = parameters[1]
if slope < 0:
left.append((slope, y_intercept))
else:
right.append((slope, y_intercept))
left_avg = np.average(left, axis = 0)
right_avg = np.average(right, axis = 0)
left_line = calculateCoordinates(frame, left_avg)
right_line = calculateCoordinates(frame, right_avg)
return np.array([left_line, right_line])

def calculateCoordinates(frame, parameters):
global old
#print(str(parameters)+” “+str(type(parameters))+” “+str(np.isnan(parameters)))
if old is None:
old = parameters
if np.isnan(parameters.any()) == False:
parameters = old
slope, intercept = parameters
y1 = frame.shape[0]
y2 = int(y1 - 150)
x1 = int((y1 - intercept) / slope)
x2 = int((y2 - intercept) / slope)
return np.array([x1, y1, x2, y2])

def visualizeLines(frame, lines):
lines_visualize = np.zeros_like(frame)
if lines is not None:
for x1, y1, x2, y2 in lines:
cv.line(lines_visualize, (x1, y1), (x2, y2), (0, 255, 0), 5)
return lines_visualize


def loadModel():
global model
model = load_model(‘model/model.h5’)
pathlabel.config(text=“Machine Learning Traffic Sign Detection Model Loaded”)
text.delete(‘1.0’, END)
text.insert(END,“Machine Learning Traffic Sign Detection Model Loaded\n\n”);

def detectSignal():
global model
filename = filedialog.askopenfilename(initialdir=“Videos”)
pathlabel.config(text=filename)
text.delete(‘1.0’, END)
text.insert(END,filename+” loaded\n\n")
text.update_idletasks()
cap = cv.VideoCapture(filename)
while (cap.isOpened()):
ret, frame = cap.read()
if frame is not None:
canny = cannyDetection(frame)
cv.imshow(“cannyImage”, canny)
segment = segmentDetection(canny)
hough = cv.HoughLinesP(segment, 2, np.pi / 180, 100, np.array([]), minLineLength = 100, maxLineGap = 50)
if hough is not None:
lines = calculateLines(frame, hough)
linesVisualize = visualizeLines(frame, lines)
cv.imshow(“hough”, linesVisualize)
output = cv.addWeighted(frame, 0.9, linesVisualize, 1, 1)
cv.imwrite(“test.jpg”,output)
temps = cv.imread(“test.jpg”)
h, w, c = temps.shape
image = load_img(“test.jpg”, target_size=(80, 80))
image = img_to_array(image) / 255.0
image = np.expand_dims(image, axis=0)
(boxPreds, labelPreds) = model.predict(image)
print(boxPreds)
boxPreds = boxPreds[0]
startX = int(boxPreds[0] * w)
startY = int(boxPreds[1] * h)
endX = int(boxPreds[2] * w)
endY = int(boxPreds[3] * h)
predict= np.argmax(labelPreds, axis=1)
predict = predict[0]
accuracy = np.amax(labelPreds, axis=1)
print(str(class_labels[predict])+" "+str(accuracy))
if accuracy > 0.97:
cv.putText(output, "Recognized As “+str(class_labels[predict]), (startX, startY), cv.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
cv.rectangle(output, (startX, startY), (endX, endY), (0, 255, 0), 2)
cv.imshow(“output”, output)
if cv.waitKey(10) & 0xFF == ord(‘q’):
break
cap.release()
cv.destroyAllWindows()



def close():
main.destroy()


font = (‘times’, 16, ‘bold’)
title = Label(main, text=‘SSLA Based Traffic Sign and Lane Detection for Autonomous cars’,anchor=W, justify=CENTER)
title.config(bg=‘yellow4’, fg=‘white’)
title.config(font=font)
title.config(height=3, width=120)
title.place(x=0,y=5)


font1 = (‘times’, 14, ‘bold’)
upload = Button(main, text=“Generate & Load Machine Learning Model”, command=loadModel)
upload.place(x=50,y=100)
upload.config(font=font1)

pathlabel = Label(main)
pathlabel.config(bg=‘yellow4’, fg=‘white’)
pathlabel.config(font=font1)
pathlabel.place(x=50,y=150)

markovButton = Button(main, text=“Upload Video & Detect Hough Lane, Signal”, command=detectSignal)
markovButton.place(x=50,y=200)
markovButton.config(font=font1)

predictButton = Button(main, text=“Exit”, command=close)
predictButton.place(x=50,y=250)
predictButton.config(font=font1)


font1 = (‘times’, 12, ‘bold’)
text=Text(main,height=15,width=78)
scroll=Scrollbar(text)
text.configure(yscrollcommand=scroll.set)
text.place(x=450,y=100)
text.config(font=font1)


main.config(bg=‘magenta3’)
main.mainloop()
”