Skip to content
This repository was archived by the owner on Sep 11, 2025. It is now read-only.

🍎 Fruit & vegetable image classifier using TensorFlow CNN. 18 categories, Streamlit web app, Jupyter training notebooks, modular Python code. Learn computer vision & deep learning. Automated preprocessing, metrics visualization, easy deployment. Educational ML project with clean architecture.

License

Notifications You must be signed in to change notification settings

NhanPhamThanh-IT/Fruit-Veggie-Classifier

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🍎 Fruit and Vegetable Image Classification

License: MIT Python TensorFlow Streamlit

A comprehensive machine learning project for classifying images of fruits and vegetables using Convolutional Neural Networks (CNNs). This project includes both a training pipeline and a web application for real-time image classification.

🌟 Features

  • Deep Learning Model: Custom CNN architecture built with TensorFlow/Keras
  • Web Application: Interactive Streamlit app for real-time predictions
  • Comprehensive Dataset: 18 different fruits and vegetables with training, validation, and test sets
  • Image Preprocessing: Automated image preprocessing pipeline
  • Model Evaluation: Training history visualization and performance metrics
  • Easy Deployment: Ready-to-deploy web application

πŸ“Š Dataset

The project uses a diverse dataset containing images of the following categories:

  • 🍎 Apple
  • 🍌 Banana
  • πŸ₯• Beetroot
  • πŸ«‘ Bell Pepper
  • πŸ₯¬ Cabbage
  • 🌢️ Capsicum
  • πŸ₯• Carrot
  • πŸ₯¦ Cauliflower
  • 🌢️ Chilli Pepper
  • 🌽 Corn
  • πŸ₯’ Cucumber
  • πŸ† Eggplant
  • πŸ§„ Garlic
  • 🫚 Ginger
  • πŸ‡ Grapes
  • 🌢️ JalapeΓ±o
  • πŸ₯ Kiwi
  • πŸ‹ Lemon

Dataset Structure

dataset/
β”œβ”€β”€ train/          # Training images (80% of data)
β”œβ”€β”€ validation/     # Validation images (10% of data)
└── test/          # Test images (10% of data)

Each category contains approximately 10 images for training and validation, ensuring balanced representation.

πŸ—οΈ Architecture

Model Architecture

The CNN model consists of:

  • Input Layer: 180x180 RGB images
  • Convolutional Layers: 3 Conv2D layers with increasing filters (16, 32, 64)
  • Pooling Layers: MaxPooling2D for downsampling
  • Regularization: Dropout layer (0.2) to prevent overfitting
  • Dense Layers: 128 neurons with ReLU activation
  • Output Layer: Softmax activation for multi-class classification

Training Configuration

  • Optimizer: Adam
  • Loss Function: Sparse Categorical Crossentropy
  • Metrics: Accuracy
  • Batch Size: 32
  • Epochs: 25
  • Image Size: 180x180 pixels

πŸš€ Installation

Prerequisites

  • Python 3.8 or higher
  • pip package manager

Setup Instructions

  1. Clone the repository

    git clone https://github.com/NhanPhamThanh-IT/ThaoMyProject.git
    cd ThaoMyProject
  2. Create a virtual environment (recommended)

    python -m venv venv
    # On Windows
    venv\Scripts\activate
    # On macOS/Linux
    source venv/bin/activate
  3. Install dependencies

    pip install -r requirements.txt
  4. Download the dataset Place your fruit and vegetable images in the dataset/ directory following the structure shown above.

πŸ“– Usage

Training the Model

  1. Navigate to the notebooks directory

    cd notebooks
  2. Run the training notebook

    python notebook.py

    This will:

    • Load and preprocess the dataset
    • Train the CNN model
    • Save the trained model to models/model.keras
    • Display training history plots

Running the Web Application

  1. Navigate to the app directory

    cd app
  2. Launch the Streamlit app

    streamlit run main.py
  3. Access the application Open your browser and go to http://localhost:8501

Using the Classification API

The web app allows you to:

  • Enter an image filename from the public/images/ directory
  • View the image preview
  • See the predicted fruit/vegetable category
  • Check the prediction confidence score

πŸ› οΈ Project Structure

ThaoMyProject/
β”œβ”€β”€ app/                    # Streamlit web application
β”‚   β”œβ”€β”€ main.py            # Main application file
β”‚   β”œβ”€β”€ config/            # Configuration management
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   └── InitConfig.py
β”‚   └── utils/             # Utility functions
β”‚       β”œβ”€β”€ __init__.py
β”‚       └── ImageProcessing.py
β”œβ”€β”€ data/                  # Configuration files
β”‚   └── config.json        # App configuration
β”œβ”€β”€ dataset/               # Image dataset
β”‚   β”œβ”€β”€ train/
β”‚   β”œβ”€β”€ validation/
β”‚   └── test/
β”œβ”€β”€ models/                # Trained models
β”‚   └── model.keras
β”œβ”€β”€ notebooks/             # Jupyter notebooks
β”‚   β”œβ”€β”€ notebook.py        # Training notebook
β”‚   └── notebook.pdf       # Notebook documentation
β”œβ”€β”€ public/                # Public assets
β”‚   └── images/           # Sample images
β”œβ”€β”€ requirements.txt       # Python dependencies
β”œβ”€β”€ LICENSE               # MIT License
└── README.md             # Project documentation

πŸ”§ Configuration

The application uses a JSON configuration file (data/config.json) that includes:

  • App Settings: Title, description, and UI parameters
  • Model Settings: Image dimensions and preprocessing parameters
  • Categories: List of fruit and vegetable classes
  • Paths: File paths for models and data

Example configuration:

{
  "APP": {
    "TITLE": "Fruit & Vegetable Classifier"
  },
  "IMG_SHAPE": {
    "width": 180,
    "height": 180
  },
  "CATEGORIES": [
    "apple",
    "banana",
    "beetroot",
    "bell pepper",
    "cabbage",
    "capsicum",
    "carrot",
    "cauliflower",
    "chilli pepper",
    "corn",
    "cucumber",
    "eggplant",
    "garlic",
    "ginger",
    "grapes",
    "jalepeno",
    "kiwi",
    "lemon"
  ]
}

πŸ“ˆ Model Performance

The trained model achieves:

  • Training Accuracy: ~95% (varies based on dataset quality)
  • Validation Accuracy: ~90% (varies based on dataset quality)
  • Test Accuracy: ~88% (varies based on dataset quality)

Performance may vary depending on:

  • Quality and diversity of training images
  • Number of training epochs
  • Model architecture complexity
  • Image preprocessing techniques

🀝 Contributing

We welcome contributions from the community! Here's how you can help:

Ways to Contribute

  • πŸ› Report Bugs: Use GitHub Issues to report bugs
  • πŸ’‘ Suggest Features: Propose new features or improvements
  • πŸ“ Improve Documentation: Help make our docs better
  • πŸ”§ Code Contributions: Submit pull requests with fixes or enhancements
  • πŸ“Š Dataset Expansion: Add more images or categories

Development Setup

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/your-feature-name
  3. Make your changes and test thoroughly
  4. Commit your changes: git commit -m 'Add some feature'
  5. Push to the branch: git push origin feature/your-feature-name
  6. Open a Pull Request

Guidelines

  • Follow PEP 8 style guidelines for Python code
  • Add docstrings to new functions and classes
  • Write tests for new features
  • Update documentation for any changes
  • Ensure all tests pass before submitting

πŸ“‹ Requirements

The project requires the following Python packages:

  • numpy - Numerical computing
  • matplotlib - Data visualization
  • tensorflow - Deep learning framework
  • streamlit - Web application framework

πŸ” Troubleshooting

Common Issues

  1. Model Loading Errors

    • Ensure models/model.keras exists
    • Check TensorFlow version compatibility
  2. Image Loading Errors

    • Verify image paths are correct
    • Ensure images are in supported formats (JPG, PNG)
    • Check image dimensions match model expectations
  3. Streamlit App Issues

    • Ensure all dependencies are installed
    • Check that the port 8501 is available
    • Verify configuration file exists
  4. Training Issues

    • Ensure dataset directory structure is correct
    • Check that all categories have sufficient images
    • Verify GPU availability for faster training

πŸ“„ License

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

πŸ™ Acknowledgments

  • TensorFlow/Keras: For providing the deep learning framework
  • Streamlit: For the web application framework
  • Open Source Community: For inspiration and tools
  • Dataset Contributors: For providing the fruit and vegetable images

πŸ“ž Contact

🎯 Future Enhancements

  • Add more fruit and vegetable categories
  • Implement data augmentation techniques
  • Add model quantization for mobile deployment
  • Create REST API for model serving
  • Add batch processing capabilities
  • Implement model explainability features
  • Add support for custom image uploads
  • Create mobile application version

⭐ Star this repository if you find it helpful!

🍎 Happy classifying! 🍌

About

🍎 Fruit & vegetable image classifier using TensorFlow CNN. 18 categories, Streamlit web app, Jupyter training notebooks, modular Python code. Learn computer vision & deep learning. Automated preprocessing, metrics visualization, easy deployment. Educational ML project with clean architecture.

Topics

Resources

License

Stars

Watchers

Forks

Languages