Skip to content

zickrian/FloodSegmentationAPP

Repository files navigation

🌊 Flood Segmentation Web Application

Advanced flood area detection using deep learning (UNet & UNet++) with a modern web interface.

Version License Python Next.js

🎯 Features

  • Dual Model Analysis: Compare UNet and UNet++ predictions
  • Interactive UI: Modern, responsive design for desktop and mobile
  • Real-time Processing: Get results in 1-3 seconds
  • Comprehensive Metrics: Flood area percentage, pixel counts, and model agreement
  • Visual Overlays: Color-coded segmentation masks
  • Disagreement Analysis: See where models differ
  • Production Ready: Deployed to Render with Docker

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Next.js   β”‚ ───> β”‚   FastAPI   β”‚ ───> β”‚  PyTorch    β”‚
β”‚  Frontend   β”‚      β”‚   Backend   β”‚      β”‚   Models    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Frontend:

  • Next.js 15 (App Router)
  • React 18
  • Tailwind CSS
  • TypeScript

Backend:

  • FastAPI
  • PyTorch 2.1 (CPU)
  • Segmentation Models PyTorch
  • Pillow, NumPy, OpenCV-Headless

Models:

  • UNet (ResNet34 encoder)
  • UNet++ (ResNet34 encoder)
  • Trained on flood segmentation dataset (290 images)
  • Test IoU: 80.35% (UNet), 81.48% (UNet++)

πŸš€ Quick Start

Prerequisites

  • Python 3.11+
  • Node.js 18+

Local Development Setup

  1. Clone repository:
git clone https://github.com/yourusername/flood-segmentation.git
cd flood-segmentation
  1. Setup Backend:
cd backend

# Create virtual environment
python -m venv venv
source venv/bin/activate  # Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

# Run backend (models will be loaded from ../Models/)
uvicorn app.main:app --reload --port 8000

Backend will run on http://localhost:8000

  1. Setup Frontend:
# In new terminal, from project root
npm install

# Create environment file
echo "NEXT_PUBLIC_API_URL=http://localhost:8000" > .env.local

# Run frontend
npm run dev

Frontend will run on http://localhost:3000

  1. Test:

Open http://localhost:3000 and upload a flood image!

πŸ“¦ Project Structure

flood-segmentation/
β”œβ”€β”€ app/                      # Next.js pages (App Router)
β”‚   β”œβ”€β”€ page.tsx             # Main upload page
β”‚   β”œβ”€β”€ layout.tsx           # Root layout
β”‚   └── globals.css          # Global styles
β”œβ”€β”€ components/              # React components
β”‚   β”œβ”€β”€ UploadZone.tsx       # Drag & drop upload
β”‚   β”œβ”€β”€ ImagePreview.tsx     # Image preview
β”‚   β”œβ”€β”€ LoadingState.tsx     # Loading animation
β”‚   β”œβ”€β”€ ImageTabs.tsx        # Tabbed image viewer
β”‚   β”œβ”€β”€ AnalysisPanel.tsx    # Statistics & insights
β”‚   └── ResultsViewer.tsx    # Complete results view
β”œβ”€β”€ lib/                     # Utilities
β”‚   β”œβ”€β”€ api.ts               # API client
β”‚   β”œβ”€β”€ types.ts             # TypeScript types
β”‚   └── utils.ts             # Helper functions
β”œβ”€β”€ backend/                 # FastAPI backend
β”‚   β”œβ”€β”€ app/
β”‚   β”‚   β”œβ”€β”€ main.py          # FastAPI app
β”‚   β”‚   β”œβ”€β”€ models.py        # Model loading
β”‚   β”‚   β”œβ”€β”€ preprocessing.py # Image preprocessing
β”‚   β”‚   β”œβ”€β”€ postprocessing.py# Analysis generation
β”‚   β”‚   └── utils.py         # Helpers
β”‚   β”œβ”€β”€ Dockerfile           # Docker configuration for Render
β”‚   └── requirements.txt     # Python dependencies
β”œβ”€β”€ Models/                  # Pre-trained model weights
β”‚   β”œβ”€β”€ unet_baseline_best.pth
β”‚   └── unetplus.pth
β”œβ”€β”€ render.yaml              # Render deployment configuration
└── README.md               # This file

πŸš€ Deployment on Render

This application is configured for easy deployment to Render using Docker.

Method 1: Blueprint (Recommended)

  1. Fork/clone this repository to your GitHub account
  2. Go to Render Dashboard
  3. Click New + β†’ Blueprint
  4. Connect your GitHub repository
  5. Render will auto-detect render.yaml and configure everything
  6. Click Apply to deploy

Method 2: Manual Web Service

  1. Go to Render Dashboard
  2. Click New + β†’ Web Service
  3. Connect your GitHub repository
  4. Configure:
    • Runtime: Docker
    • Docker Build Context: . (root)
    • Dockerfile Path: backend/Dockerfile
  5. Add environment variables:
    • CORS_ORIGINS: * (or your frontend URL)
    • PYTHONUNBUFFERED: 1
  6. Click Deploy

Environment Variables

Variable Description Default
PORT Server port (auto-set by Render) 8000
CORS_ORIGINS Allowed CORS origins *
PYTHONUNBUFFERED Python output buffering 1
MODEL_PATH_UNET Custom UNet model path /Models/unet_baseline_best.pth
MODEL_PATH_UNETPP Custom UNet++ model path /Models/unetplus.pth

πŸ“Š Model Performance

From training on flood segmentation dataset:

Model Test IoU Test Dice Pixel Accuracy
UNet 80.35% 89.06% 91.11%
UNet++ 81.48% 89.77% 91.58%

πŸ“ API Documentation

Endpoints

Health Check

GET /health

Response:

{
  "status": "healthy",
  "models_loaded": true,
  "device": "cpu"
}

Segment Image

POST /api/segment
Content-Type: multipart/form-data

Body:
  file: <image-file>

Response:

{
  "success": true,
  "data": {
    "unet": {
      "flood_percent": 32.45,
      "flood_pixels": 21234,
      "total_pixels": 65536,
      "summary": "..."
    },
    "unetpp": { ... },
    "comparison": { ... },
    "images": {
      "original": "data:image/png;base64,...",
      "unet_overlay": "data:image/png;base64,...",
      "unetpp_overlay": "data:image/png;base64,...",
      "disagreement": "data:image/png;base64,..."
    }
  }
}

🀝 Contributing

Contributions are welcome! Please follow these steps:

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

πŸ“„ License

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

πŸ™ Acknowledgments


Built with ❀️ using Next.js, FastAPI, and PyTorch

About

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors