CoolFace
Apppublic

confirmkky/oil-spill-api

sourceHugging Faceupdated 8mo agoView on Hugging Face
0likes
App README

๐ŸŒŠ Oil Spill Detection App

A web application for detecting oil spills in SAR (Synthetic Aperture Radar) images using a trained U-Net deep learning model. This app is designed to be deployed on Hugging Face Spaces.

๐Ÿ“‹ Project Overview

This application processes SAR images to identify and highlight oil spills. SAR images are particularly useful for oil spill detection because they can operate day and night and are not affected by weather conditions like optical sensors.

๐Ÿ—๏ธ Project Structure

oil-spill-app/
โ”œโ”€โ”€ app.py                 # Main Gradio application
โ”œโ”€โ”€ requirements.txt       # Python dependencies
โ”œโ”€โ”€ models/                # Model directory
โ”‚   โ””โ”€โ”€ best_model.pth    # Trained U-Net model weights
โ”œโ”€โ”€ README.md             # Project documentation
โ”œโ”€โ”€ samples/              # Sample SAR images for demo
โ”‚   โ”œโ”€โ”€ Ocean Scene/
โ”‚   โ”œโ”€โ”€ Oil Scene/
โ”‚   โ””โ”€โ”€ Coastal Scene/
โ””โ”€โ”€ utils/                # Utility functions
    โ”œโ”€โ”€ __init__.py
    โ””โ”€โ”€ preprocessing.py  # SAR image preprocessing functions

๐Ÿš€ Setup Instructions

For Hugging Face Spaces Deployment

  1. 1.Upload your files to Hugging Face Spaces:
  2. 2.Create a new Space on Hugging Face
  3. 3.Choose Gradio as the SDK
  4. 4.Upload all files from this repository
  1. 1.Add your trained model:
  2. 2.Upload your best_model.pth file to the Space
  3. 3.Update the model loading code in app.py to match your model architecture
  1. 1.Configure the Space:
  2. 2.The requirements.txt will be automatically detected
  3. 3.Make sure your model file is in the root directory

For Local Development

  1. 1.Install dependencies:
bash
   pip install -r requirements.txt
  1. 1.Place your trained model:
  2. 2.Save your trained U-Net model as best_model.pth in the models/ directory
  3. 3.If you have the model in a zip file, extract it and place best_model.pth in the models/ folder
  1. 1.Add sample images:
  2. 2.Place sample SAR images in the samples/ directory as sample1.png and sample2.png
  1. 1.Run the application:
bash
   python app.py
  1. 1.Access the app:
  2. 2.Open your browser and go to http://localhost:7860

๐Ÿง  Model Information

  • โ€”Architecture: U-Net (Convolutional Neural Network)
  • โ€”Input: SAR images (typically single-channel grayscale)
  • โ€”Output: Segmented image with detected oil spills

Model weights are tracked using Git LFS due to size (~118MB).

To Use Your Own Model

  1. 1.Update the load_model() function in app.py to match your model architecture:
python
   def load_model(model_path="best_model.pth"):
       # Define your model architecture
       model = YourUNetModel(in_channels=1, out_channels=1)
       model.load_state_dict(torch.load(model_path, map_location='cpu'))
       model.eval()
       return model
  1. 1.Update the predict_oil_spill() function to use your model for inference.

๐Ÿ“ Features

  • โ€”โœ… Upload SAR images for oil spill detection
  • โ€”โœ… Real-time image preprocessing
  • โ€”โœ… Visual display of detection results
  • โ€”โœ… Sample images for quick testing
  • โ€”โœ… Gradio-based user-friendly interface

๐Ÿ”ง Dependencies

  • โ€”gradio - Web interface framework
  • โ€”torch - Deep learning framework
  • โ€”torchvision - Image processing utilities
  • โ€”numpy - Numerical computing
  • โ€”opencv-python - Image processing
  • โ€”Pillow - Image handling

๐Ÿ“Š Preprocessing

The app includes SAR-specific preprocessing functions:

  • โ€”Image normalization
  • โ€”Histogram equalization
  • โ€”Speckle noise reduction (optional)
  • โ€”Resizing and formatting

๐Ÿค Contributing

Feel free to improve the preprocessing pipeline, add more features, or enhance the model inference code.

๐Ÿ“„ License

[Add your license here]

๐Ÿ™ Acknowledgments

  • โ€”U-Net architecture for semantic segmentation
  • โ€”Hugging Face Spaces for hosting
  • โ€”SAR image processing community