CoolFace
Apppublic

Cafendcode/dragon-fruit-quality-assessment

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

Dragon Fruit Quality Assessment System

This repository contains the complete codebase for the Dragon Fruit Quality Assessment System, a production-ready AI-powered web application designed using Transfer Learning with TensorFlow, FastAPI, Streamlit, and SQLite.

Objective

Develop a real-world system to automatically grade the visual quality of dragon fruits and diagnose crop diseases. The system identifies:

  1. 1.Fruit Quality: Fresh, Good Quality, Medium Quality, Poor Quality, Rotten
  2. 2.Diseases: Anthracnose, Stem Rot, Fruit Rot, Healthy Fruit
  3. 3.Severity: Mild, Moderate, Severe (computed dynamically via Grad-CAM defect active area ratio)
  4. 4.Actionable Recommendations: Possible causes, field prevention methods, chemical/organic treatment sprays, cold-chain storage parameters, and market suitability values.

Technical Stack

  • —Deep Learning: TensorFlow & Keras (MobileNetV2, DenseNet121, NASNetMobile)
  • —Frontend Dashboard: Streamlit (with dark theme, Plotly charts, HTML gauges)
  • —Backend API: FastAPI (REST endpoints for prediction, logs, and reports)
  • —Database: SQLite (for indexing operators, image paths, and assessment logs)
  • —Explainable AI: Grad-CAM (visual focus map highlighting tissue defects)
  • —PDF Generation: FPDF2 (compiled report cards including images and Grad-CAM side-by-side)
  • —Package Manager: UV (fast environment compiler and runner)

Directory Structure

DragonFruitAI/
│
├── .streamlit/             # Streamlit theme configuration
├── api/                    # FastAPI endpoints (main.py)
├── database/               # SQLite database logs and queries (db.py)
├── dataset/                # resturctured train/val/test splits (created by prepare_dataset.py)
├── models/                 # Saved .h5 models, charts, and metrics JSON
├── reports/                # Output PDF report cards
├── static/                 # Uploaded images and Grad-CAM outputs
├── utils/                  # Grad-CAM, PDF report builder, voice synthesis
├── Dockerfile              # Docker image configuration
├── Procfile                # Heroku/Render/Railway execution instruction
├── prepare_dataset.py      # Script to resturcture and split images
├── train.py                # Pipeline script to train and evaluate the 3 models
├── predict.py              # Prediction and Grad-CAM calculation runner
├── requirements.txt        # Lockfile of python dependencies
└── README.md               # System documentation

Setup & Installation

Prerequisites

  • —Install Python 3.11 (or let uv download it automatically).
  • —Make sure uv is installed: pip install uv or via the installation instructions.

1. Create Environment & Install Packages

Run the following commands in your terminal:

bash
# Create python 3.11 virtual environment
uv venv --python 3.11 venv

# Activate the virtual environment
# On Windows:
venv\Scripts\activate
# On Linux/macOS:
source venv/bin/activate

# Install dependencies
uv pip install -r requirements.txt

2. Prepare the Dataset

The raw dataset files in the workspace are restructured and split into dataset/ by running:

bash
python prepare_dataset.py

This distributes the healthy fruit/leaf to the healthy class, and distributes diseased fruit/leaf deterministically using name-based MD5 hashing into: rotten, anthracnose, stem_rot, fruit_rot. It splits the data into 70% train, 15% validation, and 15% test splits.

3. Run the Deep Learning Training Pipeline

Train all three models (MobileNetV2, DenseNet121, and NASNetMobile) and automatically select the best model:

bash
python train.py

This script:

  • —Loads the dataset and applies data augmentation, resizing (224x224), and normalization.
  • —Runs transfer learning followed by fine-tuning on the last layer blocks.
  • —Compares models using Accuracy, Precision, Recall, F1 Score, and Confusion Matrix.
  • —Saves the models as mobilenetv2.h5, densenet121.h5, and nasnetmobile.h5 in the models/ directory.
  • —Plots training accuracy/loss curves and confusion matrices.
  • —Saves evaluation comparison to models/evaluation_metrics.json.
  • —Identifies the highest accuracy model and writes its name to models/best_model_config.json.

Running the Application

Once the models are trained, run the backend API and frontend dashboard concurrently:

1. Start the FastAPI Backend

bash
uvicorn api.main:app --host 127.0.0.1 --port 8000

FastAPI Swagger documentation will be available at http://127.0.0.1:8000/docs.

2. Start the Streamlit Frontend

bash
streamlit run app.py

Open your browser and navigate to http://localhost:8501.


API Endpoints

  • —POST /predict?user_name=Operator: Uploads an image, processes inference, logs to SQLite DB, returns diagnostic metrics.
  • —GET /history: Returns a list of past scans.
  • —GET /report?prediction_id=ID: Returns the generated PDF report card.

Docker Deployment

Build and run the container locally:

bash
docker build -t dragon-fruit-ai .
docker run -p 8000:8000 -p 8501:8501 dragon-fruit-ai
  • —API will be accessible on port 8000.
  • —Streamlit dashboard will be accessible on port 8501.