CoolFace
Apppublic

Harsh0809/fruit-quality-classification-cnn

sourceHugging Faceupdated 4mo agoView on Hugging Face
0likes
App README

๐ŸŽ 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.keras or fruits_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_config arguments) 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)

  1. 1.Upload: The user uploads an image (PNG, JPG, JPEG, or WEBP) up to 16MB.
  2. 2.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.
  3. 3.Normalization: The pixel matrix values are scaled to `[0.0, 1.0]` by dividing by 255.0 to match model scaling during training.
  4. 4.Execution: The input tensor is fed into the Keras model. The final layer uses a single-node dense sigmoid activation that outputs a probability value ($P$) between 0 and 1.
  5. 5.Class Mapping:
  6. 6.If $P < 0.5$: The fruit is classified as Fresh (Confidence = $(1 - P) \times 100\%$).
  7. 7.If $P \ge 0.5$: The fruit is classified as Rotten (Confidence = $P \times 100\%$).
  8. 8.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)

  1. 1.Double-click the [run.bat](file:///d:/Data%20science%20course/11-Deep%20learning/6-food_classification/run.bat) file.
  2. 2.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:

bash
# 1. Install necessary dependencies
pip install -r requirements.txt

# 2. Run the server
python app.py

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