Skip to content

About

Computer vision traffic light classifier using HSV brightness features in Python/Jupyter, with 98.65% test accuracy and no red-to-green misclassifications.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

TrafficLightClassifier

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.

Python Notebook OpenCV Status

Overview

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.

Key Features

Core Capabilities

  • Standardized preprocessing: Resizes input images to 32x32 and converts labels into one-hot encoded vectors.
  • HSV brightness feature extraction: Masks dark background pixels, center-crops the signal, and uses the HSV V channel 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.

Evaluation Highlights

  • 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.

Problem Statement

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.

Objectives

  • 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.

Results

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.

Outputs

  • 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.

Tech Stack

Languages & Tools

  • Python
  • Jupyter Notebook
  • Git and GitHub

Libraries

  • OpenCV
  • NumPy
  • Matplotlib

Project Structure

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 Guide

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

  • Dataset: Udacity traffic light image dataset used by the project notebook
  • Expected local paths: traffic_light_images/training/ and traffic_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.

Methodology

  1. Load labeled RGB traffic light images with the helper script.
  2. Standardize each image to 32x32 and one-hot encode its label.
  3. Mask dark background pixels and crop the central signal area.
  4. Convert the cropped image to HSV color space.
  5. Use the V channel to compute average brightness for the top, middle, and bottom thirds of the light.
  6. Predict red, yellow, or green from the brightest region and evaluate the result on the test set.

Pipeline

RGB Image -> Resize to 32x32 -> Dark-Pixel Masking -> Center Crop -> HSV Conversion
-> Brightness by Region -> One-Hot Label Prediction -> Accuracy and Error Analysis

Setup

Prerequisites

  • Python 3
  • Jupyter Notebook
  • OpenCV
  • NumPy
  • Matplotlib
  • Local copy of the traffic light dataset in the expected folder structure

Installation

pip install notebook opencv-python numpy matplotlib

How to Run

  1. Add the dataset folders to:
    • traffic_light_images/training/
    • traffic_light_images/test/
  2. Launch the notebook:
jupyter notebook Traffic_Light_Classifier.ipynb
  1. Run the notebook cells from top to bottom to reproduce preprocessing, feature extraction, evaluation, and misclassification analysis.

Expected Outputs

  • Standardized traffic light image previews
  • Passing one-hot encoding test
  • Final test accuracy of 0.9865319865319865
  • 4 misclassified examples out of 297
  • Passing red-as-green safety test with zero failures

Limitations

  • 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.

Future Improvements

  • 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.

About

Computer vision traffic light classifier using HSV brightness features in Python/Jupyter, with 98.65% test accuracy and no red-to-green misclassifications.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages