Python Project on Traffic Signs Recognition with 95% Accuracy using CNN & Keras
Master Python with 70+ Hands-on Projects and Get Job-ready - 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:
- Fake News Detection Python Project
- Parkinson’s Disease Detection Python Project
- Color Detection Python Project
- Speech Emotion Recognition Python Project
- Breast Cancer Classification Python Project
- Age and Gender Detection Python Project
- Handwritten Digit Recognition Python Project
- Chatbot Python Project
- Driver Drowsiness Detection Python Project
- Traffic Signs Recognition Python Project
- 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.
The Dataset of Python Project
For this project, we are using the public dataset available at Kaggle:
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.
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.
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.
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.
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.
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.
Our model got a 95% accuracy on the training dataset. With matplotlib, we plot the graph for accuracy and the loss.
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.
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:
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.
Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google
Hi
Train Folder is empty ! i changed the path to Meta folder because it has 0.png
but still i get this error ::
NotADirectoryError: [Errno 20] Not a directory: ‘../input/gtsrb-german-traffic-sign/Meta/0.png’
im using koggle database in koggle compiler
check your direction.
Please make sure the file name is the path name and also make sure the file is at the same location as the current directory.
I am unble to run GUI. Please help if someone is able to run the project successfully.
I recently run it rightly. If you have any questions please post them here
Can you please tell us where exactly you are facing the problem? This would help us in solving the issue. Hoping to hear from you soon.
I got the error cannot “import name ‘to_categorical’ from ‘keras.utils”.could you plz explain once
How to add text to speech function to the result that is given by the traffic sign classification , for example , if the output is road work , then how do i convert road work to speech that is played automatically when we click the classify image button ? please help me.
You have to use NLP for speech
Hi Terence, can you help me to take graphs for others parameters than loss and accuracy?
Hi, I downloaded the mentioned zip file, but I do not see any test train and meta folder. Can anyone help me with that?
Hello,
I think you downloaded the zip file that contains Python files. Please download the dataset from the link https://www.kaggle.com/meowmeowmeowmeowmeow/gtsrb-german-traffic-sign . Hope this helps!
no data sets available pz send data set
You can find it on kaggle.com
The data set is available at kaggle. You can download it from the link https://www.kaggle.com/meowmeowmeowmeowmeow/gtsrb-german-traffic-sign .
How do you extract the image path and labels using pandas? I didn’t find anython related. I’m stuck in this, because I’d like to try other traffic signs.
You can actually form the dataframe with the image paths in one of the columns and labels in another column. If you want to test on any other data, you have can add the related information in the form of a dataframe and create a csv. This csv can be used in a smilar way used in this article. Hope I answered your question.
hello, I turn right into the project, turn left, how can I do it out loud?
hello, do you have this problem, do not translate text to voice? , can you help me if you take care of it
Hello, thank you for your nice post. Can you help me how to build GUI for object detection?
where do can i get ipynb code file?
You can download the Python files from the drive https://drive.google.com/file/d/1BGDHe6qQwrBEgnl-tXTSKo6TvDj8U3wS/view .
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”)
This part don’t work.
Error:
ValueError: With n_samples=0, test_size=0.2 and train_size=None, the resulting train set will be empty. Adjust any of the aforementioned parameters.
images = os.listdir(path)
what did you change with (path) ??
give me an example please.
This error occurs because the file from where the data is to be extracted is not found. Please check the path or location of the file. Hope this helps.
Sir I want to report of this project
FileNotFoundError Traceback (most recent call last)
in ()
17 for i in range(classes):
18 path = os.path.join(cur_path,’train’,str(i))
—> 19 images = os.listdir(path)
20 for a in images:
21 try:
FileNotFoundError: [Errno 2] No such file or directory: ‘/content/train/0’
i got this error. how to solve? thank yoou
and give me an explanation please. im newbie
Please check if the images exist in the same direcotory in your device as mentioned in the code. If not, either change the address or the path or change the location of the images. Hope this solves the issue.
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”)
i download data from below, but why i get error loading image??
It might be becasue the path needs to be chnaged. Please check the directory at which the images exist and change it accordingly. Hope this helps in solving the problem!
there is no dataset in the link provided. Also even after installing all those I get error like “cannot import name ‘to_categorical’ from ‘keras.utils’ “. Why?
Please download the dataset from the link https://www.kaggle.com/meowmeowmeowmeowmeow/gtsrb-german-traffic-sign . You can prevent getting this error by trying to import using the statement from keras.utils.np_utils import to_categorical. Hope it helps!
VALUE ERROR Traceback (most recent call last):
File “E:\Python-Project-Traffic-Sign-Classification (1)\Traffic sign classification\gui.py”, line 9, in
model = load_model(‘traffic_classifier.h5′)
File “C:\Users\ARJUN\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\saving.py”, line 261, in load_model
model = model_from_config(model_config, custom_objects=custom_objects)
File “C:\Users\ARJUN\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\saving.py”, line 335, in model_from_config
return deserialize(config, custom_objects=custom_objects)
File “C:\Users\ARJUN\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\layers\__init__.py”, line 55, in deserialize
printable_module_name=’layer’)
File “C:\Users\ARJUN\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\utils\generic_utils.py”, line 145, in deserialize_keras_object
list(custom_objects.items())))
File “C:\Users\ARJUN\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\sequential.py”, line 292, in from_config
custom_objects=custom_objects)
File “C:\Users\ARJUN\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\layers\__init__.py”, line 55, in deserialize
printable_module_name=’layer’)
File “C:\Users\ARJUN\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\utils\generic_utils.py”, line 165, in deserialize_keras_object
‘:’ + function_name)
ValueError: Unknown layer:name
I am getting this error Please help me with this.
Traceback (most recent call last):
File “C:\Users\Hritik Sharma\Desktop\Traffic sign classification\gui.py”, line 8, in
from keras.models import load_model
File “C:\Users\Hritik Sharma\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\__init__.py”, line 21, in
from tensorflow.python import tf2
File “C:\Users\Hritik Sharma\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tensorflow\__init__.py”, line 41, in
from tensorflow import module_util as _module_util
ImportError: cannot import name ‘module_util’ from partially initialized module ‘tensorflow’ (most likely due to a circular import) (C:\Users\Hritik Sharma\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tensorflow\__init__.py)
when we copy traffic signs it shows us invalid results such as a roundabout as chaildren crossing
I am using Tensor Flow 2.6.0 version .
pred = model.predict(X_test)
classes = np.argmax(pred,axis=1)
I modified the code in #testing accuracy on test dataset
to make it work.
Now when I use the gui
I am getting the following error:
Exception in Tkinter callback
Traceback (most recent call last):
File “C:\Users\Ashish G A\anaconda3\lib\tkinter\__init__.py”, line 1892, in __call__
return self.func(*args)
File “”, line 20, in
classify_b=Button(top,text=”Classify Image”,command=lambda: classify(file_path),padx=10,pady=5)
File “”, line 15, in classify
sign = classes[pred+1]
TypeError: unhashable type: ‘numpy.ndarray’
Can someone help me resolve the problem?
did you solve this problem?
i am getting the same issue when creating GUI , did you solve this error? please help
What is the Algorithm used in this?
I get this error when I run the file gui.py
File “C:\Users\Acer\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py”, line 1883, in __call__
return self.func(*args)
File “gui.py”, line 71, in
classify_b=Button(top,text=”Classify Image”,command=lambda: classify(file_path),padx=10,pady=5)
File “gui.py”, line 66, in classify
pred = model.predict_classes([image])[0]
AttributeError: ‘Sequential’ object has no attribute ‘predict_classes’
did you solve this problems??
did you solve this problem ?
thank you for the help
I train the model by using your code but when I run the GUI code and add image then it will not give any output give some exceptions when I run it
while using the GUI
AttributeError: ‘Sequential’ object has no attribute ‘predict_classes’
This error was coming and the signal output text was not shown in the textbox
i want this project from basic how to do turtioal video
for those who have this problem :
AttributeError: ‘Sequential’ object has no attribute ‘predict_classes’
solution:
in the first code change:
pred = model.predict_classes(X_test)
with:
predict_x=model.predict(X_test)
classes_x=np.argmax(predict_x,axis=1)
print(classes_x)
and in GUI code change this:
pred = model.predict_classes([image])[0]
sign = classes[pred+1]
with this:
predict_x=model.predict(image)
classes_x=numpy.argmax(predict_x,axis=1)
sign = classes[int(classes_x)+1]
which python version and tensorflow version used in this project ?
Hi, After i changed the code with this i got this error
line 93, in _check_targets
raise ValueError(
ValueError: Classification metrics can’t handle a mix of multiclass and continuous-multioutput targets
name ‘model’ is not defined. i got this error. please help