Skip to content

Muhammad-Waleed381/SafePath

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ›‘οΈ SafePath - Intelligent Street-Level Safety Prediction & Route Planning

Python License ML API

SafePath is an AI-powered urban navigation system that predicts street-level crime risk and generates optimized safe routes for pedestrians. By combining machine learning with geospatial data analysis, SafePath helps users make informed decisions about their routes through city streets.

🎯 Features

  • 🧠 ML-Powered Risk Prediction: XGBoost classifier achieving 0.726 ROC-AUC for street segment risk assessment
  • πŸ—ΊοΈ Multi-Route Generation: Provides "Fastest", "Balanced", and "Safest" route alternatives
  • πŸ“Š Heterogeneous Data Integration: Combines NYC Crime Data, OpenStreetMap, and POI datasets
  • ⚑ Fast API: RESTful API built with FastAPI for real-time route computation (<200ms response)
  • πŸ“± Mobile Ready: Android application with interactive map visualization
  • πŸ”„ A Pathfinding*: Modified A* algorithm with composite safety-distance cost function

πŸ“ˆ Performance Metrics

Model Accuracy F1-Macro ROC-AUC Training Time
XGBoost (Primary) 0.700 0.636 0.726 0.40s
Random Forest 0.688 0.623 0.703 2.07s
Decision Tree 0.704 0.633 0.681 0.09s
Logistic Regression 0.691 0.534 0.693 0.04s

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Android App    β”‚
β”‚  (Mobile UI)    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚ HTTP Requests
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚     FastAPI Server              β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚  XGBoost  β”‚  β”‚  NetworkX  β”‚  β”‚
β”‚  β”‚   Model   β”‚  β”‚   Graph    β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚        Data Layer               β”‚
β”‚  - Crime Data (NYC Open Data)   β”‚
β”‚  - OSM Street Network           β”‚
β”‚  - POI Features (Subway, etc)   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸš€ Quick Start

Prerequisites

  • Python 3.12+
  • pip or conda package manager
  • (Optional) Android Studio for mobile app

Installation

  1. Clone the repository
git clone https://github.com/Muhammad-Waleed381/SafePath.git
cd SafePath
  1. Create virtual environment
python -m venv safepath
source safepath/bin/activate  # On Windows: safepath\Scripts\activate
  1. Install dependencies
pip install -r requirements.txt

Data Setup

Note: The data/ folder is excluded from Git due to size constraints. You'll need to download and prepare the data locally.

  1. Download NYC Crime Data
python src/data/ingest_crime.py
  1. Extract OSM Street Network
python src/data/ingest_osm.py
  1. Fetch POI Data
python src/data/ingest_pois.py
  1. Process and Enrich Data
python src/data/map_crimes.py
python src/data/feature_engineering_poi.py
python src/data/finalize_dataset.py

Model Training

# Train and evaluate multiple models
python src/analysis/final_model_comparison.py

# Train the refined XGBoost model
python src/models/train_refined_model.py

The trained model will be saved to models/xgboost_refined.pkl.

Running the API

# Start the FastAPI server
bash run_app.sh

# The API will be available at http://0.0.0.0:8000
# API Documentation: http://0.0.0.0:8000/docs

πŸ“‘ API Usage

Health Check

curl http://localhost:8000/

Single Route (with safety weight)

curl -X POST http://localhost:8000/route \
  -H "Content-Type: application/json" \
  -d '{
    "origin": [40.7580, -73.9855],
    "destination": [40.7489, -73.9680],
    "alpha": 10.0
  }'

Multiple Route Alternatives

curl -X POST http://localhost:8000/routes \
  -H "Content-Type: application/json" \
  -d '{
    "origin": [40.7580, -73.9855],
    "destination": [40.7489, -73.9680]
  }'

Response includes:

  • πŸš€ Fastest route (low safety weight)
  • βš–οΈ Balanced route (medium safety weight)
  • πŸ›‘οΈ Safest route (high safety weight)

πŸ”¬ Technical Details

Feature Engineering (16 Features)

Topological Features:

  • Edge length, lanes, speed limit, one-way indicator
  • Betweenness centrality

POI-Based Features (Distance + Density):

  • Subway stations
  • Schools
  • Nightlife venues (bars/clubs)
  • Police stations

Synthetic Features:

  • Street light count (estimated)
  • Traffic volume (estimated)
  • Temporal lag crime count

Cost Function

Routes are optimized using a composite cost function:

Cost(edge) = length Γ— (1 + Ξ± Γ— risk_score)

where:

  • length: Physical edge length in meters
  • risk_score: XGBoost predicted probability [0, 1]
  • Ξ±: User-defined safety weight (higher = prioritize safety)

Algorithms

  • Risk Prediction: XGBoost Gradient Boosting Classifier
  • Routing: Dijkstra's shortest path with composite weights
  • Multi-Route: Yen's K-Shortest Paths algorithm

πŸ“‚ Project Structure

SafePath/
β”œβ”€β”€ data/
β”‚   β”œβ”€β”€ raw/              # Raw downloaded data (gitignored)
β”‚   └── processed/        # Processed features and graphs (gitignored)
β”œβ”€β”€ models/
β”‚   └── xgboost_refined.pkl
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ data/            # Data ingestion and processing scripts
β”‚   β”œβ”€β”€ models/          # Model training scripts
β”‚   β”œβ”€β”€ analysis/        # Evaluation and comparison scripts
β”‚   └── api/             # FastAPI application
β”œβ”€β”€ reports/             # Generated figures and CSVs
β”œβ”€β”€ notebooks/           # Jupyter notebooks for exploration
β”œβ”€β”€ requirements.txt
└── run_app.sh          # API startup script

πŸ—ΊοΈ Data Sources

πŸ“± Mobile Application

The Android application provides:

  • Interactive map interface
  • Route visualization with risk overlay
  • Origin/destination selection
  • Multi-route comparison UI
  • Offline route caching

Building the Android App

cd src/app
# Open in Android Studio
# Build > Make Project
# Run on emulator or device

🀝 Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Commit changes (git commit -m 'Add AmazingFeature')
  4. Push to branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

πŸ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • NYC Open Data for providing comprehensive crime datasets
  • OpenStreetMap contributors for detailed street network data
  • OSMnx library by Geoff Boeing for geospatial network analysis
  • XGBoost team for the gradient boosting framework

πŸ“§ Contact

Muhammad Waleed - Student ID: 455706

Project Link: https://github.com/Muhammad-Waleed381/SafePath


⚠️ Disclaimer: SafePath provides route suggestions based on historical crime data and should be used as a supplementary tool. Always remain aware of your surroundings and follow local safety guidelines.

About

SafePath is an AI-powered urban navigation project that predicts street-level crime risk and returns safer walking routes in NYC by combining XGBoost risk scoring with graph-based pathfinding in a FastAPI backend.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors