Single-model object detector for traffic lights where class encodes state:
redlightyellowlightgreenlight
The detector outputs bounding boxes, class labels, and confidence scores in one pass.
traffic_light_colab_train_smoketest.ipynb: Colab-ready notebookconfig/data.yaml.example: dataset YAML templatesrc/validate_dataset.py: dataset integrity checkssrc/train.py: training entrypointsrc/evaluate.py: validation metrics entrypointsrc/infer.py: image/folder/video inference + annotated outputssrc/state_smoothing.py: temporal smoothing (NONE/RED/YELLOW/GREEN)
Expected structure:
traffic_lights/
images/
train/
val/
labels/
train/
val/
data.yaml
Class mapping:
0 = redlight1 = yellowlight2 = greenlight
Install dependencies:
pip install -r requirements.txtValidate dataset:
python src/validate_dataset.py --dataset_root /path/to/traffic_lights --strictTrain:
python src/train.py \
--data /path/to/traffic_lights/data.yaml \
--model yolo8n.pt \
--imgsz 640 \
--batch 16 \
--epochs 50 \
--project runs/tl \
--name yolo8n_traffic_lightsEvaluate:
python src/evaluate.py \
--weights runs/tl/yolo8n_traffic_lights/weights/best.pt \
--data /path/to/traffic_lights/data.yamlInfer (best detection only, with optional ROI):
python src/infer.py \
--weights runs/tl/yolo8n_traffic_lights/weights/best.pt \
--source /path/to/images_or_video \
--conf 0.35 \
--best_only \
--roi 0,0,1,0.6 \
--save_dir outputs- Upload this repo to GitHub.
- Open
traffic_light_colab_train_smoketest.ipynbin Colab. - Enable GPU runtime.
- In the Kaggle setup cell, set
DATASET_SLUGto your dataset. - Upload
kaggle.jsonwhen prompted. - Run all cells top-to-bottom.
- Smoke test cell performs dataset validation + 1-epoch CUDA training.
The notebook now downloads and extracts the dataset directly in Colab and auto-detects a YOLO dataset root with:
images/trainimages/vallabels/trainlabels/val
This model is vision-only. Downstream control logic should consume output and decide stop/go separately:
RED-> stop candidateYELLOW-> cautionGREEN-> continue
Combine with stop-line/map distance logic outside this detector.