A full-stack, modular system that predicts traffic risk levels from a single image.
Combines YOLO detection, traffic-light CNN classification, structured feature engineering, hard-rule safety overrides, and an XGBoost risk model, wrapped in a FastAPI backend for real-time inference.
- Detects cars, trucks, buses, pedestrians, cyclists, traffic signs, and traffic lights.
- Provides bounding boxes and class probabilities as input for downstream modules.
- Automatically extracts traffic-light regions from YOLO output.
- Classifies each ROI as red / yellow / green / unknown.
- Identifies the central traffic light (closest to the image center) for rule-based safety logic.
Converts perception outputs into ML-ready numerical features, including:
- Pedestrian & vehicle counts
- Object sizes, normalized distances, centrality
- Occlusion / overlap metrics
- Scene density indicators
- One-hot central traffic-light color representation
- All values normalized to [0,1]
A deterministic safety layer applied before/after ML prediction:
- Central red light → immediate high risk
- Central yellow + dense scene → escalate
- Non-central red count boosting
- Overrides low-confidence model outputs to ensure safety
Trained on weak labels derived from structured heuristics:
- Predicts risk_level ∈ {0 = low, 1 = medium, 2 = high}
- Produces human-readable explanations showing top contributing features
- Incorporates traffic-light modulation logic
Provides a simple and robust backend:
POST /api/predict
Returns:
{
"risk_level": 2,
"risk_reason": "central red traffic light; high pedestrian density",
"features_used": {...}
}autonomous-risk-assessment/
│
├── models/
│ └── risk_xgb.pkl # Trained XGBoost classifier
│
├── src/
│ ├── pipeline.py # Full inference pipeline
│ ├── build_features.py # Feature generation logic
│ ├── predict_tl_color.py # Traffic-light CNN inference
│ ├── train_tl_cnn.py # CNN training script
│ ├── hard_rules.py # Rule-based overrides
│ ├── third_party/
│ │ └── ultralytics/ # YOLO (git submodule)
│ └── ...
│
├── api/
│ └── app.py # FastAPI backend
│
├── requirements.txt
├── README.md
└── .gitignore
git clone https://github.com/BOWEN2491/Image_Based-Traffic-Risk-Assessment.git
cd Image_Based-Traffic-Risk-Assessmentgit submodule update --init --recursivepython -m venv .venv
.\.venv\Scripts\activate # Windows
# or
source .venv/bin/activate # macOS / Linuxpip install -r requirements.txtpython src/pipeline.py --image path/to/image.jpguvicorn api.app:app --reloadOpen:
http://127.0.0.1:8000/docs
- YOLO object detection
- Traffic-light ROI extraction
- CNN color classification
- Feature engineering
- Hard-rule safety overrides
- XGBoost risk prediction
- Risk explanation
This project includes YOLO via git submodule.
Please follow Ultralytics licensing terms when redistributing related components.
Bowen