Skip to content

shield44-project/traffic_monitor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AI-Powered Traffic Congestion Prediction for Emission Reduction

A Smart Traffic Intelligence System designed to mitigate urban pollution through predictive congestion management.

This project detects and counts vehicles, fine-tunes YOLOv8 for emergency vehicles, forecasts congestion using LSTM to enable proactive emission reduction, and estimates gaseous pollutants with XGBoost.

Primary Aim

To reduce the carbon and toxic footprint of urban traffic by predicting congestion peaks before they occur, allowing for proactive traffic flow management that keeps vehicles moving and minimizes idling emissions.

Features

  • Vehicle detection with a precision-first YOLO policy: COCO/multi-class vehicle checkpoints by default, generic one-class checkpoints only when explicitly enabled.
  • Emergency vehicle detection through YOLOv8 transfer learning.
  • Vehicle counts, lane occupancy proxy, traffic density and congestion levels.
  • LSTM predictions for 5, 10 and 15 minute congestion horizons.
  • XGBoost/factor-table CO2, CO2e, CO, NOx, PM2.5, PM10, HC, VOC, SO2, CH4 and N2O emission prediction.
  • Live video, webcam and RTSP support.
  • Browser/phone camera frame capture and uploaded video analysis.
  • User-installable vehicle traffic-density YOLO .pt models.
  • Left/right lane ROI density analysis with Smooth/Heavy intensity labels.
  • SQLite persistence, REST APIs, PDF/CSV reports.
  • Dashboard with dark mode, Chart.js graphs, historical analytics and alerts.
  • Authentication with admin/viewer roles.
  • CPU fallback and CUDA GPU support when available.

Quick Start

From the repository root:

pip install -r requirements.txt
python app.py

Open http://127.0.0.1:5000.

Default login:

admin / admin123

Generate demo data for a populated dashboard:

cd traffic_ai_system
python training/generate_sample_data.py --days 7

Project Structure

traffic_ai_system/
  app.py
  config.py
  database.db
  database/
  detection/
  prediction/
  training/
  evaluation/
  models/
  datasets/
  reports/
  templates/
  static/
  tests/

Model Training

Emergency YOLO fine-tuning:

cd traffic_ai_system
python training/preprocess_yolo_dataset.py --data datasets/emergency/data.yaml
python training/train_emergency_yolo.py --data datasets/emergency/data.yaml

The script starts from pretrained YOLOv8 weights, freezes the backbone for the initial stage, fine-tunes detection layers, validates, and saves:

models/yolo/emergency/best.pt
data/emergency_yolo_metrics.json

Install an existing trusted emergency best.pt:

python training/download_emergency_model.py --url "https://your-trusted-host/best.pt"

or upload best.pt from the Model Performance page. There is no official Ultralytics emergency-vehicle checkpoint bundled with YOLOv8; the app does not simulate emergency detections when best.pt is missing.

Install a vehicle/traffic-density .pt from the Model Performance page. The app checks models in this order: models/yolo/traffic_density_best.pt, the local multi-class Smart-Traffic-Intelligence-System/best.pt, then the Ultralytics YOLO fallback. The top-view YOLOv8_Traffic_Density_Estimation checkpoint has only a generic Vehicle label, so it is no longer a default for webcam/live monitoring. To use that one-class model for controlled top-view traffic clips, set ALLOW_GENERIC_VEHICLE_CLASS=true.

The app validates model labels before installing checkpoints. Do not upload the generic traffic-density model as the emergency model; emergency detection expects ambulance, fire_truck, or police labels.

Traffic forecasting:

python training/generate_sample_data.py --days 14
python training/train_lstm.py

Emission prediction:

python training/train_xgboost.py

REST API

  • GET /api/summary
  • POST /api/live/start
  • POST /api/live/stop
  • GET /api/live/status
  • GET /api/live/frame
  • POST /api/analyze-image
  • POST /api/analyze-video
  • POST /api/analyze-browser-frame
  • POST /api/predict/traffic
  • POST /api/predict/emissions
  • GET /api/history/<table>
  • GET|POST /api/cameras

Authenticated session login is required for dashboard and API access.

Database Tables

  • vehicles
  • emergency_events
  • traffic_data
  • emissions
  • predictions
  • cameras
  • users

The schema is in database/schema.sql.

Reports

PDF report:

/reports -> Export PDF

CSV export supports vehicles, emergency_events, traffic_data, emissions and predictions.

Datasets

Supported datasets include UA-DETRAC, AI City Challenge, Roboflow emergency vehicle datasets, and custom YOLO-format datasets. See datasets/README.md.

For a detailed literature review and report-ready reference list covering YOLO traffic detection, ROI density, AI City Challenge, CityFlow, BDD100K, BMD-45, LSTM forecasting and emissions estimation, see PROJECT.md.

Testing

cd traffic_ai_system
pytest

Notes

See PROJECT.md for the detailed objectives, methodology, literature review, IEEE/paper directions, datasets used, testing-video sources, camera-feed flow, model choices, public URL options, Docker usage and deployment guidance. See datasets/README.md for dataset layout and recommended traffic video sources.

The dashboard remains usable before custom training by using transparent fallbacks for LSTM and an EPA/MOVES/EEA-style emission factor table. Emissions are engineering estimates in g/km-equivalent by detected visible vehicle class. For live frames, CO2e is the current visible-frame estimate; for uploaded videos, the report estimates g/km-equivalent emissions from unique tracked vehicle counts. Exact regulatory inventories require local MOVES/COPERT inputs such as fuel, vehicle age, temperature, road grade and drive cycle. Emergency detection requires models/yolo/emergency/best.pt for real ambulance/fire truck/police detection; the app does not simulate those detections.

About

Traffic congestion,pollution emmision level and real time monitoring system

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors