diff --git a/MachineLearning Projects/traffic sign recognition using CNN and Keras/Readme.md b/MachineLearning Projects/traffic sign recognition using CNN and Keras/Readme.md new file mode 100644 index 00000000..7c8f2987 --- /dev/null +++ b/MachineLearning Projects/traffic sign recognition using CNN and Keras/Readme.md @@ -0,0 +1,12 @@ +Download the CSV file from the following link https://www.kaggle.com/datasets/meowmeowmeowmeowmeow/gtsrb-german-traffic-sign +You've probably heard about self-driving automobiles, in which the driver is completely dependent on the vehicle for transportation. But for vehicles to reach level 5 autonomy, all traffic regulations must be understood and adhered to. +Speed limits, no entry, traffic signals, turns to the left or right, children crossing, no passage of heavy trucks, etc. are just a few examples of the various types of traffic signs. The process of determining which class a traffic sign belongs to is known as categorization of traffic signs. + +![traffic signjpg1](https://user-images.githubusercontent.com/61727479/198211059-8629caa2-e441-4c5e-9f24-d12feaf2199a.jpg) +![traffic signjpg2](https://user-images.githubusercontent.com/61727479/198211180-66c94d0e-1586-44d1-adec-ca9de1f1122c.jpg) +![traffic signjpg3](https://user-images.githubusercontent.com/61727479/198211202-d636ddd4-94ba-4e34-92ff-20f67076e084.jpg) + +Follow the instructions below to run the project:- +*install Python, jupyter notebook to run the project. +*Import the relative path of the dataset clone the project and install all the required libraries and run the ipynb file in jupyter notebook. +*Download the CSV file from the following link https://www.kaggle.com/datasets/meowmeowmeowmeowmeow/gtsrb-german-traffic-sign. diff --git a/MachineLearning Projects/traffic sign recognition using CNN and Keras/gui.py b/MachineLearning Projects/traffic sign recognition using CNN and Keras/gui.py new file mode 100644 index 00000000..571c4612 --- /dev/null +++ b/MachineLearning Projects/traffic sign recognition using CNN and Keras/gui.py @@ -0,0 +1,106 @@ +import tkinter as tk +from tkinter import filedialog +from tkinter import * +from PIL import ImageTk, Image + +import numpy +#load the trained model to classify sign +from keras.models import load_model +model = load_model('traffic_classifier.h5') + +#dictionary to label all traffic signs class. +classes = { 1:'Speed limit (20km/h)', + 2:'Speed limit (30km/h)', + 3:'Speed limit (50km/h)', + 4:'Speed limit (60km/h)', + 5:'Speed limit (70km/h)', + 6:'Speed limit (80km/h)', + 7:'End of speed limit (80km/h)', + 8:'Speed limit (100km/h)', + 9:'Speed limit (120km/h)', + 10:'No passing', + 11:'No passing veh over 3.5 tons', + 12:'Right-of-way at intersection', + 13:'Priority road', + 14:'Yield', + 15:'Stop', + 16:'No vehicles', + 17:'Veh > 3.5 tons prohibited', + 18:'No entry', + 19:'General caution', + 20:'Dangerous curve left', + 21:'Dangerous curve right', + 22:'Double curve', + 23:'Bumpy road', + 24:'Slippery road', + 25:'Road narrows on the right', + 26:'Road work', + 27:'Traffic signals', + 28:'Pedestrians', + 29:'Children crossing', + 30:'Bicycles crossing', + 31:'Beware of ice/snow', + 32:'Wild animals crossing', + 33:'End speed + passing limits', + 34:'Turn right ahead', + 35:'Turn left ahead', + 36:'Ahead only', + 37:'Go straight or right', + 38:'Go straight or left', + 39:'Keep right', + 40:'Keep left', + 41:'Roundabout mandatory', + 42:'End of no passing', + 43:'End no passing veh > 3.5 tons' } + +#initialise GUI +top=tk.Tk() +top.geometry('800x600') +top.title('Traffic sign classification') +top.configure(background='#CDCDCD') + +label=Label(top,background='#CDCDCD', font=('arial',15,'bold')) +sign_image = Label(top) + +def classify(file_path): + global label_packed + image = Image.open(file_path) + image = image.resize((30,30)) + image = numpy.expand_dims(image, axis=0) + image = numpy.array(image) + print(image.shape) + pred = model.predict_classes([image])[0] + sign = classes[pred+1] + print(sign) + label.configure(foreground='#011638', text=sign) + + +def show_classify_button(file_path): + classify_b=Button(top,text="Classify Image",command=lambda: classify(file_path),padx=10,pady=5) + classify_b.configure(background='#364156', foreground='white',font=('arial',10,'bold')) + classify_b.place(relx=0.79,rely=0.46) + +def upload_image(): + try: + file_path=filedialog.askopenfilename() + uploaded=Image.open(file_path) + uploaded.thumbnail(((top.winfo_width()/2.25),(top.winfo_height()/2.25))) + im=ImageTk.PhotoImage(uploaded) + + sign_image.configure(image=im) + sign_image.image=im + label.configure(text='') + show_classify_button(file_path) + except: + pass + +upload=Button(top,text="Upload an image",command=upload_image,padx=10,pady=5) +upload.configure(background='#364156', foreground='white',font=('arial',10,'bold')) + +upload.pack(side=BOTTOM,pady=50) +sign_image.pack(side=BOTTOM,expand=True) +label.pack(side=BOTTOM,expand=True) +heading = Label(top, text="Know Your Traffic Sign",pady=20, font=('arial',20,'bold')) +heading.configure(background='#CDCDCD',foreground='#364156') +heading.pack() +top.mainloop() diff --git a/MachineLearning Projects/traffic sign recognition using CNN and Keras/traffic_sign.py b/MachineLearning Projects/traffic sign recognition using CNN and Keras/traffic_sign.py new file mode 100644 index 00000000..37d976c0 --- /dev/null +++ b/MachineLearning Projects/traffic sign recognition using CNN and Keras/traffic_sign.py @@ -0,0 +1,110 @@ +import numpy as np +import pandas as pd +import matplotlib.pyplot as plt +import cv2 +import tensorflow as tf +from PIL import Image +import os +from sklearn.model_selection import train_test_split +from keras.utils import to_categorical +from keras.models import Sequential, load_model +from keras.layers import Conv2D, MaxPool2D, Dense, Flatten, Dropout + +data = [] +labels = [] +classes = 43 +cur_path = os.getcwd() + +#Retrieving the images and their labels +for i in range(classes): + path = os.path.join(cur_path,'train',str(i)) + images = os.listdir(path) + + for a in images: + try: + image = Image.open(path + '\\'+ a) + image = image.resize((30,30)) + image = np.array(image) + #sim = Image.fromarray(image) + data.append(image) + labels.append(i) + except: + print("Error loading image") + +#Converting lists into numpy arrays +data = np.array(data) +labels = np.array(labels) + +print(data.shape, labels.shape) +#Splitting training and testing dataset +X_train, X_test, y_train, y_test = train_test_split(data, labels, test_size=0.2, random_state=42) + +print(X_train.shape, X_test.shape, y_train.shape, y_test.shape) + +#Converting the labels into one hot encoding +y_train = to_categorical(y_train, 43) +y_test = to_categorical(y_test, 43) + +#Building the model +model = Sequential() +model.add(Conv2D(filters=32, kernel_size=(5,5), activation='relu', input_shape=X_train.shape[1:])) +model.add(Conv2D(filters=32, kernel_size=(5,5), activation='relu')) +model.add(MaxPool2D(pool_size=(2, 2))) +model.add(Dropout(rate=0.25)) +model.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu')) +model.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu')) +model.add(MaxPool2D(pool_size=(2, 2))) +model.add(Dropout(rate=0.25)) +model.add(Flatten()) +model.add(Dense(256, activation='relu')) +model.add(Dropout(rate=0.5)) +model.add(Dense(43, activation='softmax')) + +#Compilation of the model +model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) + +epochs = 15 +history = model.fit(X_train, y_train, batch_size=32, epochs=epochs, validation_data=(X_test, y_test)) +model.save("my_model.h5") + +#plotting graphs for accuracy +plt.figure(0) +plt.plot(history.history['accuracy'], label='training accuracy') +plt.plot(history.history['val_accuracy'], label='val accuracy') +plt.title('Accuracy') +plt.xlabel('epochs') +plt.ylabel('accuracy') +plt.legend() +plt.show() + +plt.figure(1) +plt.plot(history.history['loss'], label='training loss') +plt.plot(history.history['val_loss'], label='val loss') +plt.title('Loss') +plt.xlabel('epochs') +plt.ylabel('loss') +plt.legend() +plt.show() + +#testing accuracy on test dataset +from sklearn.metrics import accuracy_score + +y_test = pd.read_csv('Test.csv') + +labels = y_test["ClassId"].values +imgs = y_test["Path"].values + +data=[] + +for img in imgs: + image = Image.open(img) + image = image.resize((30,30)) + data.append(np.array(image)) + +X_test=np.array(data) + +pred = model.predict_classes(X_test) + +#Accuracy with the test data +from sklearn.metrics import accuracy_score +print(accuracy_score(labels, pred))