Skip to content

chenggaol/Banana-lifespan

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Banana Lifespan Prediction

A deep learning project that predicts how many days until a banana goes bad using computer vision. The model analyzes banana images and provides freshness estimates, ripeness status, and storage recommendations.

Table of Contents

Overview

This project uses a convolutional neural network (CNN) to predict banana freshness by analyzing images. The model was trained on a custom dataset of banana images captured over multiple days, learning to associate visual features (color, texture, spots) with the number of days until the banana becomes overripe.

The system consists of:

  • Backend API: FastAPI server that processes images and returns predictions
  • Frontend Interface: Beautiful web interface for uploading and analyzing banana images
  • Deep Learning Model: Custom CNN architecture trained on regression task

Features

  • Image Analysis: Upload banana photos to get instant freshness predictions
  • Detailed Predictions:
    • Days until banana goes bad
    • Ripeness status (Unripe, Perfect, Ripe, Very Ripe, Overripe)
    • Confidence score
    • Storage recommendations
  • Modern UI: Beautiful, responsive web interface with gradient animations
  • Fast API: RESTful API with automatic documentation
  • Real-time Processing: Quick inference using optimized PyTorch model

Installation

Prerequisites

  • Python 3.8 or higher
  • CUDA-capable GPU (optional, for faster training)

Setup

  1. Clone the repository

    git clone <repository-url>
    cd Banana-lifespan
  2. Create a virtual environment

    python -m venv venv
  3. Activate the virtual environment

    • Windows:
      venv\Scripts\activate
    • Linux/Mac:
      source venv/bin/activate
  4. Install dependencies

    pip install torch torchvision
    pip install fastapi uvicorn
    pip install pillow numpy pandas scikit-learn
    pip install python-multipart

    Or create a requirements.txt with:

    torch>=2.0.0
    torchvision>=0.15.0
    fastapi>=0.100.0
    uvicorn>=0.23.0
    pillow>=10.0.0
    numpy>=1.24.0
    pandas>=2.0.0
    scikit-learn>=1.3.0
    python-multipart>=0.0.6
    

Usage

Training the Model

  1. Prepare the dataset

    • Ensure images are organized in creating_dataset/banana_dataset/Day X/ folders
    • Generate the CSV file:
      cd creating_dataset
      python generate_csv.py
  2. Train the model

    python tests.py

    The trained model will be saved to models/model_1

Running the API Server

  1. Start the backend server

    python backend.py

    The API will be available at http://localhost:5000

  2. Access the web interface

    • Open banana-app.html in your web browser
    • Or serve it using a local server:
      python -m http.server 8000
      Then navigate to http://localhost:8000/banana-app.html
  3. Use the API directly

    curl -X POST "http://localhost:5000/api/predict" \
         -H "accept: application/json" \
         -H "Content-Type: multipart/form-data" \
         -F "image=@path/to/banana/image.jpg"

API Endpoints

  • POST /api/predict - Upload a banana image for freshness prediction
  • GET /api/health - Health check endpoint
  • GET /docs - Interactive API documentation (Swagger UI)
  • GET /redoc - Alternative API documentation (ReDoc)

Project Structure

Banana-lifespan/
├── backend.py              # FastAPI server and prediction endpoint
├── model.py                # CNN model architecture
├── training.py             # Training pipeline and utilities
├── dataloading.py          # Dataset class and data loaders
├── tests.py                # Training script
├── banana-app.html         # Frontend web interface
├── models/
│   └── model_1            # Trained model weights
├── creating_dataset/
│   ├── banana_dataset/    # Image dataset organized by day
│   │   ├── Day 1/
│   │   ├── Day 10/
│   │   └── ...
│   ├── bananadata.csv     # Dataset metadata
│   └── generate_csv.py    # Script to generate CSV from images
└── Test_images/           # Test images for validation
    ├── Test photo #1.png
    ├── Test photo #2.png
    └── Test photo #3.png

Model Architecture

The model uses a custom CNN architecture with the following structure:

  • 5 Convolutional Blocks: Each with Conv2d, BatchNorm2d, ReLU, and MaxPool2d
    • Block 1: 3 → 32 channels
    • Block 2: 32 → 64 channels
    • Block 3: 64 → 128 channels
    • Block 4: 128 → 256 channels
    • Block 5: 256 → 512 channels
  • Classifier: Flatten + Linear layer (512×7×7 → 1)
  • Output: Single regression value (days until bad)

Training Details:

  • Loss Function: Mean Squared Error (MSE)
  • Optimizer: Adam (learning rate: 0.005)
  • Batch Size: 32
  • Image Size: 225×225 pixels
  • Data Augmentation: Random horizontal flips, rotations, color jitter

Dataset

The dataset consists of banana images captured over multiple days:

  • Images organized by day (Day 1, Day 10, Day 12, etc.)
  • Each image labeled with the day number
  • Total: ~900+ images across different ripening stages
  • Format: JPG images
  • Preprocessing: Resized to 225×225, normalized with ImageNet statistics

The dataset is structured as:

banana_dataset/
├── Day 1/    (Fresh, unripe bananas)
├── Day 10/   (Ripening bananas)
├── Day 12/   (Ripe/overripe bananas)
└── ...

##Results

Test Images Predictions

Here are predictions on sample test images:

Test Image #1

Test Image 1

Test Image #2

Test Image 2

Test Image #3

Test Image 3

Model Performance

  • Training Loss: Achieved validation loss < 3.0
  • Input Size: 225×225 RGB images
  • Inference Speed: ~50-100ms per image (GPU), ~200-500ms (CPU)
  • Accuracy: Model trained on regression task predicting days until bad

Ripeness Categories

The model classifies bananas into 5 ripeness categories:

  1. Unripe (6+ days): Still green, wait 2-3 days for optimal ripeness
  2. Perfect (4-5 days): Peak ripeness, eat within next few days
  3. Ripe (2-3 days): Getting soft with brown spots, eat soon
  4. Very Ripe (1 day): Very soft with many brown spots, use immediately
  5. Overripe (0 days): Past prime, best for baking or freezing

API Documentation

Prediction Endpoint

POST /api/predict

Upload a banana image to get freshness prediction.

Request:

  • Content-Type: multipart/form-data
  • Body: Form data with image field (file)

Response:

{
  "daysUntilBad": 4,
  "confidence": 0.90,
  "ripeness": "Perfect",
  "recommendation": "Peak ripeness! Eat within the next few days for best taste and texture. Store at room temperature."
}

Example using curl:

curl -X POST "http://localhost:5000/api/predict" \
     -F "image=@banana.jpg"

Example using Python:

import requests

with open('banana.jpg', 'rb') as f:
    response = requests.post(
        'http://localhost:5000/api/predict',
        files={'image': f}
    )
    print(response.json())

Health Check

GET /api/health

Returns API status.

Response:

{
  "status": "healthy",
  "message": "Banana Freshness API is running"
}

##Technologies Used

  • PyTorch: Deep learning framework for model training and inference
  • FastAPI: Modern, fast web framework for building the API
  • Uvicorn: ASGI server for running FastAPI
  • Pillow (PIL): Image processing
  • NumPy: Numerical computations
  • Pandas: Data manipulation for dataset handling
  • scikit-learn: Data splitting utilities
  • React: Frontend framework (via CDN)
  • Tailwind CSS: Utility-first CSS framework for styling

Notes

  • The model expects images to be 225×225 pixels (not the standard 224×224)
  • Image preprocessing must match training transforms exactly
  • Model uses ImageNet normalization statistics
  • GPU is recommended for training but not required for inference
  • The frontend connects to http://localhost:5000 by default

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published