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.
- π§ 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
| 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 |
βββββββββββββββββββ
β 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) β
βββββββββββββββββββββββββββββββββββ
- Python 3.12+
- pip or conda package manager
- (Optional) Android Studio for mobile app
- Clone the repository
git clone https://github.com/Muhammad-Waleed381/SafePath.git
cd SafePath- Create virtual environment
python -m venv safepath
source safepath/bin/activate # On Windows: safepath\Scripts\activate- Install dependencies
pip install -r requirements.txtNote: The
data/folder is excluded from Git due to size constraints. You'll need to download and prepare the data locally.
- Download NYC Crime Data
python src/data/ingest_crime.py- Extract OSM Street Network
python src/data/ingest_osm.py- Fetch POI Data
python src/data/ingest_pois.py- Process and Enrich Data
python src/data/map_crimes.py
python src/data/feature_engineering_poi.py
python src/data/finalize_dataset.py# Train and evaluate multiple models
python src/analysis/final_model_comparison.py
# Train the refined XGBoost model
python src/models/train_refined_model.pyThe trained model will be saved to models/xgboost_refined.pkl.
# 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/docscurl http://localhost:8000/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
}'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)
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
Routes are optimized using a composite cost function:
Cost(edge) = length Γ (1 + Ξ± Γ risk_score)
where:
length: Physical edge length in metersrisk_score: XGBoost predicted probability [0, 1]Ξ±: User-defined safety weight (higher = prioritize safety)
- Risk Prediction: XGBoost Gradient Boosting Classifier
- Routing: Dijkstra's shortest path with composite weights
- Multi-Route: Yen's K-Shortest Paths algorithm
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
- NYC Crime Data: NYC Open Data - NYPD Complaint Data Historic
- Street Networks: OpenStreetMap via OSMnx
- POI Data: OpenStreetMap Overpass API
The Android application provides:
- Interactive map interface
- Route visualization with risk overlay
- Origin/destination selection
- Multi-route comparison UI
- Offline route caching
cd src/app
# Open in Android Studio
# Build > Make Project
# Run on emulator or deviceContributions are welcome! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Commit changes (
git commit -m 'Add AmazingFeature') - Push to branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- 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
Muhammad Waleed - Student ID: 455706
Project Link: https://github.com/Muhammad-Waleed381/SafePath