Harsh0809/fruit-quality-classification-cnn
๐ SavorShield - Fruit Freshness Classification System
SavorShield is an interactive deep learning web application that uses a Keras Convolutional Neural Network (CNN) to instantly classify fruits as Fresh or Rotten. It features a modern Flask-based Python backend and a premium glassmorphic dark-mode web user interface styled with Vanilla CSS and high-fidelity Javascript micro-interactions.
๐ Live Demo on Hugging Face Spaces:
๐ [SavorShield Live Classifier](https://huggingface.co/spaces/Harsh0809/fruit-quality-classification-cnn)
๐ Key Features
- Instant CNN Inference: Pre-loads the 134MB Keras model (
fruits_fresh_rotten_model.kerasorfruits_fresh_rotten_model.h5) at server startup to deliver quick prediction times. - Compatibility Patching: Features a custom runtime patch to resolve Keras 2/3 serialization mismatches (
quantization_configarguments) dynamically at startup, guaranteeing stable loading. - Drag-and-Drop Dropzone: Implemented with responsive drag events, instant client-side preview, and verification checks.
- Dynamic Visual Themes: The interface adapts to predictions:
- Vibrant Emerald theme for Fresh fruits.
- Amber/Crimson theme for Rotten fruits.
- Conic-Gradient Progress Gauge: Smoothly animates the confidence level returned by the sigmoid layer.
- Latency & Architecture Stats: Displays neural network characteristics (Inference delay, model sequence layers, total latency, and exact sigmoid probabilities).
- Single-Click Portability: Windows batch script wrapper (
run.bat) to automate installation and host launching.
๐ Project Architecture
d:\Data science course\11-Deep learning\6-food_classification\
โโโ fruits_fresh_rotten_model.keras # Pre-trained Keras Model file
โโโ fruits_fresh_rotten_model.h5 # Fallback Keras Model file
โโโ app.py # Flask Web Server & Inference Pipeline (with Keras 2/3 compatibility patches)
โโโ requirements.txt # Environment Dependencies (optimized for tensorflow-cpu)
โโโ run.bat # Windows Environment Setup & Launcher
โโโ Dockerfile # Deployment configuration for Hugging Face Spaces
โโโ README.md # Project Documentation
โโโ templates/
โ โโโ index.html # Semantic Web Structure
โโโ static/
โโโ css/
โ โโโ style.css # Custom CSS Stylesheet & Glassmorphic variables
โโโ js/
โโโ main.js # Javascript Controller & API Fetchesโ๏ธ How It Works (The Inference Pipeline)
- Upload: The user uploads an image (PNG, JPG, JPEG, or WEBP) up to 16MB.
- Preprocessing: The Flask application opens the image stream via Pillow, converts it to RGB color channels, and resizes it to $224 \times 224$ pixels matching the model input shape requirement.
- Normalization: The pixel matrix values are scaled to `[0.0, 1.0]` by dividing by
255.0to match model scaling during training. - Execution: The input tensor is fed into the Keras model. The final layer uses a single-node
densesigmoid activation that outputs a probability value ($P$) between0and1. - Class Mapping:
- If $P < 0.5$: The fruit is classified as Fresh (Confidence = $(1 - P) \times 100\%$).
- If $P \ge 0.5$: The fruit is classified as Rotten (Confidence = $P \times 100\%$).
- Result Transformation: The JSON output triggers theme state overrides (
.is-fresh/.is-rotten) in JavaScript, transitioning the UI context theme.
โ๏ธ Hugging Face Spaces Docker Deployment
SavorShield is fully Dockerized and deployed under Hugging Face Spaces utilizing:
- `python:3.10-slim` base image to keep image overhead minimal.
- Standard Port `7860` for proper routing inside Space containers.
- `tensorflow-cpu>=2.16.1` to leverage Keras 3 while remaining lightweight and avoiding out-of-memory errors.
- Git LFS (Large File Storage) tracking rules configured to handle Keras model weight files larger than 10MB.
๐ Installation & Local Launch
Option A: Double-Click Launcher (Recommended for Windows)
- Double-click the [run.bat](file:///d:/Data%20science%20course/11-Deep%20learning/6-food_classification/run.bat) file.
- It will automatically check for Python, verify dependencies (installing Flask, Pillow, and Numpy if missing), and start the server.
Option B: Terminal Command Line Setup
Navigate to this directory using your terminal and run:
# 1. Install necessary dependencies
pip install -r requirements.txt
# 2. Run the server
python app.pyAccessing the UI
Open your favorite web browser and go to: ๐ [http://localhost:5000](http://localhost:5000) (Locally) or [Hugging Face Live Demo](https://huggingface.co/spaces/Harsh0809/fruit-quality-classification-cnn) (Cloud)
