Skip to content

eimtechnology/KT27-AI_Vision_Further_Applications

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 

Repository files navigation

AI Vision Further Applications

A collection of two deep learning computer vision projects for practical applications: Coffee Bean Classification and Meat Quality Detection. Both projects utilize transfer learning with ResNet architectures and feature user-friendly GUI applications for real-time predictions.

📋 Projects Overview

☕ Coffee Bean Classification

An intelligent system that automatically classifies coffee bean roast levels into 4 categories:

  • Dark Roast: Deep brown color, oily surface
  • Green Beans: Raw, unroasted coffee beans
  • Light Roast: Light brown color, dry surface
  • Medium Roast: Medium brown color, even roasting

🥩 Meat Quality Detection

An AI-powered system that classifies meat quality as Fresh or Spoiled using deep learning, helping ensure food safety and quality control.


🚀 Quick Start

Prerequisites

  • Python: 3.8 or higher (3.9+ recommended)
  • Operating System: Windows 10/11, Linux, or macOS
  • RAM: Minimum 8GB (16GB recommended)
  • GPU: Optional but recommended for faster training (NVIDIA GPU with CUDA support)

Installation

  1. Clone the repository

    git clone <repository-url>
    cd KT27-AI_Vision_Further_Applications-main
  2. Install dependencies

    For Coffee Bean project:

    cd "Coffee Bean"
    pip install -r requirements.txt

    For Meat Quality project:

    cd "Meat Quality"
    pip install -r requirements.txt

    Or install for both projects from root:

    pip install -r "Coffee Bean/requirements.txt"
    pip install -r "Meat Quality/requirements.txt"
  3. Optional: GPU Support (if you have NVIDIA GPU)

    pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118

☕ Coffee Bean Classification

Project Structure

Coffee Bean/
├── train_coffee_bean.py      # Training script with advanced features
├── gui_coffee_bean.py         # GUI application for predictions
├── requirements.txt           # Python dependencies
├── README.md                  # Detailed project documentation
└── data_analysis.png          # Dataset analysis visualization

Features

Training Features:

  • ✅ Transfer learning with pretrained ResNet50
  • ✅ Advanced data preprocessing and analysis
  • ✅ Multiple data augmentation strengths (light/medium/strong)
  • ✅ Early stopping mechanism
  • ✅ Cross-validation support (5-fold)
  • ✅ Test-time augmentation (TTA)
  • ✅ Gradient clipping
  • ✅ Adaptive learning rate scheduling
  • ✅ Automatic checkpoint saving
  • ✅ Rich visualizations (training curves, confusion matrix, etc.)

GUI Features:

  • 📁 Upload Image: Select local image for prediction
  • 🎲 Random Test: Randomly select image from training or test set
  • 📷 Camera Prediction: Real-time camera prediction
  • 🎯 Start Prediction: Classify current image
  • 📊 Detailed Results: Display predicted class, confidence, timestamp, and full probability distribution

Usage

1. Train the Model

cd "Coffee Bean"
python train_coffee_bean.py

After training, the following files will be generated:

  • coffee_bean_classifier.pth - Trained model file
  • best_model_checkpoint.pth - Best model checkpoint
  • data_analysis.png - Dataset analysis chart
  • training_history.png - Training history chart
  • confusion_matrix.png - Confusion matrix chart

2. Run the GUI Application

python gui_coffee_bean.py

Expected Performance

  • Training Accuracy: 95%+
  • Validation Accuracy: 90%+
  • Test Accuracy: 85%+

Model Architecture

  • Backbone: ResNet50 (pretrained)
  • Classifier: Two fully connected layers + Dropout
  • Input Size: 224×224×3
  • Output Classes: 4 classes

🥩 Meat Quality Detection

Project Structure

Meat Quality/
├── train_meat_quality.py              # Standard training script
├── train_meat_quality_optimized.py    # CPU-optimized training
├── train_resnet_transfer_learning.py  # Advanced training with enhanced features
├── train_meat_quality_cv.py           # Cross-validation training
├── quick_train.py                     # Quick start training script
├── meat_quality_gui.py                # GUI application
├── requirements.txt                   # Python dependencies
├── setup_environment.bat              # Windows setup script
├── setup_environment.sh               # Linux/Mac setup script
├── README.md                          # Detailed project documentation
└── README_TRAINING.md                  # Detailed training guide

Features

Training Scripts:

  • train_meat_quality.py: Standard training with ResNet variants (18, 34, 50, 101)
  • train_meat_quality_optimized.py: CPU-optimized training with lightweight ResNet-18
  • train_resnet_transfer_learning.py: Advanced training with enhanced features
  • train_meat_quality_cv.py: Cross-validation training for robust evaluation
  • quick_train.py: Quick start training script for beginners

GUI Features:

  • 📁 Single Image Prediction: Upload and analyze individual images
  • 📂 Batch Processing: Process multiple images simultaneously
  • 📹 Real-time Camera Detection: Live analysis using webcam
  • 💾 Results Export: Save predictions in CSV/JSON format
  • 📊 Statistical Analysis: Visual charts and prediction history
  • 🎨 Modern UI: Professional dark theme interface

Dataset Structure

Your dataset should be organized as follows:

meat_quality/
├── Fresh/
│   ├── image1.jpg
│   ├── image2.jpg
│   └── ... (948 images)
└── Spoiled/
    ├── image1.jpg
    ├── image2.jpg
    └── ... (948 images)

Usage

1. Quick Training (Recommended for beginners)

cd "Meat Quality"
python quick_train.py

2. Standard Training

python train_meat_quality.py --epochs 50 --batch_size 32 --learning_rate 0.001

3. Advanced Training

python train_resnet_transfer_learning.py --model_name resnet50 --epochs 50

4. Run the GUI Application

python meat_quality_gui.py

Expected Performance

  • Training Accuracy: 90-95%
  • Validation Accuracy: 85-92%
  • Training Time: 30-60 minutes (depending on hardware)

Model Architecture

Input Image (224x224x3)
↓
ResNet Backbone (pre-trained)
↓
Global Average Pooling
↓
Fully Connected (2048 → 512)
↓
ReLU + Dropout (0.3)
↓
Fully Connected (512 → 2)
↓
Output: [Fresh, Spoiled]

🛠️ Technical Details

Common Technologies

Both projects share similar technical foundations:

  • Deep Learning Framework: PyTorch 2.0+
  • Computer Vision: torchvision, OpenCV
  • Image Processing: PIL/Pillow
  • Data Science: NumPy, Pandas, scikit-learn
  • Visualization: Matplotlib, Seaborn
  • GUI Framework: CustomTkinter (Coffee Bean), Tkinter (Meat Quality)

Training Strategy

  1. Transfer Learning: Leverage pretrained ResNet models on ImageNet
  2. Data Augmentation: Random crops, flips, rotations, color jitter
  3. Two-Phase Training:
    • Phase 1: Transfer learning with frozen early layers
    • Phase 2: Fine-tuning with all layers unfrozen
  4. Optimization: Adam optimizer with learning rate scheduling
  5. Validation: 20% of data used for validation

Performance Metrics

  • Accuracy: Overall classification accuracy
  • Precision: True positives / (True positives + False positives)
  • Recall: True positives / (True positives + False negatives)
  • F1-Score: Harmonic mean of precision and recall

📖 Documentation

Each project has its own detailed README:


🔧 Troubleshooting

Common Issues

1. Model file not found

Solution: Run the training script first to train the model

2. CUDA out of memory

Solution: Reduce batch size (e.g., --batch_size 16 or --batch_size 8)

3. Camera not working

Solution: 
1. Check camera permissions
2. Ensure no other applications are using the camera
3. Try different camera index (modify cv2.VideoCapture(0) to cv2.VideoCapture(1))

4. Dependency conflicts

Solution: Use virtual environment and reinstall dependencies

Performance Tips

  1. GPU Usage: Ensure CUDA is properly installed for GPU acceleration
  2. Data Quality: Use high-quality, well-lit images for best results
  3. Batch Size: Increase batch size if you have sufficient GPU memory
  4. Training Time: Use ResNet-18 for faster training, ResNet-50/101 for better accuracy

📊 Project Comparison

Feature Coffee Bean Meat Quality
Classes 4 (Dark, Green, Light, Medium) 2 (Fresh, Spoiled)
Model ResNet50 ResNet18/34/50/101
Input Size 224×224 224×224 (112×112 for optimized)
GUI Framework CustomTkinter Tkinter
Batch Processing
Results Export
Training Scripts 1 5

🤝 Contributing

Contributions are welcome! To improve the projects:

  1. Collect more diverse training data
  2. Experiment with different architectures
  3. Implement additional data augmentation techniques
  4. Add more detailed quality classifications
  5. Develop mobile/web applications
  6. Deploy as web services

📝 License

This project is for educational and research purposes. Make sure to comply with any dataset licensing requirements.


🆘 Support

If you encounter issues:

  1. Check the console output for error messages
  2. Verify all dependencies are installed correctly
  3. Ensure your dataset follows the required structure
  4. Try running with default parameters first
  5. Check the individual project README files for detailed troubleshooting

📈 Future Enhancements

Possible improvements for both projects:

  • 🔄 Add more classification categories
  • 🎨 Add image quality detection
  • 📱 Develop mobile applications
  • 🌐 Deploy as web services
  • 📊 Add batch processing (for Coffee Bean)
  • 🔍 Implement explainable AI features
  • 📈 Add model performance monitoring

📞 Contact & Credits

Version: 2.0
Last Updated: December 2024
Author: AI Assistant


Happy Coding! 🎉

About

This is Source and Example Code for KT27 AI Vision Chapter 8

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages