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.
- Overview
- Features
- Installation
- Usage
- Project Structure
- Model Architecture
- Dataset
- Results
- API Documentation
- Technologies Used
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
- 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
- Python 3.8 or higher
- CUDA-capable GPU (optional, for faster training)
-
Clone the repository
git clone <repository-url> cd Banana-lifespan
-
Create a virtual environment
python -m venv venv
-
Activate the virtual environment
- Windows:
venv\Scripts\activate
- Linux/Mac:
source venv/bin/activate
- Windows:
-
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.txtwith: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
-
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
- Ensure images are organized in
-
Train the model
python tests.py
The trained model will be saved to
models/model_1
-
Start the backend server
python backend.py
The API will be available at
http://localhost:5000 -
Access the web interface
- Open
banana-app.htmlin your web browser - Or serve it using a local server:
Then navigate to
python -m http.server 8000
http://localhost:8000/banana-app.html
- Open
-
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"
POST /api/predict- Upload a banana image for freshness predictionGET /api/health- Health check endpointGET /docs- Interactive API documentation (Swagger UI)GET /redoc- Alternative API documentation (ReDoc)
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
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
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
Here are predictions on sample test images:
- 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
The model classifies bananas into 5 ripeness categories:
- Unripe (6+ days): Still green, wait 2-3 days for optimal ripeness
- Perfect (4-5 days): Peak ripeness, eat within next few days
- Ripe (2-3 days): Getting soft with brown spots, eat soon
- Very Ripe (1 day): Very soft with many brown spots, use immediately
- Overripe (0 days): Past prime, best for baking or freezing
POST /api/predict
Upload a banana image to get freshness prediction.
Request:
- Content-Type:
multipart/form-data - Body: Form data with
imagefield (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())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
- 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:5000by default


