Skip to content

razyousuf/Road-Safety-system

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Road Safety Enhancement through Predictive and Generative AI

Overview

This project focuses on improving road safety by integrating predictive machine learning models and generative AI for real-time accident risk prediction and safety alerts. The system leverages a Random Forest model trained on historical UK accident data in Google Colab, combined with real-time data from APIs like OpenWeatherMap, HERE, and Google Maps, to predict accident-prone areas. A generative AI model (Small Language Model) provides contextual, real-time safety recommendations in both text and audio formats. FastAPI is used for the implementation of all the project components.


Features

  • Predictive Model: Random Forest classifier predicting accident severity based on historical and real-time data.
  • Generative AI: A Small Language Model (SLM) generating personalized, context-specific safety alerts in text and audio formats.
  • Real-time Data Integration: Integrates data from OpenWeatherMap, HERE, and Google Maps APIs to provide live traffic and weather updates.
  • Route-based Risk Prediction: Predicts accident risks along specific routes in real time.
  • API-based Architecture: FastAPI framework for efficient handling of model requests and generating alerts.

System Architecture

  1. Data Collection:

    • Historical UK road accident data (CSV format).
    • Real-time traffic, weather, and geographic data from OpenWeatherMap, HERE, and Google Maps APIs.
  2. Predictive AI:

    • Random Forest Classifier trained on Google Colab using class-balancing techniques and optimized hyperparameters.
  3. Generative AI:

    • Phi-3 Small Language Model (SLM) generates safety recommendations based on real-time input.
  4. API Integration:

    • Weather and traffic data from OpenWeatherMap, HERE, and Google Maps APIs are integrated to enhance real-time predictions.
  5. FastAPI Implementation:

    • All components are integrated and deployed using FastAPI for real-time interaction and safety alert generation.

Requirements

To run this project, ensure the following dependencies are installed:

pip install -r requirements.txt

Key dependencies include:

  • fastapi
  • uvicorn
  • scikit-learn==1.3.2
  • joblib
  • pandas
  • huggingface_hub
  • gtts
  • folium
  • polyline

Setup Instructions

1. Train the Predictive Model (Google Colab)

The Random Forest model is trained in Google Colab. To train the model:

  1. Upload the Road Accident Dataset to your Jupiter Notebook or Google Colab environment .
  2. Run the provided notebook to preprocess the data and train the model.
  3. Save the trained model as a .joblib and other required artifacts for use in FastAPI.

2. Clone the Repository

Download or clone the repository to your local environment and navigate to the project directory. Example code for cloning:

git clone https://github.com/razyousuf/Road-Safety-system.git
cd road-safety-system

3. Set Up API Keys

You will need API keys for:

  • OpenWeatherMap
  • HERE
  • Google Maps

Create a .env file and add your keys following the appropriate format. For instance:

OPENWEATHERMAP_API_KEY=your_openweathermap_key
HERE_API_KEY=your_here_key
GOOGLE_MAPS_API_KEY=your_google_maps_key

4. Data Exploration in Power BI

To explore the data more, you can use the Power BI desktop using a similar dashboard as below. Exploraiton And Exploration

5. Data Preprocessing

Implement the preprocessing steps taken in the RF_Model_Training_Code.ipynb to clean and prepare the data for the predictive before training the predictive model, and the utils.py script for generative modle. Google Colab And Fast API

6. Implement the Generative AI Model

To set up the generative AI model for generating alerts, initialize the Phi-3-mini model for real-time safety alert generation.

7. FastAPI Implementation

Start the FastAPI server with typing the folloing command in the environment terminal.

uvicorn main:app --reload

Usage

Access FastAPI UI Interface

To access FastAPI's user interface in your web browser, navigate to http://127.0.0.1:8000/docs after starting the server. This interface allows you to interact with the API by providing inputs such as location coordinates and receiving real-time predictions from the predictive model and safety recommendations from the generative AI.

Simply select the endpoint you want to test (e.g., /predict-from-coordinates), enter the required data, and the model will provide its predictions. See figure below:

API UI Interface

Accident Severity Prediction

To predict accident severity based on geographic coordinates and real-time data, make an API request with the necessary latitude and longitude data. The API will return a severity prediction (0, 1, or 2) and relevant risk information.

curl -X POST "http://127.0.0.1:8000/predict-from-coordinates" \
-H "Content-Type: application/json" \
-d '{"latitude": <lat>, "longitude": <lon>}'

API UI Predict

Generate Safety Alerts

To generate real-time text or audio safety alerts, make an API request with location data to receive a contextual recommendation based on the predicted risk.

curl -X POST "http://127.0.0.1:8000/generative-response" \
-H "Content-Type: application/json" \
-d '{"latitude": <lat>, "longitude": <lon>}'

API UI Safety Alert

Route Risk Prediction

For route-based accident risk prediction, provide origin and destination points, and the system will assess the accident risk along the specified route, delivering safety alerts.

curl -X POST "http://127.0.0.1:8000/route-accident-heatmap" \
-H "Content-Type: application/json" \
-d '{"origin": "<start_point>", "destination": "<end_point>"}'

API UI Predict along the route

Future Enhancements

  • V2V and V2I Data Integration: Incorporate vehicle-to-vehicle and vehicle-to-infrastructure data for improved prediction accuracy.
  • Advanced Generative Models: Implement Transformer-based models for more accurate and contextually aware safety alerts.
  • Adaptability Across Regions: Test the system in different regions and road networks for enhanced geographic adaptability.
  • User Feedback Integration: Collect real-time user feedback for continuous improvement of safety recommendations.

License

Feel free to use with reference


Acknowledgments

Thanks to:

  • Hope University for providing me access to a wide range of data sources.
  • Microsoft and HuggingFace for making the Phi-3 model publically available.
  • OpenWeatherMap, HERE, and Google Maps for making the real-time data publically available through their APIs.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published