An intelligent foam detection system that monitors water quality in real-time using YOLOv8 computer vision and automated email notifications.
##Overview
This project implements an automated water quality monitoring solution that:
- Detects foam presence in live video streams using YOLOv8 object detection
- Monitors water flow continuously with OpenCV-based analysis
- Sends automated alerts via email when foam is detected or streams are offline
- Prevents duplicate processing with intelligent video tracking
- Detection: YOLOv8 model trained for 25 epochs with optimized 224×224 resolution
- Smart Notifications: Automated email system with 24-hour interval monitoring
- Stream Validation: Real-time video stream availability checking
- Water Flow Analysis: OpenCV-based flow detection with 10% pixel threshold
- Duplicate Prevention: Tracks processed videos to avoid redundant analysis
- Continuous Monitoring: 24/7 automated surveillance with configurable intervals
- Deep Learning: YOLOv8 (Ultralytics)
- Computer Vision: OpenCV
- Email Service: SMTP with Gmail integration
- Cloud Platform: Google Colab
- Storage: Google Drive integration
- Languages: Python 3.x
ultralytics
opencv-python
google-colab
smtplib (built-in)
subprocess (built-in)
-
Clone the repository
git clone https://github.com/yourusername/foam-detection.git cd foam-detection -
Install dependencies
pip install -U ultralytics opencv-python
-
Setup Google Drive (if using Colab)
from google.colab import drive drive.mount('/content/drive')
-
Configure email credentials
# Update in the script TO_EMAIL = 'your-email@gmail.com' from_email = 'sender-email@gmail.com' from_password = 'your-app-password' # Gmail App Password
!yolo task=detect mode=train model=yolov8m.pt data=data.yaml epochs=25 imgsz=224 plots=True- Detection Confidence: 0.25
- Image Size: 224×224 pixels
- Monitoring Interval: 24 hours
- Email Cooldown: 24 hours
- Water Flow Threshold: 10% pixel activity
Video Stream → YOLOv8 Detection → Water Flow Analysis → Email Notification
↓ ↓ ↓ ↓
Stream Check → Foam Detection → Flow Validation → Alert System
# Train YOLOv8 on your foam dataset
!yolo task=detect mode=train model=yolov8m.pt data=data.yaml epochs=25 imgsz=224# Single prediction
!yolo task=detect mode=predict model=runs/detect/train6/weights/best.pt conf=0.25 source=your_video.mp4
# Continuous monitoring
python foam_detection.pyThe system automatically sends different types of alerts:
- ✅ "No Foam Detected" - Water quality is good
⚠️ "Foam Detected" - Foam present in water- 🔴 "No Live Stream" - Video stream unavailable
- 💧 "No Water Flow Detected" - No water movement detected
foam-detection/
├── foam_detection.py # Main detection script
├── data/
│ ├── train/ # Training images
│ ├── val/ # Validation images
│ ├── test/ # Test videos
│ └── data.yaml # Dataset configuration
├── runs/
│ └── detect/
│ └── train6/
│ └── weights/
│ └── best.pt # Trained model weights
├── last_email_time.txt # Email tracking
├── processed_videos.txt # Video processing log
└── README.md
- Stream Validation: Checks if video stream is accessible
- Water Flow Analysis: Validates presence of water movement
- YOLOv8 Detection: Runs foam detection on video frames
- Result Processing: Analyzes detection outputs
- Notification: Sends appropriate email alerts
- Logging: Records processed videos to prevent duplicates
- Model: YOLOv8 Medium (yolov8m.pt)
- Training Epochs: 25
- Input Resolution: 224×224
- Confidence Threshold: 0.25
- Processing: Real-time video analysis
- Monitoring: 24/7 continuous surveillance
Educational implementation demonstrating fundamental clustering concepts in machine learning.