Skip to content

isthatlak/fruit-freshness-classifier

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

10 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🍎 Fruit Freshness Classifier

Python TensorFlow Streamlit License: MIT

An AI-powered web application that classifies fruit freshness using deep learning. Upload an image of an apple, banana, or orange, and the model will determine whether it's fresh or rotten with confidence scores and detailed visualizations.

🎯 Features

  • Real-time Fruit Classification: Instantly classify fruits as fresh or rotten
  • Multiple Input Methods: Upload images or use camera for live capture
  • Interactive Visualizations: Confidence gauges and probability charts using Plotly
  • 6 Fruit Categories: Fresh & rotten apples, bananas, and oranges
  • Transfer Learning: Built on MobileNetV2 for optimal performance
  • Class Imbalance Handling: Weighted training for balanced predictions
  • User-friendly Interface: Streamlit-powered responsive web app

πŸš€ Live Demo

Try the app here!

πŸ“Έ Screenshots

App Interface Prediction Results

πŸ› οΈ Technology Stack

  • Deep Learning: TensorFlow 2.13+, Keras
  • Computer Vision: OpenCV, PIL
  • Web Framework: Streamlit
  • Data Science: NumPy, Pandas, Scikit-learn
  • Visualizations: Plotly, Matplotlib, Seaborn
  • Model Architecture: MobileNetV2 + Transfer Learning

πŸ“¦ Installation

Prerequisites

  • Python 3.8 or higher
  • pip package manager
  • Virtual environment (recommended)

Step 1: Clone the Repository

git clone https://github.com/yourusername/fruit-freshness-classifier.git
cd fruit-freshness-classifier

Step 2: Create Virtual Environment

python -m venv venv
# Windows
venv\Scripts\activate
# macOS/Linux
source venv/bin/activate

Step 3: Install Dependencies

pip install -r requirements.txt

Step 4: Run the Application

streamlit run streamlit_app.py

The app will open in your browser at http://localhost:8501

πŸ—οΈ Project Structure

fruit-freshness-classifier/
β”‚
β”œβ”€β”€ streamlit_app.py              # Main Streamlit application
β”œβ”€β”€ Fruit_Freshness_Classifier.ipynb  # Training notebook
β”œβ”€β”€ requirements.txt              # Python dependencies
β”œβ”€β”€ fruit_freshness_model.h5      # Trained model (download separately)
β”œβ”€β”€ README.md                     # Project documentation
β”‚
β”œβ”€β”€ dataset/                      # Dataset directory
β”‚   β”œβ”€β”€ train/                    # Training images
β”‚   β”‚   β”œβ”€β”€ freshapples/
β”‚   β”‚   β”œβ”€β”€ freshbanana/
β”‚   β”‚   β”œβ”€β”€ freshoranges/
β”‚   β”‚   β”œβ”€β”€ rottenapples/
β”‚   β”‚   β”œβ”€β”€ rottenbanana/
β”‚   β”‚   └── rottenoranges/
β”‚   └── test/                     # Test images
β”‚       └── [same structure as train]
β”‚
└── screenshots/                  # App screenshots
    β”œβ”€β”€ app_interface.png
    └── prediction_results.png

πŸ€– Model Architecture

The classifier uses MobileNetV2 as the base model with transfer learning:

  • Base Model: MobileNetV2 (pre-trained on ImageNet)
  • Input Size: 224Γ—224Γ—3 RGB images
  • Classes: 6 categories (3 fruits Γ— 2 conditions)
  • Training Strategy: Two-phase training with fine-tuning
  • Optimization: Class weight balancing for imbalanced datasets

Training Performance

  • Test Accuracy: 85%+ (varies by dataset)
  • Training Time: ~30 minutes on GPU
  • Model Size: ~14MB (optimized for deployment)

πŸ“Š Dataset

The model was trained on a curated dataset of fruit images:

  • Total Images: 6,000+ images
  • Categories: Fresh and rotten apples, bananas, oranges
  • Preprocessing: Data augmentation, normalization, resizing
  • Split: 80% training, 20% validation

Note: The dataset is not included in the repository due to size. Train your own model using the provided notebook.

πŸ”§ Usage

Web Interface

  1. Open the Streamlit app
  2. Upload an image or use camera
  3. Click "Analyze Fruit"
  4. View results with confidence scores
  5. See detailed probability breakdown

Model Training

Run the Jupyter notebook Fruit_Freshness_Classifier.ipynb to:

  • Analyze dataset distribution
  • Train the model with your data
  • Evaluate performance metrics
  • Generate visualizations

API Usage (Optional)

import tensorflow as tf
from PIL import Image
import numpy as np

# Load model
model = tf.keras.models.load_model('fruit_freshness_model.h5')

# Preprocess image
image = Image.open('fruit_image.jpg')
img_array = np.array(image.resize((224, 224))) / 255.0
img_batch = np.expand_dims(img_array, axis=0)

# Predict
predictions = model.predict(img_batch)
class_names = ['freshapples', 'freshbanana', 'freshoranges', 
               'rottenapples', 'rottenbanana', 'rottenoranges']
predicted_class = class_names[np.argmax(predictions)]

🎨 Key Features Explained

Class Imbalance Handling

The training script automatically detects and handles class imbalance using:

  • Computed class weights
  • Balanced sampling strategies
  • Performance metrics per class

Interactive Visualizations

  • Confidence Gauge: Radial gauge showing prediction confidence
  • Probability Chart: Horizontal bar chart of all class probabilities
  • Color Coding: Green for fresh, red for rotten fruits

Error Analysis

The training pipeline includes:

  • Confusion matrix generation
  • Misclassification analysis
  • Per-class performance metrics

πŸš€ Deployment

Streamlit Cloud (Recommended)

  1. Fork this repository
  2. Connect to Streamlit Cloud
  3. Deploy directly from GitHub
  4. Share your app URL!

Local Deployment

streamlit run streamlit_app.py --server.port 8501

Docker Deployment

FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 8501
CMD ["streamlit", "run", "streamlit_app.py"]

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Commit changes (git commit -m 'Add AmazingFeature')
  4. Push to branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

πŸ“ˆ Future Enhancements

  • Support for more fruit types (strawberries, grapes, etc.)
  • Mobile app version using Flutter/React Native
  • Batch processing for multiple images
  • REST API endpoints
  • Model quantization for edge deployment
  • Real-time webcam integration
  • Freshness timeline prediction

πŸ› Known Issues

  • Model performs best with clear, well-lit images
  • Very dark or blurry images may reduce accuracy
  • Background objects can sometimes affect predictions

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ‘¨β€πŸ’» Author

Your Name

πŸ™ Acknowledgments

  • TensorFlow team for the excellent deep learning framework
  • Streamlit for the amazing web app framework
  • MobileNetV2 architecture by Google
  • Open source community for inspiration and resources

πŸ“š References


⭐ Star this repo if you found it helpful! ⭐

Releases

No releases published

Packages

No packages published