Skip to content

OldAdventurer/pedestrian-behavior-analyzer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

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

Repository files navigation

Pedestrian Behavior Analyzer

A modern web application for visualizing and analyzing pre-collision pedestrian data. Built with Flask (Python) backend and React frontend, this platform enables interactive exploration of pedestrian safety modeling and collision risk analysis.

Pedestrian Behavior Analyzer Python React Flask

🎯 Features

Interactive Dashboard

  • Map Visualization: Interactive map using Leaflet.js displaying collision events
  • Color-Coded Markers: Events color-coded by severity (green to dark red)
  • Real-time Statistics: Live stats showing collision rates and average time-to-collision

Data Upload Interface

  • Drag & Drop: Easy CSV file upload with drag-and-drop support
  • Data Validation: Automatic validation and preview of uploaded data
  • Sample Data: Includes synthetic dataset for quick demonstration

Statistical Analysis

  • Multiple Models:
    • Logistic Regression
    • Cox Proportional Hazards
    • Random Forest Classifier
  • Feature Selection: Interactive feature selection for model training
  • Performance Metrics: Comprehensive metrics including accuracy, precision, recall, F1-score, and AUC
  • Visual Analytics: Charts showing feature importance and model performance

Simulation Panel

  • Parameter Adjustment: Real-time sliders for modifying input parameters
  • Sensitivity Analysis: Visualize how parameters affect collision probability
  • Interactive Predictions: See instant predictions as parameters change

Report Generation

  • PDF Reports: Professional reports with model summaries and visualizations
  • CSV Export: Export results for further analysis in Excel or R/Python
  • Academic Format: Reports formatted for research and teaching use

πŸš€ Quick Start

Prerequisites

  • Python 3.8 or higher
  • Node.js 16 or higher
  • npm or yarn

Installation

  1. Clone the repository
git clone <repository-url>
cd testing-pedestrians
  1. Backend Setup
# Install Python dependencies
pip install -r requirements.txt

# Generate sample data
cd backend
python generate_sample_data.py
cd ..
  1. Frontend Setup
cd frontend
npm install
cd ..

Running the Application

You'll need two terminal windows:

Terminal 1 - Backend (Flask)

cd backend
python app.py

The backend API will run on http://localhost:5000

Terminal 2 - Frontend (React)

cd frontend
npm run dev

The frontend will run on http://localhost:3000

Open your browser and navigate to http://localhost:3000

πŸ“Š Sample Data

The application includes a sample dataset generator that creates realistic pedestrian collision data with:

  • 500 events with varied parameters
  • Location coordinates (latitude/longitude)
  • Pedestrian characteristics (age, velocity, distraction status)
  • Vehicle characteristics (type, velocity, braking)
  • Environmental factors (weather, lighting, road type)
  • Collision outcomes with severity ratings (0-5)
  • Time-to-collision values
  • Avoidance behaviors

To generate new sample data:

cd backend
python generate_sample_data.py

πŸ“– Usage Guide

1. Upload Data

Option A: Use Sample Data (Recommended for First Time)

  • Navigate to "Upload Data" in the sidebar
  • Click "Load Sample Data" button
  • Sample dataset (500 events) loads instantly
  • Dashboard and all features become immediately available

Option B: Upload Your Own Data

  • Navigate to "Upload Data" in the sidebar
  • Drag and drop your CSV file or click to browse
  • Review the upload summary
  • Data becomes available across all features

Option C: Download Sample for Inspection

  • Click "Download Sample CSV" to save the file
  • Inspect the format and structure
  • Use as a template for your own data

2. View Dashboard

  • See all collision events on an interactive map
  • Color-coded markers show severity
  • Click markers for event details
  • View overall statistics

3. Run Analysis

  • Go to "Analytics" section
  • Select a statistical model (Logistic, Cox, or Random Forest)
  • Choose features for analysis
  • Click "Train Model"
  • Review performance metrics and feature importance

4. Simulate Scenarios

  • Navigate to "Simulation"
  • Adjust parameters using sliders:
    • Pedestrian velocity
    • Vehicle velocity
    • Time to collision
    • Distraction level
    • Braking status
  • Click "Run Simulation"
  • View sensitivity charts showing impact of each parameter

5. Generate Reports

  • Go to "Reports" section
  • Select your trained model
  • Generate PDF report with all metrics and visualizations
  • Or export CSV for further analysis

πŸ“ Project Structure

testing-pedestrians/
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ app.py                    # Flask API server
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   β”œβ”€β”€ logistic_regression.py
β”‚   β”‚   β”œβ”€β”€ cox_hazards.py
β”‚   β”‚   └── random_forest.py
β”‚   β”œβ”€β”€ utils/
β”‚   β”‚   β”œβ”€β”€ data_processor.py     # Data processing utilities
β”‚   β”‚   └── report_generator.py   # PDF report generation
β”‚   β”œβ”€β”€ generate_sample_data.py   # Sample data generator
β”‚   └── uploads/                  # Uploaded files directory
β”œβ”€β”€ frontend/
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ components/
β”‚   β”‚   β”‚   β”œβ”€β”€ Dashboard.jsx     # Map and statistics
β”‚   β”‚   β”‚   β”œβ”€β”€ Upload.jsx        # File upload interface
β”‚   β”‚   β”‚   β”œβ”€β”€ Analytics.jsx     # Model training and analysis
β”‚   β”‚   β”‚   β”œβ”€β”€ Simulation.jsx    # Parameter simulation
β”‚   β”‚   β”‚   └── Reports.jsx       # Report generation
β”‚   β”‚   β”œβ”€β”€ App.jsx               # Main app component
β”‚   β”‚   └── main.jsx              # Entry point
β”‚   β”œβ”€β”€ package.json
β”‚   └── vite.config.js
β”œβ”€β”€ requirements.txt              # Python dependencies
└── README.md                     # This file

πŸ”§ API Endpoints

Data Management

  • POST /api/upload - Upload CSV data
  • GET /api/data/sample - Get sample of current dataset
  • GET /api/data/map - Get collision events for map

Model Operations

  • POST /api/model/train - Train a statistical model
  • POST /api/model/predict - Make predictions

Simulation & Reports

  • POST /api/simulate - Run parameter simulation
  • POST /api/report/generate - Generate PDF report
  • POST /api/report/export-csv - Export results as CSV

πŸŽ“ Educational Use Cases

This platform is designed for:

  1. Graduate Research: Interactive tool for exploring pedestrian safety datasets
  2. Teaching: Demonstrate statistical modeling concepts in traffic safety courses
  3. Policy Analysis: Communicate findings to municipalities and stakeholders
  4. Hypothesis Testing: Quickly test different model configurations
  5. Data Exploration: Visual exploration of complex collision datasets

πŸ› οΈ Technology Stack

Backend

  • Flask - Python web framework
  • scikit-learn - Machine learning models
  • lifelines - Survival analysis (Cox model)
  • pandas - Data manipulation
  • ReportLab - PDF generation

Frontend

  • React - UI framework
  • Vite - Build tool
  • Tailwind CSS - Styling
  • Leaflet.js - Interactive maps
  • Recharts - Data visualization
  • Axios - HTTP client

πŸ”’ Data Privacy

  • All data processing happens locally
  • No data is sent to external servers
  • Uploaded files are stored temporarily
  • Clear data management through the interface

πŸ“ Expected CSV Format

Your CSV file should include these columns:

Column Description Type
latitude Event latitude float
longitude Event longitude float
pedestrian_velocity Pedestrian speed (m/s) float
vehicle_velocity Vehicle speed (m/s) float
ttc Time to collision (seconds) float
collision Collision occurred (0/1) binary
severity Collision severity (0-5) integer
pedestrian_distracted Distraction status (0/1) binary
vehicle_braking Braking status (0/1) binary

Optional columns: weather, lighting, road_type, avoidance_behavior, etc.

☁️ Cloud Deployment

Deploy to Azure (Cheapest Option: ~$15-20/month)

Quick Deploy (10 minutes):

# Set your Docker Hub username (or use environment variable)
export DOCKER_HUB_USERNAME=your-dockerhub-username
./deploy-azure.sh

Manual Deployment: See detailed guides:

Free Option: Use Azure App Service Free Tier ($0/month) - good for testing and demos. See DEPLOYMENT_OPTIONS.md for details.

What you get:

  • βœ… Azure Static Web Apps (Frontend) - FREE
  • βœ… Azure Container Instances (Backend) - ~$15/month
  • βœ… Custom domain support with free SSL
  • βœ… Auto-scaling and CDN
  • βœ… One-click deployment script

New to Azure? Get $200 free credits for 30 days! Sign up here

πŸ› Troubleshooting

Backend won't start

  • Check Python version: python --version (should be 3.8+)
  • Reinstall dependencies: pip install -r requirements.txt
  • Check port 5000 is available

Frontend won't start

  • Check Node version: node --version (should be 16+)
  • Delete node_modules and reinstall: rm -rf node_modules && npm install
  • Check port 3000 is available

Models fail to train

  • Ensure data is uploaded first
  • Check CSV format matches expected structure
  • Verify sufficient data points (recommended: 100+ rows)

🀝 Contributing

Contributions are welcome! Please feel free to submit issues or pull requests.

πŸ“„ License

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

πŸ‘€ Author

Created by OldAdventurer as an academic research platform for pedestrian safety analysis.

πŸ™ Acknowledgments

This project uses open-source libraries and tools. We're grateful to all contributors of these projects:

Backend:

Frontend:

Infrastructure:

  • OpenStreetMap - Map tiles and data
  • All the amazing open-source contributors whose work makes projects like this possible

See requirements.txt and package.json for the complete list of dependencies.


Note: This is a research and educational tool. Model predictions should be validated with domain experts before use in policy decisions.

About

A modern web application for visualizing and analyzing pre-collision pedestrian data.

Topics

Resources

License

Stars

Watchers

Forks

Contributors