Skip to content

DevGurav/fall-detect-system

Repository files navigation

AI-Based Fall Risk Detection System

A comprehensive fall detection system using ESP32 wristband sensors, cloud-based AI prediction, and real-time mobile notifications.

πŸš€ System Overview

This system detects falls using a wristband (ESP32 + MPU6050) and notifies caregivers in real-time through a mobile app. The system uses a trained AI model deployed in the cloud to analyze sensor data and determine if a fall has occurred.

πŸ—οΈ Architecture

ESP32 Device β†’ Flask API β†’ AI Model β†’ Firebase β†’ Mobile App
  1. ESP32 collects accelerometer + gyroscope data from MPU6050
  2. When suspicious activity is detected, ESP32 sends data via HTTP POST to cloud API
  3. Cloud API loads pre-trained ML model (RandomForest/MLP) and predicts if a fall occurred
  4. If fall is confirmed β†’ API triggers Firebase Cloud Messaging (FCM) push notification
  5. Mobile App (Flutter) receives notification even when closed/backgrounded
  6. App shows fall event history stored in Firestore

πŸ“ Project Structure

fall-detection-system/
β”œβ”€β”€ backend/                 # Flask API server
β”‚   β”œβ”€β”€ main.py             # Main Flask application
β”‚   β”œβ”€β”€ requirements.txt    # Python dependencies
β”‚   β”œβ”€β”€ .env.example       # Environment variables template
β”‚   └── Procfile           # Deployment configuration
β”œβ”€β”€ mobile_app/             # Flutter mobile application
β”‚   β”œβ”€β”€ lib/
β”‚   β”‚   β”œβ”€β”€ models/        # Data models
β”‚   β”‚   β”œβ”€β”€ services/      # Firebase service
β”‚   β”‚   β”œβ”€β”€ screens/       # UI screens
β”‚   β”‚   β”œβ”€β”€ widgets/       # Reusable UI components
β”‚   β”‚   └── main.dart      # App entry point
β”‚   └── pubspec.yaml       # Flutter dependencies
└── README.md              # This file

πŸ› οΈ Tech Stack

  • Device: ESP32 (Wi-Fi), MPU6050 accelerometer/gyroscope
  • Backend: Python + Flask, joblib (ML model), Firebase Admin SDK
  • Database: Firebase Firestore (event logs & device tokens)
  • Notifications: Firebase Cloud Messaging (FCM)
  • Mobile App: Flutter (cross-platform, Android/iOS)
  • Hosting: Free tier cloud hosting (Render/Railway) + Firebase free tier

πŸš€ Quick Start

Backend Setup

  1. Navigate to backend directory:

    cd backend
  2. Install dependencies:

    pip install -r requirements.txt
  3. Configure Firebase:

    • Create a Firebase project at https://console.firebase.google.com/
    • Generate a service account key (Settings β†’ Service accounts β†’ Generate new private key)
    • Copy .env.example to .env and add your Firebase service account key
  4. Run the server:

    python main.py

Mobile App Setup

  1. Navigate to mobile app directory:

    cd mobile_app
  2. Install Flutter dependencies:

    flutter pub get
  3. Configure Firebase:

    • Add your Firebase configuration files (see firebase-setup.md)
    • Update Firebase project settings in the app
  4. Run the app:

    flutter run

πŸ“± Features

Backend API

  • βœ… /predict - Accepts sensor data and returns fall prediction
  • βœ… /register-device - Registers mobile devices for notifications
  • βœ… /health - Health check endpoint
  • βœ… /fall-events - Retrieves fall event history
  • βœ… Automatic Firestore logging of all events
  • βœ… Real-time FCM notifications for fall alerts

Mobile App

  • βœ… Real-time fall event monitoring
  • βœ… Background notification handling (app closed/minimized)
  • βœ… Local notifications with sound and vibration
  • βœ… Fall event history with timestamps
  • βœ… Device statistics dashboard
  • βœ… Event acknowledgment system
  • βœ… Automatic Firebase synchronization

ESP32 Integration

The ESP32 device should send HTTP POST requests to your backend /predict endpoint with this JSON format:

{
  "device_id": "esp32_001",
  "accelX": 0.123,
  "accelY": 0.456,
  "accelZ": 0.789,
  "gyroX": 1.234,
  "gyroY": 5.678,
  "gyroZ": 9.012
}

πŸ”§ Configuration

Environment Variables (Backend)

Create a .env file in the backend directory:

FIREBASE_SERVICE_ACCOUNT_KEY={"type": "service_account", ...}
FLASK_ENV=production
SECRET_KEY=your-secret-key
PORT=5000

Firebase Setup

  1. Enable required services:

    • Firestore Database
    • Cloud Messaging
    • Authentication (optional)
  2. Firestore Security Rules:

    rules_version = '2';
    service cloud.firestore {
      match /databases/{database}/documents {
        match /fall_events/{document} {
          allow read, write: if true;
        }
        match /devices/{document} {
          allow read, write: if true;
        }
      }
    }

🌐 Deployment

Backend Deployment (Render/Railway)

  1. Create a new web service
  2. Connect your GitHub repository
  3. Set environment variables:
    • FIREBASE_SERVICE_ACCOUNT_KEY
    • FLASK_ENV=production
  4. The service will automatically use the Procfile for deployment

Mobile App Deployment

  1. Android:

    flutter build apk --release
  2. iOS:

    flutter build ios --release

πŸ”” Notification Flow

  1. ESP32 detects suspicious motion β†’ sends data to API
  2. API runs ML prediction β†’ if fall detected, stores event in Firestore
  3. API retrieves FCM tokens from devices collection
  4. API sends push notification to all registered devices
  5. Mobile app receives notification (even if closed)
  6. App shows local notification with sound/vibration
  7. User can view event details and acknowledge

πŸ€– Machine Learning Model

The system uses a pre-trained ML model (fall_model.pkl) that should be placed in the backend directory. The model expects 6 features:

  • accelX, accelY, accelZ (accelerometer data)
  • gyroX, gyroY, gyroZ (gyroscope data)

For testing purposes, a dummy RandomForest model is created automatically if no model file is found.

πŸ“Š API Endpoints

POST /predict

Analyze sensor data for fall detection.

Request Body:

{
  "device_id": "string",
  "accelX": "number",
  "accelY": "number", 
  "accelZ": "number",
  "gyroX": "number",
  "gyroY": "number",
  "gyroZ": "number"
}

Response:

{
  "fall": "boolean",
  "confidence": "number",
  "event_id": "string",
  "timestamp": "string",
  "notification_sent": "boolean"
}

POST /register-device

Register a device for FCM notifications.

Request Body:

{
  "device_id": "string",
  "fcm_token": "string"
}

GET /fall-events

Retrieve fall event history.

Query Parameters:

  • limit (optional): Number of events to return (default: 50)
  • device_id (optional): Filter by specific device

πŸ›‘οΈ Security Considerations

  • Use HTTPS for all API communication
  • Implement proper Firebase security rules
  • Store sensitive configuration in environment variables
  • Regular security updates for all dependencies
  • Consider implementing API authentication for production use

πŸ” Troubleshooting

Common Issues

  1. Firebase initialization fails:

    • Check service account key format
    • Verify Firebase project configuration
    • Ensure required Firebase services are enabled
  2. Notifications not received:

    • Check FCM token registration
    • Verify notification permissions
    • Test with Firebase console
  3. App crashes on startup:

    • Run flutter clean && flutter pub get
    • Check Firebase configuration files
    • Verify minimum SDK versions

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Test thoroughly
  5. Submit a pull request

πŸ“„ License

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

πŸ“ž Support

For support, please create an issue in the GitHub repository or contact the development team.


Note: This system is designed for monitoring purposes and should not be used as the sole method of fall detection for critical safety applications. Always have backup safety measures in place.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors