Python Project on Traffic Signs Recognition with 95% Accuracy using CNN & Keras

Free Python courses with 57 real-time projects - Learn Python

Python Project – Traffic Signs Recognition 

You must have heard about the self-driving cars in which the passenger can fully depend on the car for traveling. But to achieve level 5 autonomous, it is necessary for vehicles to understand and follow all traffic rules.

In the world of Artificial Intelligence and advancement in technologies, many researchers and big companies like Tesla, Uber, Google, Mercedes-Benz, Toyota, Ford, Audi, etc are working on autonomous vehicles and self-driving cars. So, for achieving accuracy in this technology, the vehicles should be able to interpret traffic signs and make decisions accordingly.

This is the 8th project of DataFlair’s series of 20 Python projects. I recommend you to bookmark the previous projects:

  1. Fake News Detection Python Project
  2. Parkinson’s Disease Detection Python Project
  3. Color Detection Python Project
  4. Speech Emotion Recognition Python Project
  5. Breast Cancer Classification Python Project
  6. Age and Gender Detection Python Project
  7. Handwritten Digit Recognition Python Project
  8. Chatbot Python Project
  9. Driver Drowsiness Detection Python Project
  10. Traffic Signs Recognition Python Project
  11. Image Caption Generator Python Project

What is Traffic Signs Recognition?

There are several different types of traffic signs like speed limits, no entry, traffic signals, turn left or right, children crossing, no passing of heavy vehicles, etc. Traffic signs classification is the process of identifying which class a traffic sign belongs to.

Traffic Signs Recognition – About the Python Project

In this Python project example, we will build a deep neural network model that can classify traffic signs present in the image into different categories. With this model, we are able to read and understand traffic signs which are a very important task for all autonomous vehicles.

traffic sign recognition python project idea

The Dataset of Python Project

For this project, we are using the public dataset available at Kaggle:

Traffic Signs Dataset

The dataset contains more than 50,000 images of different traffic signs. It is further classified into 43 different classes. The dataset is quite varying, some of the classes have many images while some classes have few images. The size of the dataset is around 300 MB. The dataset has a train folder which contains images inside each class and a test folder which we will use for testing our model.

Python Project on Traffic Signs Recognition Meme

Prerequisites

This project requires prior knowledge of Keras, Matplotlib, Scikit-learn, Pandas, PIL and image classification.

To install the necessary packages used for this Python data science project, enter the below command in your terminal:

pip install tensorflow keras sklearn matplotlib pandas pil

 

Want to become a pro in Python?

Check out 270+ Free Python Tutorials

Steps to Build the Python Project

To get started with the project, download and unzip the file from this link – Traffic Signs Recognition Zip File

And extract the files into a folder such that you will have a train, test and a meta folder.

Python Project Dataset

Create a Python script file and name it traffic_signs.py in the project folder.

Our approach to building this traffic sign classification model is discussed in four steps:

  • Explore the dataset
  • Build a CNN model
  • Train and validate the model
  • Test the model with test dataset

Step 1: Explore the dataset

Our ‘train’ folder contains 43 folders each representing a different class. The range of the folder is from 0 to 42. With the help of the OS module, we iterate over all the classes and append images and their respective labels in the data and labels list.

The PIL library is used to open image content into an array.

exploring dataset in python project

Finally, we have stored all the images and their labels into lists (data and labels).

We need to convert the list into numpy arrays for feeding to the model.

The shape of data is (39209, 30, 30, 3) which means that there are 39,209 images of size 30×30 pixels and the last 3 means the data contains colored images (RGB value).

With the sklearn package, we use the train_test_split() method to split training and testing data.

From the keras.utils package, we use to_categorical method to convert the labels present in y_train and t_test into one-hot encoding.

splitting dataset in python project

Step 2: Build a CNN model

To classify the images into their respective categories, we will build a CNN model (Convolutional Neural Network). CNN is best for image classification purposes.

The architecture of our model is:

  • 2 Conv2D layer (filter=32, kernel_size=(5,5), activation=”relu”)
  • MaxPool2D layer ( pool_size=(2,2))
  • Dropout layer (rate=0.25)
  • 2 Conv2D layer (filter=64, kernel_size=(3,3), activation=”relu”)
  • MaxPool2D layer ( pool_size=(2,2))
  • Dropout layer (rate=0.25)
  • Flatten layer to squeeze the layers into 1 dimension
  • Dense Fully connected layer (256 nodes, activation=”relu”)
  • Dropout layer (rate=0.5)
  • Dense layer (43 nodes, activation=”softmax”)

We compile the model with Adam optimizer which performs well and loss is “categorical_crossentropy” because we have multiple classes to categorise.

cnn model in python data science project

Steps 3: Train and validate the model

After building the model architecture, we then train the model using model.fit(). I tried with batch size 32 and 64. Our model performed better with 64 batch size. And after 15 epochs the accuracy was stable.

training the model in project in python

Our model got a 95% accuracy on the training dataset. With matplotlib, we plot the graph for accuracy and the loss.

plotting accuracy in python project example

Plotting Accuracy

accuracy & loss in python machine learning project

Accuracy and Loss Graphs

Step 4: Test our model with test dataset

Our dataset contains a test folder and in a test.csv file, we have the details related to the image path and their respective class labels. We extract the image path and labels using pandas. Then to predict the model, we have to resize our images to 30×30 pixels and make a numpy array containing all image data. From the sklearn.metrics, we imported the accuracy_score and observed how our model predicted the actual labels. We achieved a 95% accuracy in this model.

testing accuracy in advanced python project

In the end, we are going to save the model that we have trained using the Keras model.save() function.

model.save(‘traffic_classifier.h5’)

Full Source code:

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))

model.save(‘traffic_classifier.h5’)

WAIT! Have you checked our latest tutorial on OpenCV & Computer Vision

Traffic Signs Classifier GUI

Now we are going to build a graphical user interface for our traffic signs classifier with Tkinter. Tkinter is a GUI toolkit in the standard python library. Make a new file in the project folder and copy the below code. Save it as gui.py and you can run the code by typing python gui.py in the command line.

In this file, we have first loaded the trained model ‘traffic_classifier.h5’ using Keras. And then we build the GUI for uploading the image and a button is used to classify which calls the classify() function. The classify() function is converting the image into the dimension of shape (1, 30, 30, 3). This is because to predict the traffic sign we have to provide the same dimension we have used when building the model. Then we predict the class, the model.predict_classes(image) returns us a number between (0-42) which represents the class it belongs to. We use the dictionary to get the information about the class. Here’s the code for the gui.py file.

Code:

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)
    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()

Output:

graphical user interface for project in python

Summary

In this Python project with source code, we have successfully classified the traffic signs classifier with 95% accuracy and also visualized how our accuracy and loss changes with time, which is pretty good from a simple CNN model.

Time to become the next Python developer

Enroll for the Python Online Course at DataFlair now!

If you liked the Python project on traffic signs classification, do share it on social media with your friends and colleagues. For more projects like this, keep visiting DataFlair.

We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google

follow dataflair on YouTube

120 Responses

  1. sai says:

    I am getting this error can anyone help me

    Traceback (most recent call last):
    File “C:\Users\rsais\Traffic sign classification_final\Traffic sign classification\gui.py”, line 7, in
    from keras.models import load_model
    File “C:\Users\rsais\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\__init__.py”, line 20, in
    from . import initializers
    File “C:\Users\rsais\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\initializers\__init__.py”, line 124, in
    populate_deserializable_objects()
    File “C:\Users\rsais\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\initializers\__init__.py”, line 82, in populate_deserializable_objects
    generic_utils.populate_dict_with_module_objects(
    AttributeError: module ‘keras.utils.generic_utils’ has no attribute ‘populate_dict_with_module_objects’

  2. Talha says:

    what the purpose of meta folder and csv files?

  3. Talha says:

    please share the link to the video if you have

  4. neon says:

    Getting this error when running the GUI application
    C:\Users\kidcannabis420\Project>python gui.py
    2022-07-18 09:52:41.869882: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library ‘cudart64_110.dll’; dlerror: cudart64_110.dll not found
    2022-07-18 09:52:41.870217: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
    2022-07-18 09:52:51.095900: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library ‘nvcuda.dll’; dlerror: nvcuda.dll not found
    2022-07-18 09:52:51.096435: W tensorflow/stream_executor/cuda/cuda_driver.cc:263] failed call to cuInit: UNKNOWN ERROR (303)
    2022-07-18 09:52:51.127466: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: DESKTOP-OLSKNHA
    2022-07-18 09:52:51.128581: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: DESKTOP-OLSKNHA
    2022-07-18 09:52:51.131056: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX AVX2
    To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
    Someone help

  5. neon says:

    Someone who has successfully run this kindly hmu, thanks.

  6. jis says:

    while training getting error,

    Traceback (most recent call last):
    File “traffic_sign.py”, line 108, in
    #Accuracy with the test data
    AttributeError: ‘Sequential’ object has no attribute ‘predict_classes’

    can anyone help to resolve it

    did anyone got this error

  7. Nick F says:

    predoct_class has been deprecated in recent versions of Keras. Replace that line with:

    model.predict_classes(X_test)

  8. mike says:

    i faced this error after uploading a photo and clicking classify image :

    Exception in Tkinter callback
    Traceback (most recent call last):
    File “C:\Users\T\anaconda3\lib\tkinter\__init__.py”, line 1892, in __call__
    return self.func(*args)
    File “C:\Users\T\AppData\Local\Temp\ipykernel_191852\1463066544.py”, line 77, in
    classify_b=Button(top,text=”Classify Image”,command=lambda: classify(file_path),padx=10,pady=5)
    File “C:\Users\T\AppData\Local\Temp\ipykernel_191852\1463066544.py”, line 72, in classify
    sign = classes[pred+1]
    TypeError: unhashable type: ‘numpy.ndarray’

    what should i do

  9. reCAPTCHA says:

    same error showing… what to do??

  10. edwin farro says:

    Exception in Tkinter callback
    Traceback (most recent call last):
    File “C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py”, line 1921, in __call__
    return self.func(*args)
    File “e:\CURSOS UTP\SEGUNDO CICLO\INNOVACION Y TRANSFORMACION DIGITAL\PROYECTOS EN PYTHON\Reconocimiento de Señales de Tránsito\gui.py”, line 79, in
    classify_b=Button(top,text=”Clasificar imagen”,command=lambda: classify(file_path),padx=10,pady=5)
    File “e:\CURSOS UTP\SEGUNDO CICLO\INNOVACION Y TRANSFORMACION DIGITAL\PROYECTOS EN PYTHON\Reconocimiento de Señales de Tránsito\gui.py”, line 72, in classify
    pred = model.predict_classes([image])[0]
    AttributeError: ‘Sequential’ object has no attribute ‘predict_classes’

    Alguien me puede ayudar con este error

  11. mostafa says:

    ValueError: Classification metrics can’t handle a mix of multiclass and continuous-multioutput targets

  12. mima says:

    i have error in tk.Tk()

  13. Muhammad Imtiaz says:

    I’m student of Data Science and Analytics University of Hertfordshire London, Uk. This is very helpful information for me. If a data set become available so that I can apply for all things.

  14. Soumyadeep Ghosh says:

    in the above Traffic Sign Recognition Zip, there is no data set file that you mentioned like – Test, train, and meta csv files are missing.
    Please give the dataset zip file containing test, train and meta folder.

  15. Noohi Sadiya says:

    I am getting error in prediction

  16. abdel hay says:

    Exception in Tkinter callback
    Traceback (most recent call last):
    File “C:\Users\user\.conda\envs\tensorflow\lib\tkinter\__init__.py”, line 1921, in __call__
    return self.func(*args)
    File “C:\Users\user\gui.py”, line 71, in
    classify_b=Button(top,text=”Classify Image”,command=lambda: classify(file_path),padx=10,pady=5)
    File “C:\Users\user\gui.py”, line 66, in classify
    pred = model.predict_classes([image])[0]
    AttributeError: ‘Sequential’ object has no attribute ‘predict_classes’

    could you help me what should i do ?

  17. kavya says:

    how did u rectified this can u please tell me

  18. Mostafa says:

    I have an error loading images what happens the directory is right but it can’t read the images

  19. cc says:

    I am trying to run it in online notebooks and struggling to access the data set from the link provided.
    Getting error in the line :
    images = os.listdir(path)
    As there is no proper cur_path when i run in online editors.
    Can anyone help me what command to use and how to feed proper path to the code in order to access the images

  20. 杨坤 says:

    when i run the gui.py,i get the error:
    ValueError: Kernel shape must have the same length as input, but received kernel of shape (5, 5, 3, 32) and input of shape (None, None, 30, 30, 3).

  21. Prodipta says:

    GUI is not running

  22. Shama says:

    Training is done but the part of code for testing displays error such as ————————————–
    FileNotFoundError Traceback (most recent call last)
    Cell In[50], line 3
    1 from sklearn.metrics import accuracy_score
    —-> 3 y_test = pd.read_csv(‘C:/Shama/FEB_TASK/TRAFFIC_SIGN/Test.csv’)
    5 labels = y_test[“ClassId”].values
    6 imgs = y_test[“Path”].values

    File ~\anaconda3\envs\TrafficSign\Lib\site-packages\pandas\io\parsers\readers.py:1024, in read_csv(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, date_format, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, encoding_errors, dialect, on_bad_lines, delim_whitespace, low_memory, memory_map, float_precision, storage_options, dtype_backend)
    1011 kwds_defaults = _refine_defaults_read(
    1012 dialect,
    1013 delimiter,
    (…)
    1020 dtype_backend=dtype_backend,
    1021 )
    1022 kwds.update(kwds_defaults)
    -> 1024 return _read(filepath_or_buffer, kwds)

    File ~\anaconda3\envs\TrafficSign\Lib\site-packages\pandas\io\parsers\readers.py:618, in _read(filepath_or_buffer, kwds)
    615 _validate_names(kwds.get(“names”, None))
    617 # Create the parser.
    –> 618 parser = TextFileReader(filepath_or_buffer, **kwds)
    620 if chunksize or iterator:
    621 return parser

    File ~\anaconda3\envs\TrafficSign\Lib\site-packages\pandas\io\parsers\readers.py:1618, in TextFileReader.__init__(self, f, engine, **kwds)
    1615 self.options[“has_index_names”] = kwds[“has_index_names”]
    1617 self.handles: IOHandles | None = None
    -> 1618 self._engine = self._make_engine(f, self.engine)

    File ~\anaconda3\envs\TrafficSign\Lib\site-packages\pandas\io\parsers\readers.py:1878, in TextFileReader._make_engine(self, f, engine)
    1876 if “b” not in mode:
    1877 mode += “b”
    -> 1878 self.handles = get_handle(
    1879 f,
    1880 mode,
    1881 encoding=self.options.get(“encoding”, None),
    1882 compression=self.options.get(“compression”, None),
    1883 memory_map=self.options.get(“memory_map”, False),
    1884 is_text=is_text,
    1885 errors=self.options.get(“encoding_errors”, “strict”),
    1886 storage_options=self.options.get(“storage_options”, None),
    1887 )
    1888 assert self.handles is not None
    1889 f = self.handles.handle

    File ~\anaconda3\envs\TrafficSign\Lib\site-packages\pandas\io\common.py:873, in get_handle(path_or_buf, mode, encoding, compression, memory_map, is_text, errors, storage_options)
    868 elif isinstance(handle, str):
    869 # Check whether the filename is to be opened in binary mode.
    870 # Binary mode does not support ‘encoding’ and ‘newline’.
    871 if ioargs.encoding and “b” not in ioargs.mode:
    872 # Encoding
    –> 873 handle = open(
    874 handle,
    875 ioargs.mode,
    876 encoding=ioargs.encoding,
    877 errors=errors,
    878 newline=””,
    879 )
    880 else:
    881 # Binary mode
    882 handle = open(handle, ioargs.mode)

    FileNotFoundError: [Errno 2] No such file or directory: ‘C:/Shama/FEB_TASK/TRAFFIC_SIGN/Test.csv’ and as i skipped the tesing code gui ran but when i pass one image it displays different name as the symbol name, please help me resolve if not in testing part atleast for displaying correct nmae of the symbol i pass

Leave a Reply

Your email address will not be published. Required fields are marked *