imageO is an intelligent web application that leverages computer vision and machine learning to detect edible oil adulteration in milk samples through image analysis. The system combines advanced background removal techniques with deep learning classification to provide accurate, real-time detection of milk quality.
- Automated Background Removal: Utilizes U²-Net neural network for precise subject isolation
- Advanced Image Processing: Multi-stage enhancement pipeline including cropping, color correction, and contrast optimization
- AI-Powered Classification: Binary classification system distinguishing between pure milk and oil-adulterated samples
- Secure Authentication: Firebase-based user authentication with session management
- Production Ready: Comprehensive logging, health monitoring, and error handling
imageO/
├── app/
│ ├── __init__.py # Application factory and configuration
│ ├── config.py # Environment and Flask configurations
│ ├── auth/ # Authentication module
│ │ ├── decorators.py # Login required decorators
│ │ └── routes.py # Authentication endpoints
│ ├── main/ # Core application routes
│ │ └── routes.py # Main endpoints and prediction API
│ ├── models/ # Model initialization and management
│ │ └── models.py # U²-Net and classification model setup
│ ├── services/ # Business logic services
│ │ ├── firebase_service.py # Firebase Admin SDK integration
│ │ ├── image_processing.py # Image preprocessing pipeline
│ │ └── prediction.py # ML inference service
│ └── utils/ # Utility modules
│ ├── logging_config.py # Structured logging configuration
│ └── validators.py # Input validation utilities
├── templates/ # Jinja2 HTML templates
├── static/ # Static assets (CSS, JavaScript, images)
├── run.py # Application entry point
├── Dockerfile # Container configuration
└── requirements.txt # Python dependencies
- Background Removal: U²-Net model removes background elements for focused analysis
- Foreground Extraction: Alpha mask-based cropping to isolate the milk sample
- Color Correction: White balance adjustment and blue tint correction
- Enhancement: Gamma correction and CLAHE (Contrast Limited Adaptive Histogram Equalization)
- Standardization: Resize to 224×224 pixels for model input
- Background Removal: U²-Net ONNX model via
rembglibrary - Classification: Custom TensorFlow/Keras model trained for milk adulteration detection
- Model Repository: Hosted on Hugging Face Hub for reliable access and versioning
- Client authenticates using Firebase Web SDK
- Firebase ID token exchanged for server-side session
- Protected routes validate session before processing requests
- Secure logout clears both client and server sessions
- Python 3.12 or higher
- Firebase project with Web App configuration
- Hugging Face account (optional, for faster model downloads)
Create a .env file in the project root:
FIREBASE_CREDS=<base64-encoded-firebase-service-account-json>
HF_TOKEN=<hugging-face-token>
SECRET_KEY=<flask-secret-key>
FLASK_ENV=production
PORT=7860Windows PowerShell:
[Convert]::ToBase64String([IO.File]::ReadAllBytes("path\to\firebase-credentials.json"))Linux/macOS:
base64 -i path/to/firebase-credentials.json-
Clone the repository:
git clone https://github.com/yourusername/imageO.git cd imageO -
Create virtual environment:
python -m venv venv source venv/bin/activate # Linux/macOS # or venv\Scripts\activate # Windows
-
Install dependencies:
pip install --upgrade pip pip install -r requirements.txt
-
Set environment variables:
export FIREBASE_CREDS="<base64-encoded-credentials>" export HF_TOKEN="<your-huggingface-token>"
-
Run the application:
python run.py
The application will be available at http://localhost:7860
# Build the image
docker build -t imageo .
# Run the container
docker run -p 7860:7860 \
-e FIREBASE_CREDS="<base64-credentials>" \
-e HF_TOKEN="<huggingface-token>" \
imageo| Endpoint | Method | Description |
|---|---|---|
/auth/login |
GET | Login page with Firebase authentication |
/auth/sessionLogin |
POST | Exchange Firebase ID token for session |
/auth/logout |
GET | Clear user session |
| Endpoint | Method | Auth Required | Description |
|---|---|---|---|
/ |
GET | ✓ | Main application interface |
/predict |
POST | ✓ | Image analysis and classification |
/health |
GET | ✗ | System health and model status |
Request:
{
"image": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEA..."
}Response:
{
"predicted_class": "Milk",
"confidence": 0.95,
"cropped_image": "data:image/jpeg;base64,..."
}Example Usage:
curl -X POST http://localhost:7860/predict \
-H "Content-Type: application/json" \
-H "Cookie: session=<session-cookie>" \
-d '{"image": "data:image/jpeg;base64,<base64-image>"}'- Maximum Upload Size: 16 MB
- Supported Image Formats: JPEG, JPG, PNG, WebP
- Image Processing Timeout: 30 seconds
- Session Duration: 24 hours
The application includes comprehensive logging and monitoring:
- Structured Logging: JSON-formatted logs with timestamps and severity levels
- Log Rotation: Automatic log file rotation to manage disk usage
- Health Checks: Real-time monitoring of model availability and system status
- Error Tracking: Detailed error reporting with stack traces
Logs are written to logs/milk_detector.log and console output.
- Session-Based Authentication: Secure server-side session management
- Input Validation: Comprehensive validation of image uploads and requests
- CSRF Protection: Built-in Flask security measures
- Environment Isolation: Sensitive credentials stored as environment variables
- Production Security: Security headers and HTTPS-ready configuration
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- U²-Net for background removal capabilities
- rembg for ONNX model integration
- TensorFlow for machine learning framework
- Firebase for authentication services
- Hugging Face for model hosting and distribution
For questions, issues, or contributions, please:
- Open an issue on GitHub
- Contact the development team