Skip to content

Repository files navigation

πŸ’§ LeakLens β€” Water Leakage Detection System

AI-powered anomaly detection for SCADA water networks using a trained Conv1D Autoencoder.
Full-stack system: FastAPI backend Β· Web dashboard Β· Flutter mobile app.


πŸ—οΈ Architecture

Water-Leak/
β”œβ”€β”€ backend/                 ← FastAPI + TensorFlow inference
β”‚   β”œβ”€β”€ main.py              ← App entry point
β”‚   β”œβ”€β”€ model/
β”‚   β”‚   β”œβ”€β”€ predictor.py     ← Real + Mock autoencoder inference
β”‚   β”‚   └── zones.py         ← Zone mapping (n1β†’Zone1 … n300β†’Zone5)
β”‚   β”œβ”€β”€ api/routes/
β”‚   β”‚   β”œβ”€β”€ predict.py       ← POST /predict
β”‚   β”‚   β”œβ”€β”€ reports.py       ← POST /report Β· GET /alerts
β”‚   β”‚   β”œβ”€β”€ analytics.py     ← GET /analytics Β· GET /timeseries
β”‚   β”‚   └── auth.py          ← User & Engineer register/login
β”‚   β”œβ”€β”€ db/
β”‚   β”‚   β”œβ”€β”€ database.py      ← SQLAlchemy + SQLite engine
β”‚   β”‚   └── models.py        ← users Β· engineers Β· reports Β· anomalies Β· sensor_logs Β· outages
β”‚   β”œβ”€β”€ schemas/
β”‚   β”‚   └── schemas.py       ← Pydantic v2 request/response models
β”‚   β”œβ”€β”€ model_files/         ← DROP .keras + .pkl FILES HERE
β”‚   β”œβ”€β”€ requirements.txt
β”‚   └── .env
β”‚
β”œβ”€β”€ dashboard/               ← Browser-based control panel
β”‚   β”œβ”€β”€ index.html
β”‚   β”œβ”€β”€ style.css            ← Dark-mode glassmorphism design
β”‚   └── app.js               ← Synthetic data generators + Chart.js
β”‚
β”œβ”€β”€ mobile/                  ← Flutter mobile app
β”‚   β”œβ”€β”€ pubspec.yaml
β”‚   └── lib/
β”‚       β”œβ”€β”€ main.dart
β”‚       β”œβ”€β”€ theme/app_theme.dart
β”‚       β”œβ”€β”€ models/prediction_model.dart
β”‚       β”œβ”€β”€ services/api_service.dart
β”‚       └── screens/
β”‚           β”œβ”€β”€ role_selection_screen.dart
β”‚           β”œβ”€β”€ auth/login_screen.dart
β”‚           β”œβ”€β”€ auth/register_screen.dart
β”‚           β”œβ”€β”€ user/user_home_screen.dart
β”‚           β”œβ”€β”€ user/report_issue_screen.dart
β”‚           β”œβ”€β”€ engineer/engineer_dashboard_screen.dart
β”‚           └── engineer/analytics_screen.dart
β”‚
β”œβ”€β”€ run_system.bat        ← One-click Windows startup script
└── another_copy_of_water_leakage_advanced.py  ← Original training notebook

πŸš€ Quick Start

1 Β· Backend

# Windows β€” double-click or run:
run_system.bat
# Manual
cd backend
python -m venv venv
venv\Scripts\activate
pip install -r requirements.txt
uvicorn main:app --reload --host 0.0.0.0 --port 8000
URL Purpose
http://localhost:8000/docs Interactive Swagger UI
http://localhost:8000/health Model status + sensor count
http://localhost:8000/redoc ReDoc documentation

2 Β· Web Dashboard

Simply open in your browser β€” no build step needed:

dashboard/index.html

Click the three buttons to test the detection pipeline live.


3 Β· Mobile App

cd mobile
flutter pub get
flutter run

Android emulator: Backend URL is pre-set to http://10.0.2.2:8000
Physical device / iOS: Update baseUrl in lib/services/api_service.dart


🧠 Model Artifacts

Place your trained files in backend/model_files/:

backend/model_files/
β”œβ”€β”€ water_leakage_model.keras   ← from training notebook
β”œβ”€β”€ scaler.pkl                  ← RobustScaler
└── threshold.pkl               ← 95th-percentile MSE float

No files? The backend auto-switches to a smart mock predictor that correctly classifies Leak / Normal / Random data based on statistical patterns β€” perfect for development.


πŸ“‘ API Reference

POST /api/v1/predict

// Request
{
  "data": [[...], [...], ...]   // shape (48, N_sensors)
}

// Response
{
  "is_anomaly": 1,
  "confidence": 0.8742,
  "mse": 0.234512,
  "threshold": 0.100000,
  "top_sensors": ["n33", "n28", "n74"],
  "zone": "Zone 2",
  "sensor_errors": [0.001, 0.023, ...],
  "message": "⚠️ Leak detected in Zone 2 with 87.4% confidence",
  "latency_ms": 12.4
}

POST /api/v1/report

{ "zone": "Zone 3", "description": "Visible puddle near node 65", "severity": "high" }

GET /api/v1/alerts?limit=50&zone=Zone+2&anomaly_only=true

GET /api/v1/analytics?days=30

GET /api/v1/timeseries?hours=24

POST /api/v1/auth/user/register

{ "name": "Ahmed", "address": "...", "zone": "Zone 1", "phone": "...", "email": "...", "password": "..." }

POST /api/v1/auth/engineer/login

{ "engineer_id": "ENG-001", "password": "..." }

POST /api/v1/outages

{ "zone": "Zone 1", "title": "Main pipe repair", "description": "...", "start_time": "2026-05-14T20:00:00Z", "end_time": "2026-05-14T22:00:00Z" }

GET /api/v1/outages


πŸ—„οΈ Database Schema

Table Key Columns
users id Β· name Β· email Β· phone Β· zone Β· password_hash
engineers id Β· engineer_id Β· name Β· password_hash
reports id Β· user_id Β· zone Β· description Β· severity Β· status
anomalies id Β· is_anomaly Β· confidence Β· mse Β· threshold Β· top_sensors Β· zone
sensor_logs id Β· timestamp Β· num_sensors Β· mean_value Β· std_value Β· anomaly_detected
water_outages id Β· zone Β· title Β· description Β· start_time Β· end_time Β· is_cancelled

🌐 Zone Mapping

Zone Sensors Description
Zone 1 n1 – n30 Early Network (Intake / Primary Pipes)
Zone 2 n31 – n60 Middle Distribution Network
Zone 3 n61 – n90 Main Distribution Grid
Zone 4 n91 – n120 End Network / High Pressure Zones
Zone 5 n121 – n300 Extended / Remote Network

πŸ“± Mobile App Screens

Screen Role Features
Role Selection Both Animated User/Engineer card selection
Login Both Email/phone + password Β· Forgot password
Register User 6-field form Β· Zone dropdown Β· Password confirm
User Home User Alerts feed Β· Report issue Β· Water-saving tips Β· Active Outages
Report Issue User Zone + severity + description Β· Success animation
Outages User Full-screen alarm notification Β· Countdown timer
Engineer Dashboard Engineer Overview KPIs Β· Pressure/flow charts Β· Alerts tab
Analytics Engineer Bar chart Β· Zone leaks table Β· Period filter

πŸ”’ Security Notes

  • Passwords hashed with bcrypt (12 rounds)
  • JWT tokens with 7-day expiry
  • Change SECRET_KEY in .env before any production deployment
  • Restrict CORS allow_origins to your actual frontend domain in production

πŸ§ͺ Testing the Prediction Pipeline

# Leak data β€” should return is_anomaly: 1
curl -X POST http://localhost:8000/api/v1/predict \
  -H "Content-Type: application/json" \
  -d @- << 'EOF'
{
  "data": [
    [-0.0, -0.1, -0.2, 0.01, 0.02, 0.0, -0.01, 0.01, 0.0, -0.02,
      0.0, 0.01, -0.01, 0.0, 0.01, -0.01, 0.0, 0.01, 0.0, -0.01],
    [-0.1, -0.2, -0.35, 0.0, 0.01, -0.01, 0.0, 0.01, 0.0, -0.01,
      0.01, 0.0, -0.01, 0.0, 0.01, 0.0, 0.01, 0.0, 0.01, 0.0]
  ]
}
EOF

Tip: Use the web dashboard buttons for a richer visual test experience.

About

AI-powered water leakage detection system using deep learning and SCADA sensor data with FastAPI, Flutter, and an interactive web dashboard.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages