Skip to content

A FastAPI-based web service for detecting and counting bacteria in microscopy images using a trained YOLOv8 model.

Notifications You must be signed in to change notification settings

junction-DataVim/BacteriaCountModel-IOT

Repository files navigation

🦠 Bacteria Detection API

A FastAPI-based web service for detecting and counting bacteria in microscopy images using a trained YOLOv8 model.

πŸ“‹ Table of Contents

πŸ”¬ Overview

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.

🏭 IoT Device Process

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:

πŸ”„ Automated Water Quality Assessment Process

1. Water Sampling πŸ’§

  • 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

2. Sample Preparation πŸ§ͺ

  • 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

3. Fluorescent Illumination πŸ’‘

  • 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

4. Image Capture πŸ“Έ

  • 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

5. AI Analysis πŸ€–

  • 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

6. Real-time Results πŸ“Š

  • 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

🚨 Alert System

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

πŸ“‘ Integration Features

  • 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

πŸ”§ Prototype Design

Our smart aquaculture monitoring device represents the future of automated water quality assessment:

Device Components

  • 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

Prototype Images

Prototype Device Smart aquaculture monitoring device in operation - showing the compact IoT housing with transparent chamber, LED illumination, and display screen

IoT Setup Complete IoT setup showing device integration with fish tank - demonstrating real-world deployment in aquaculture environment

Technical Specifications

  • 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

Environmental Considerations

  • 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

🌊 Real-World Applications

Smart Aquaculture Benefits

  1. Disease Prevention: Early detection of bacterial contamination prevents fish disease outbreaks
  2. Automated Monitoring: 24/7 surveillance without human intervention
  3. Cost Reduction: Reduces manual testing costs and prevents costly fish losses
  4. Quality Assurance: Ensures optimal water conditions for healthy fish growth
  5. Regulatory Compliance: Automated documentation for food safety standards
  6. Scalability: Single API can handle multiple monitoring devices across farms

Use Cases

  • 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

πŸ”¬ Scientific Innovation

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

✨ Features

  • 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

πŸ“‹ Prerequisites

  • Python 3.8 or higher
  • Virtual environment (recommended)
  • Trained YOLOv8 model file (bacteria_detector_final_n.pt)

System Dependencies

sudo apt update
sudo apt install -y build-essential liblzma-dev zlib1g-dev libssl-dev libffi-dev

πŸš€ Installation

  1. Clone or download the project

    cd "/home/mounir/Desktop/Junction/Bacteria Count"
  2. Activate your virtual environment

    ai  # Or your virtual environment activation command
  3. Install Python dependencies

    pip install -r requirements.txt

πŸ“¦ Requirements

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

🎯 Usage

Starting the API Server

  1. Ensure your model file is present

    • Make sure bacteria_detector_final_n.pt is in the project directory
  2. Start the server

    python main.py
  3. Verify the server is running

    • Open your browser and go to http://localhost:8000
    • You should see the API status message

API Documentation

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

🌐 API Endpoints

GET /

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"
  }
}

GET /health

Detailed health status

Response:

{
  "status": "healthy",
  "model_loaded": true
}

POST /detect/

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 filename
  • 500 Internal Server Error: Processing error
  • 503 Service Unavailable: Model not loaded

πŸ§ͺ Testing

Using the Test Script

A comprehensive test script is included to test the API with sample images:

python test_api.py

This will:

  • Test all images in the test images/ folder
  • Display detection results for each image
  • Provide a summary report

Manual Testing with cURL

# 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"

Using Python Requests

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())

πŸ“ Project Structure

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

πŸ€– Model Information

  • Architecture: YOLOv8 Nano (YOLOv8n)
  • Task: Object Detection
  • Classes: 1 (bacteria)
  • Input Size: 640x640 pixels
  • Model File: bacteria_detector_final_n.pt

Model Performance

  • Training: Custom dataset with artificial fluorescent bacteria images
  • Validation: Trained with early stopping and periodic saving
  • Optimization: AdamW optimizer with learning rate scheduling

πŸ”§ Troubleshooting

Common Issues

  1. "Model not found" error

    Solution: Ensure bacteria_detector_final_n.pt is in the project directory
    
  2. "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
  3. "Model not loaded" error

    Check the server logs for detailed error messages
    Verify the model file exists and is not corrupted
    
  4. Slow inference

    Consider using a GPU for faster inference
    Reduce image size before uploading
    

Log Messages

  • βœ… 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

πŸ“Š Performance Tips

  1. Image Size: Smaller images (< 1MB) process faster
  2. Format: PNG and JPG are recommended formats
  3. Batch Processing: Use the test script for multiple images
  4. Memory: Monitor memory usage for large images

πŸ”’ Security Considerations

  • File type validation is implemented
  • Temporary files are automatically cleaned up
  • Consider adding authentication for production use
  • Implement rate limiting for public APIs

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

πŸ“ License

This project is developed for research and educational purposes.

πŸ“ž Support

For issues and questions:

  1. Check the troubleshooting section
  2. Review the server logs
  3. Test with the provided sample images
  4. Verify all dependencies are installed

Happy bacteria detecting! πŸ¦ πŸ”¬

About

A FastAPI-based web service for detecting and counting bacteria in microscopy images using a trained YOLOv8 model.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published