This project aims to detect swimming pools in aerial images using YOLOv8 segmentation.
The model is trained to identify pools of any shape and output their exact boundaries.
- A single aerial image (
.jpg,.png, etc.)
- coordinates.txt → Detected pool boundary coordinates.
- output_image.jpg → Image with red outlines around pools.
├── pool_detect.py # Command-line script for pool detection
├── requirements.txt # Required Python packages
├── server.py # (Optional) API server for pool detection
├── test_images/ # Sample test images
│ ├── test1.jpg
│ ├── test2.jpg
│ ├── test3.jpg
│ ├── test4.jpg
│ └── test5.jpg
├── weights/
│ └── best.pt # Trained YOLOv8 model weights
├── output_image.jpg # Result of running pool_detect.py on test1.jpg
├── coordinates.txt # Polygon coordinates of detected pools in test1.jpg
python -m venv pool_venv
source pool_venv/bin/activate # On Linux/Mac
pool_venv\Scripts\activate # On Windowspip install -r requirements.txt- or manually install the requiremnets *
pip install ultralytics opencv-python numpyThe model was trained using YOLOv8n-seg (segmentation model) from the Ultralytics library.
- Labeled dataset using Roboflow with polygon annotations
- Split the dataset into:
- Train → 70%
- Validation → 20%
- Test → 10%
- Exported the dataset in YOLOv8 segmentation format.
from ultralytics import YOLO
# Load YOLOv8 segmentation model
model = YOLO("yolov8n-seg.pt")
# Train the model
model.train(data="data.yaml", epochs=50, imgsz=640, device="cpu", batch=8, workers=4)- The best model weights are saved in:
runs/segment/train/weights/best.pt - Final model stored in
weights/best.pt.
To detect pools in an image, use the following command:
python pool_detect.py test_images/test1.jpg- Loads the trained model (
weights/best.pt). - Runs inference on the input image.
- Filters detections with confidence ≥ 60%.
- Extracts pool boundary coordinates and saves them to
coordinates.txt. - Draws a red outline around detected pools (no rectangles).
- Saves the output as
output_image.jpg.
Example before & after pool detection:

- The script successfully outlines pools without adding rectangles.
coordinates.txtcontains the polygon coordinates of detected pools.
You can also test the model using a Gradio interface by running the following script:
python gradio_app.pyThis will launch a web interface where you can upload images and see the pool detection results in real-time.
- The Gradio app provides an easy-to-use interface for testing the model without writing any code.
- Simply upload an image, and the app will display the detected pools with red outlines.
You can also use the server.py script to run a Flask server that takes an image and returns an image with the pool outlined.
python server.pyOnce the server is running, you can send a POST request with an image to the server and receive the processed image in response.
Example using curl:
curl -X POST -F "file=@test_images/test1.jpg" http://127.0.0.1:5000/detect -o output_image.jpg- The server will process the image and return the result with detected pools outlined in red.
You can also try the model online without setting it up or installing anything by visiting the Hugging Face Space: Try Pool Detection on Hugging Face
- This space provides an interactive interface to upload images and see the pool detection results instantly.
- It's a convenient way to test the model before setting it up locally.
- Ultralytics YOLOv8: https://github.com/ultralytics/ultralytics
- Dataset Source: Swimming Pool Detection - Algarve's Landscape
- Roboflow (for labeling): https://roboflow.com/
