Traffic Signs Classification using Deep Learning & Flask

Updated: Sep 16

Hello readers, I am back with another Machine Learning project. In this project, we are going to learn about Traffic Signs Recognition. So let's see how we can do it using Python. So let's create a project from scratch.






So we need to first download the dataset.


Let's Start the code.

  • Create the working directory, extract the dataset into it.

  • Open Jupyter notebook ( I have used Anaconda 3.7)

  • Create a new python ipynb file.

Now import the necessary library.



import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import cv2import tensorflow as tf
from PIL import Image
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
import os
os.chdir('D:/Traffic_Sign_Recognition')

We have more than 50K images for the 43 classes of traffic signs. Now we need to process all those images.



data =[]
labels = []
classes =43 
cur_path = os.getcwd()
for i inrange(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)) 
            # Resizing all images into 30*30                                                 
            image =np.array(image)             
            data.append(image)             
            labels.append(i)
        except Exceptionas e:
            print(e)

Now, we need to convert the image data list into NumPy array.


data = np.array(data) 
labels = np.array(labels)
print(data.shape, labels.shape) 
# (78418,30,30,3)(78418,)

Now we need to split the data into training and testing.

X_train, X_test, y_train, y_test =train_test_split(data, labels, test_size=0.2, random_state=0)
print(X_train.shape, X_test.shape, y_train.shape, y_test.shape) 
# (62734,30,30,3)(15684,30,30,3)(62734,)(15684,)

Now convert labels into one hot encoding.

y_train = to_categorical(y_train,43) 
y_test = to_categorical(y_test,43)

Now, it's time to build 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))
# We have 43 classes that's why we have defined 43in the dense model.add(Dense(43, activation='softmax'))

Let's compile & train the model.

#Compilation of the model 
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) 
epochs = 20 
history = model.fit(X_train, y_train, batch_size=32, epochs=epochs, validation_data=(X_test, y_test))

The last epoch r