A FastAPI-based web service for detecting and counting bacteria in microscopy images using a trained YOLOv8 model.
- Overview
- IoT Device Process
- Prototype Design
- Features
- Prerequisites
- Installation
- Usage
- API Endpoints
- Testing
- Project Structure
- Model Information
- Troubleshooting
- Contributing
This API provides automated bacteria detection and counting capabilities for microscopy images. It uses a custom-trained YOLOv8 model to identify bacteria and return:
- Total bacteria count
- Bounding box coordinates for each detection
- Confidence scores
- Additional metadata
This API is designed to be integrated into a smart aquaculture monitoring system that provides real-time water quality assessment through automated bacteria detection.
The bacteria detection API is part of a comprehensive IoT-based water quality monitoring system for smart aquaculture farms. Here's how the complete process works:
- A robotic pump automatically collects water samples from the fish tank
- The sample is drawn into a transparent chamber within the IoT device
- Sampling occurs at regular intervals or on-demand
- The collected water sample is mixed with fluorescent staining agents
- Chemical reagents are automatically dispensed to highlight microbial DNA
- The mixture is allowed to settle for optimal staining
- A glowing blue LED or UV light activates underneath the chamber
- The illumination causes stained microbial DNA to fluoresce
- Green or orange fluorescent glow indicates the presence of bacteria
- The fluorescence intensity correlates with bacterial concentration
- A high-resolution camera captures images of the fluorescent sample
- Multiple images may be taken for better accuracy
- Images are optimized for bacterial detection algorithms
- The captured images are sent to this bacteria detection API
- Our trained YOLOv8 model processes the fluorescent bacteria images
- The AI identifies and counts individual bacteria in the sample
- Confidence scores are calculated for each detection
- Bacteria count is instantly displayed on the device's screen
- Water quality status is determined based on bacterial load
- Warning alerts are triggered if contamination levels exceed thresholds
- Results are logged and can be transmitted to monitoring systems
The system provides immediate feedback:
- π’ Clean Water: Low bacterial count, safe for aquaculture
- π‘ Monitor: Moderate levels, requires attention
- π΄ Contaminated Water Detected: High bacterial load, immediate action required
- Real-time Monitoring: Continuous water quality assessment
- Remote Access: API endpoints allow remote monitoring
- Data Logging: Historical bacteria count data for trend analysis
- Automated Alerts: Instant notifications when contamination is detected
- Scalable: Can monitor multiple tanks simultaneously
Our smart aquaculture monitoring device represents the future of automated water quality assessment:
- Compact IoT Housing: Modern, waterproof enclosure
- Robotic Sampling System: Automated water collection mechanism
- Fluorescent Chamber: Transparent viewing area for sample analysis
- LED/UV Illumination: Blue light system for fluorescent activation
- High-Resolution Camera: Specialized imaging for bacterial detection
- Display Screen: Real-time results and status information
- Connectivity Module: Wi-Fi/cellular for data transmission
Smart aquaculture
monitoring device in operation - showing the compact IoT housing with
transparent chamber, LED illumination, and display screen
Complete IoT setup showing
device integration with fish tank - demonstrating real-world deployment in
aquaculture environment
- Sampling Volume: 1-5 mL per test
- Detection Time: < 30 seconds from sample to result
- Accuracy: >95% bacteria detection rate
- Operating Range: 0-40Β°C, pH 6-9
- Power: Low-power IoT design with battery backup
- Connectivity: Wi-Fi, Bluetooth, optional cellular
- Data Storage: Local and cloud-based logging
- Aquaculture-Safe: All materials are fish-safe and non-toxic
- Waterproof Design: IP67 rating for harsh aquatic environments
- Easy Maintenance: Automated cleaning cycles and replaceable components
- Minimal Interference: Quiet operation that doesn't disturb aquatic life
- Disease Prevention: Early detection of bacterial contamination prevents fish disease outbreaks
- Automated Monitoring: 24/7 surveillance without human intervention
- Cost Reduction: Reduces manual testing costs and prevents costly fish losses
- Quality Assurance: Ensures optimal water conditions for healthy fish growth
- Regulatory Compliance: Automated documentation for food safety standards
- Scalability: Single API can handle multiple monitoring devices across farms
- Fish Farms: Continuous monitoring of breeding tanks and ponds
- Hatcheries: Critical water quality control for juvenile fish
- Research Facilities: Precise bacterial load measurements for studies
- Aquaponics Systems: Integrated monitoring for plant and fish health
- Shellfish Farms: Water quality assurance for mollusc cultivation
This system represents a breakthrough in aquaculture technology by:
- Combining IoT and AI: Integration of hardware automation with machine learning
- Fluorescent Detection: Advanced staining techniques for precise bacterial identification
- Real-time Processing: Instant results enabling immediate corrective actions
- Non-invasive Monitoring: Continuous assessment without disrupting aquatic ecosystems
- Data-Driven Decisions: Historical data analysis for predictive water quality management
- Fast Detection: Real-time bacteria detection using YOLOv8
- REST API: Easy integration with web applications and services
- Multiple Formats: Supports various image formats (PNG, JPG, TIFF, BMP)
- Detailed Results: Returns bounding boxes, confidence scores, and metadata
- Error Handling: Comprehensive error handling and validation
- CORS Support: Cross-origin resource sharing enabled
- Interactive Docs: Automatic API documentation with Swagger UI
- Python 3.8 or higher
- Virtual environment (recommended)
- Trained YOLOv8 model file (
bacteria_detector_final_n.pt)
sudo apt update
sudo apt install -y build-essential liblzma-dev zlib1g-dev libssl-dev libffi-dev-
Clone or download the project
cd "/home/mounir/Desktop/Junction/Bacteria Count"
-
Activate your virtual environment
ai # Or your virtual environment activation command -
Install Python dependencies
pip install -r requirements.txt
The project uses the following Python packages:
fastapi==0.104.1
uvicorn[standard]==0.24.0
ultralytics==8.0.200
opencv-python-headless==4.8.1.78
numpy==1.24.3
Pillow==10.1.0
python-multipart==0.0.6
-
Ensure your model file is present
- Make sure
bacteria_detector_final_n.ptis in the project directory
- Make sure
-
Start the server
python main.py
-
Verify the server is running
- Open your browser and go to
http://localhost:8000 - You should see the API status message
- Open your browser and go to
Once the server is running, you can access:
- Interactive API Docs:
http://localhost:8000/docs - ReDoc Documentation:
http://localhost:8000/redoc - OpenAPI JSON:
http://localhost:8000/openapi.json
Health check and API information
Response:
{
"message": "Bacteria Detection API is running",
"model_loaded": true,
"endpoints": {
"detect": "/detect/ (POST) - Upload image for bacteria detection",
"docs": "/docs - API documentation"
}
}Detailed health status
Response:
{
"status": "healthy",
"model_loaded": true
}Detect bacteria in an uploaded image
Request:
- Method:
POST - Content-Type:
multipart/form-data - Body: Image file (PNG, JPG, JPEG, TIFF, BMP)
Response:
{
"bacteria_count": 15,
"detections": [
{
"id": 1,
"bbox": [123.45, 67.89, 156.78, 98.12],
"confidence": 0.89,
"center_x": 140.12,
"center_y": 82.01,
"width": 33.33,
"height": 30.23
}
],
"image_info": {
"filename": "bacteria_sample.png",
"size_bytes": 245760
}
}Error Responses:
400 Bad Request: Invalid file format or missing filename500 Internal Server Error: Processing error503 Service Unavailable: Model not loaded
A comprehensive test script is included to test the API with sample images:
python test_api.pyThis will:
- Test all images in the
test images/folder - Display detection results for each image
- Provide a summary report
# Test a single image
curl -X POST "http://localhost:8000/detect/" \
-H "accept: application/json" \
-H "Content-Type: multipart/form-data" \
-F "file=@test_images/11.png"import requests
# Test the API
with open("test_images/11.png", "rb") as f:
files = {"file": ("11.png", f, "image/png")}
response = requests.post("http://localhost:8000/detect/", files=files)
print(response.json())Bacteria Count/
βββ main.py # FastAPI application
βββ test_api.py # API testing script
βββ requirements.txt # Python dependencies
βββ bacteria_detector_final_n.pt # Trained YOLO model
βββ bacteria-count.ipynb # Training notebook
βββ Prototype image/ # IoT device prototype images
β βββ Prototype1.webp # Device operation view
β βββ Prototype2.webp # Complete setup view
βββ test images/ # Sample test images
β βββ 11.png
β βββ 12.png
β βββ 134.png
β βββ 165.png
β βββ 184.png
β βββ 243.png
βββ README.md # This file
- Architecture: YOLOv8 Nano (YOLOv8n)
- Task: Object Detection
- Classes: 1 (bacteria)
- Input Size: 640x640 pixels
- Model File:
bacteria_detector_final_n.pt
- Training: Custom dataset with artificial fluorescent bacteria images
- Validation: Trained with early stopping and periodic saving
- Optimization: AdamW optimizer with learning rate scheduling
-
"Model not found" error
Solution: Ensure bacteria_detector_final_n.pt is in the project directory -
"ModuleNotFoundError: No module named '_lzma'" error
# Install system dependencies sudo apt install -y liblzma-dev build-essential # Reinstall Python packages pip install --force-reinstall ultralytics
-
"Model not loaded" error
Check the server logs for detailed error messages Verify the model file exists and is not corrupted -
Slow inference
Consider using a GPU for faster inference Reduce image size before uploading
- β Model loaded successfully: API is ready to process images
- β Error loading model: Check model file and dependencies
- π Processing image: Normal operation
β οΈ Model failed to load: Check error logs
- Image Size: Smaller images (< 1MB) process faster
- Format: PNG and JPG are recommended formats
- Batch Processing: Use the test script for multiple images
- Memory: Monitor memory usage for large images
- File type validation is implemented
- Temporary files are automatically cleaned up
- Consider adding authentication for production use
- Implement rate limiting for public APIs
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is developed for research and educational purposes.
For issues and questions:
- Check the troubleshooting section
- Review the server logs
- Test with the provided sample images
- Verify all dependencies are installed
Happy bacteria detecting! π¦ π¬