confirmkky/oil-spill-api
0
๐ 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
- Upload your files to Hugging Face Spaces:
- Create a new Space on Hugging Face
- Choose Gradio as the SDK
- Upload all files from this repository
- Add your trained model:
- Upload your
best_model.pthfile to the Space - Update the model loading code in
app.pyto match your model architecture
- Configure the Space:
- The
requirements.txtwill be automatically detected - Make sure your model file is in the root directory
For Local Development
- Install dependencies:
pip install -r requirements.txt- Place your trained model:
- Save your trained U-Net model as
best_model.pthin themodels/directory - If you have the model in a zip file, extract it and place
best_model.pthin themodels/folder
- Add sample images:
- Place sample SAR images in the
samples/directory assample1.pngandsample2.png
- Run the application:
python app.py- Access the app:
- 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
- Update the
load_model()function inapp.pyto match your model architecture:
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- 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 frameworktorch- Deep learning frameworktorchvision- Image processing utilitiesnumpy- Numerical computingopencv-python- Image processingPillow- 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
