Traffic Signal Classification
π Overview
Train a CNN to recognise the colour of traffic lights (red, yellow, green), which can serve as the perception stage of a self-driving system.
The project has three parts - data preparation, model training, and real-time prediction: collect or download a dataset of signal images, learn the classification with a convolutional network, then hook up a camera to judge the light in real time.
π§° What you need
- Computer + camera
- Python 3.7+ environment
- pip install: opencv-python, numpy, tensorflow or pytorch
- A traffic light image dataset (public, or collected yourself)
π§ Step by step
Prepare the dataset
Collect several images of red, yellow, and green lights
Cropping the ROI to a smaller image speeds up training
Pre-process the data
Resize to a uniform size (64x64, for example) and normalise the pixels
Split into training and validation sets; data augmentation is optional
Build the CNN
A standard classification stack of convolution, pooling, and dense layers
Three output neurons for red, yellow, and green
Train the model
Compile with cross-entropy loss and Adam, then train for several epochs
Watch the validation accuracy to guard against overfitting
Evaluate the model
Measure accuracy and a confusion matrix on the validation set
Analyse misclassified samples (lighting, size)
Predict in real time
Grab a frame, pre-process it, and run the model
Take a majority vote over several frames for a more stable result
π‘ Tips
- With limited data, transfer learning (fine-tuning a pre-trained model) works much better
- Traffic lights are small in frame, so consider localising them before classifying
- A multi-frame vote is effective at suppressing jitter from single-frame mistakes