Skip to content

BOWEN2491/Image_Based-Traffic-Risk-Assessment

Repository files navigation

🚗 Image-Based Traffic Risk Assessment

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.


⭐️ Key Features

1. YOLO Object Detection

  • Detects cars, trucks, buses, pedestrians, cyclists, traffic signs, and traffic lights.
  • Provides bounding boxes and class probabilities as input for downstream modules.

2. Traffic-Light ROI Extraction & CNN Classification

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

3. Structured Feature Engineering

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]

4. Rule-Based Safety Overrides (Hard Rules)

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

5. XGBoost Risk Prediction

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

6. FastAPI Deployment

Provides a simple and robust backend:

POST /api/predict

Returns:

{
  "risk_level": 2,
  "risk_reason": "central red traffic light; high pedestrian density",
  "features_used": {...}
}

📁 Project Structure

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

⚙️ Installation

1. Clone the repository

git clone https://github.com/BOWEN2491/Image_Based-Traffic-Risk-Assessment.git
cd Image_Based-Traffic-Risk-Assessment

2. Initialize YOLO submodule

git submodule update --init --recursive

3. Set up Python environment

python -m venv .venv
.\.venv\Scripts\activate     # Windows
# or
source .venv/bin/activate    # macOS / Linux

4. Install dependencies

pip install -r requirements.txt

🚀 Usage

Run the complete risk prediction pipeline

python src/pipeline.py --image path/to/image.jpg

Start FastAPI server

uvicorn api.app:app --reload

Open:

http://127.0.0.1:8000/docs

🧠 ML Pipeline Overview

  1. YOLO object detection
  2. Traffic-light ROI extraction
  3. CNN color classification
  4. Feature engineering
  5. Hard-rule safety overrides
  6. XGBoost risk prediction
  7. Risk explanation

📜 License

This project includes YOLO via git submodule.
Please follow Ultralytics licensing terms when redistributing related components.


✨ Author

Bowen

About

Practical ML Project AU25

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors