Traffic Signs Recognition Using CNN and Keras in Python

When I first started exploring deep learning projects in Python, one of the most fascinating ideas I came across was traffic sign recognition. It felt rewarding to see a model correctly identify road signs, something that’s crucial for modern driver-assistance systems and autonomous vehicles.

In this tutorial, I’ll walk you through how I built a Traffic Signs Recognition System using CNN (Convolutional Neural Networks) and Keras in Python. I’ll explain everything from data preprocessing to model training and evaluation, all in simple, step-by-step language.

Even if you’re new to Keras or deep learning, don’t worry. I’ll break it down into clear, practical steps you can follow easily.

Traffic Sign Recognition

Traffic sign recognition isn’t just an academic project; it’s a real-world application that powers autonomous driving systems, smart traffic monitoring, and road safety analytics.

In the USA, where traffic laws and sign systems are standardized, recognizing these signs accurately can prevent accidents and improve navigation systems.

So, let’s dive into how we can build a reliable model in Python using Keras and CNN.

Dataset for Traffic Signs Recognition

For this project, I used the German Traffic Sign Recognition Benchmark (GTSRB) dataset. It’s publicly available and widely used for research and tutorials.

You can download it from Kaggle’s GTSRB dataset page. It contains more than 50,000 images of traffic signs categorized into 43 classes.

Once you’ve downloaded the dataset, extract it into a folder and note the path. We’ll use it in our Python code.

Method 1 – Traffic Signs Recognition Using CNN and Keras in Python

In this method, we’ll train a Convolutional Neural Network (CNN) using Keras. CNNs are excellent for image classification because they can automatically learn spatial hierarchies and features from the images.

Let’s start by importing the required Python libraries.

Step 1: Import Required Libraries

Before we begin, make sure you have the following Python libraries installed:

pip install tensorflow keras numpy matplotlib scikit-learn opencv-python

Now, let’s import them into our Python script.

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import os
import cv2
from sklearn.model_selection import train_test_split
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, Dropout
from tensorflow.keras.preprocessing.image import ImageDataGenerator

These libraries handle image processing, data manipulation, and deep learning model creation.

Step 2: Load and Preprocess the Dataset

We’ll now load the images, resize them, and prepare the labels for training.

data = []
labels = []
classes = 43
cur_path = 'path_to_gtsrb_dataset/Train'

for i in range(classes):
    path = os.path.join(cur_path, str(i))
    images = os.listdir(path)
    for img in images:
        try:
            image = cv2.imread(os.path.join(path, img))
            image = cv2.resize(image, (30, 30))
            data.append(image)
            labels.append(i)
        except:
            print("Error loading image:", img)

data = np.array(data)
labels = np.array(labels)

Here, we loop through all 43 classes, read each image, resize it to 30×30 pixels, and store it in arrays. This ensures our dataset is uniform and ready for training.

Step 3: Split the Dataset

Now, let’s split the dataset into training and testing sets.

X_train, X_test, y_train, y_test = train_test_split(data, labels, test_size=0.2, random_state=42)

print("Training samples:", X_train.shape[0])
print("Testing samples:", X_test.shape[0])

We’re using an 80-20 split to train and test our CNN model efficiently.

Step 4: Normalize and Encode Labels

We’ll normalize pixel values to the range [0,1] and convert labels to one-hot encoding.

X_train = X_train / 255.0
X_test = X_test / 255.0

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

Normalization helps the neural network converge faster during training.

Step 5: Build the CNN Model in Keras

Now comes the exciting part: building our CNN model using Keras.

model = Sequential()

model.add(Conv2D(32, kernel_size=(5,5), activation='relu', input_shape=(30,30,3)))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(64, kernel_size=(3,3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(43, activation='softmax'))

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

This CNN architecture includes two convolutional layers, pooling layers, and a dense layer with dropout to prevent overfitting.

Step 6: Data Augmentation

To make our model more robust, we’ll use data augmentation.

datagen = ImageDataGenerator(rotation_range=10,
                             zoom_range=0.1,
                             width_shift_range=0.1,
                             height_shift_range=0.1)
datagen.fit(X_train)

This technique helps the model generalize better by introducing slight variations in the training images.

Step 7: Train the CNN Model

Now, let’s train the model using the augmented data.

history = model.fit(datagen.flow(X_train, y_train, batch_size=32),
                    epochs=15,
                    validation_data=(X_test, y_test))

Training may take a few minutes depending on your system, but you’ll soon see the accuracy improve with each epoch.

Step 8: Evaluate the Model

Once training is complete, we can evaluate the model’s accuracy on the test data.

test_loss, test_acc = model.evaluate(X_test, y_test)
print("Test accuracy:", test_acc)

A good model should achieve over 95% accuracy on this dataset.

Step 9: Save and Load the Model

You can save your trained model for future use.

model.save('traffic_sign_recognition_model.h5')

Later, you can load it using:

from tensorflow.keras.models import load_model
model = load_model('traffic_sign_recognition_model.h5')

This is useful when deploying the model in real-world Python applications.

Step 10: Visualize the Training Results

Let’s visualize how the model performed during training.

plt.figure(1)
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('Model Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend(['train', 'validation'])
plt.show()

plt.figure(2)
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend(['train', 'validation'])
plt.show()

You can see the output in the screenshot below.

Traffic Signs Recognition Using CNN and Keras Python
Traffic Signs Recognition Using CNN and Keras

These plots help us understand whether the model is overfitting or performing consistently.

Method 2 – Use Pre-Trained Models (Transfer Learning in Python)

If you want to save time and resources, you can use transfer learning with a pre-trained model like MobileNetV2 or VGG16.

Here’s a quick example using MobileNetV2 in Python:

from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.layers import GlobalAveragePooling2D

base_model = MobileNetV2(weights='imagenet', include_top=False, input_shape=(30,30,3))
model = Sequential([
    base_model,
    GlobalAveragePooling2D(),
    Dense(128, activation='relu'),
    Dropout(0.5),
    Dense(43, activation='softmax')
])

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

Transfer learning allows you to leverage pre-trained weights, drastically reducing training time while maintaining high accuracy.

Testing the Model with Real Images

Let’s test the model with a real traffic sign image from the USA.

img = cv2.imread('test_sign.jpg')
img = cv2.resize(img, (30, 30))
img = np.expand_dims(img, axis=0)
img = img / 255.0

prediction = model.predict(img)
sign_class = np.argmax(prediction)
print("Predicted traffic sign class:", sign_class)

This step simulates how your model would behave in a real-world traffic system.

Building a Traffic Signs Recognition System using CNN and Keras in Python is a rewarding project that combines computer vision, deep learning, and real-world applications.

I’ve personally used this approach in multiple experiments, and the results have been consistently impressive. The model performs well even under varying lighting and rotation conditions, especially when data augmentation is applied.

If you’re looking to expand your deep learning portfolio, this project is an excellent addition. You can even extend it further by integrating it with OpenCV to detect and classify signs in real-time video streams.

You may also like to read:

Leave a Comment

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.