A portfolio-ready computer vision notebook that classifies traffic lights as red, yellow, or green using HSV brightness features and achieves 98.65% test accuracy with zero red-to-green errors.
This project implements a traffic light classifier for RGB images in the Udacity Self-Driving Car Nanodegree workflow. The goal is to identify whether a traffic signal is showing red, yellow, or green while meeting a strict safety requirement: red lights must never be predicted as green.
The final solution is a rule-based computer vision pipeline built in Jupyter Notebook. It standardizes each image, isolates the central traffic light region, converts the image to HSV color space, and compares brightness across the top, middle, and bottom sections of the signal. On the provided test set, the notebook reaches 98.65% accuracy and produces no red-to-green misclassifications.
- Standardized preprocessing: Resizes input images to
32x32and converts labels into one-hot encoded vectors. - HSV brightness feature extraction: Masks dark background pixels, center-crops the signal, and uses the HSV
Vchannel to measure brightness by light position. - Rule-based classification: Predicts the active light by selecting the brightest region and returns a one-hot encoded label.
- Rubric-aligned testing: Includes one-hot encoding validation and a dedicated check that no red light is classified as green.
- Misclassification analysis: Surfaces the remaining error cases directly in the notebook for visual inspection and discussion.
Autonomous driving systems and driver-assistance pipelines need reliable traffic signal recognition to make safe decisions at intersections. This project tackles a simplified but safety-critical version of that problem by classifying traffic light images into red, yellow, and green states using interpretable computer vision features instead of a trained deep learning model.
- Build a complete RGB image classifier for red, yellow, and green traffic lights.
- Achieve greater than 90% accuracy on the evaluation set.
- Ensure red lights are never misclassified as green.
| Metric | Value |
|---|---|
| Test Accuracy | 98.65% |
| Correct Predictions | 293 / 297 |
| Misclassified Images | 4 |
| Red as Green Errors | 0 |
Interpretation:
- Best system: HSV brightness region classifier
- Main takeaway: Simple spatial brightness features are sufficient to exceed the rubric threshold while preserving the key safety constraint.
- Limitation: All remaining errors are green lights predicted as red in difficult lighting conditions.
- Notebook implementation and analysis:
Traffic_Light_Classifier.ipynb - Dataset loading utilities:
helpers.py - Evaluation helpers:
test_functions.py - Reference visuals used in the notebook and documentation:
images/
The notebook includes image previews, preprocessing checks, classifier evaluation, and a visualization of the misclassified examples.
Languages & Tools
- Python
- Jupyter Notebook
- Git and GitHub
Libraries
- OpenCV
- NumPy
- Matplotlib
TrafficLightClassifier/
├── README.md
├── Traffic_Light_Classifier.ipynb
├── helpers.py
├── test_functions.py
└── images/
├── all_lights.png
├── feature_ext_steps.png
├── masked_images.png
└── processing_steps.png
| File / Folder | Purpose |
|---|---|
Traffic_Light_Classifier.ipynb |
Main notebook containing preprocessing, feature extraction, classification, evaluation, and written analysis |
helpers.py |
Loads traffic light images and labels from the expected dataset folders |
test_functions.py |
Provides rubric-oriented validation helpers for one-hot encoding and red-as-green safety checks |
images/ |
Contains reference images used in the project materials and notebook walkthrough |
- Dataset: Udacity traffic light image dataset used by the project notebook
- Expected local paths:
traffic_light_images/training/andtraffic_light_images/test/ - Labels:
red,yellow,green
The dataset folders are not included in this repository. To run the notebook, place the training and test image directories in the expected local paths shown above.
- Load labeled RGB traffic light images with the helper script.
- Standardize each image to
32x32and one-hot encode its label. - Mask dark background pixels and crop the central signal area.
- Convert the cropped image to HSV color space.
- Use the
Vchannel to compute average brightness for the top, middle, and bottom thirds of the light. - Predict red, yellow, or green from the brightest region and evaluate the result on the test set.
RGB Image -> Resize to 32x32 -> Dark-Pixel Masking -> Center Crop -> HSV Conversion
-> Brightness by Region -> One-Hot Label Prediction -> Accuracy and Error Analysis
- Python 3
- Jupyter Notebook
- OpenCV
- NumPy
- Matplotlib
- Local copy of the traffic light dataset in the expected folder structure
pip install notebook opencv-python numpy matplotlib- Add the dataset folders to:
traffic_light_images/training/traffic_light_images/test/
- Launch the notebook:
jupyter notebook Traffic_Light_Classifier.ipynb- Run the notebook cells from top to bottom to reproduce preprocessing, feature extraction, evaluation, and misclassification analysis.
- Standardized traffic light image previews
- Passing one-hot encoding test
- Final test accuracy of
0.9865319865319865 4misclassified examples out of297- Passing red-as-green safety test with zero failures
- The classifier uses fixed masking and cropping heuristics that may be brittle under unusual framing or lighting.
- It relies primarily on brightness rather than richer color semantics, which contributes to the remaining green-to-red errors.
- The current approach is designed for the provided dataset structure and does not generalize automatically to other traffic signal formats.
- Add hue- and saturation-based features to better separate red and green under difficult lighting.
- Replace the fixed background threshold with adaptive thresholding.
- Extend the approach to handle arrow-shaped lights and more varied signal geometries.