A TensorFlow-based web application to classify apple images as fresh or rotten with percentage confidence scores. This project combines deep learning with interactive visualization tools to provide insights into the model's decision-making process.
- Real-time apple freshness classification
- Interactive web interface
- Model visualization and explainability tools
- Detailed training metrics and performance analysis
- Support for both image upload and drag-and-drop
- Comprehensive model architecture visualization
├── app.py # Flask web application
├── train_model.py # Model training script
├── visualize_model.py # Advanced model visualization tool
├── simple_visualize.py # Basic visualization utilities
├── explained_visuals.py # Model explainability tools
├── requirements.txt # Project dependencies
├── templates/ # HTML templates
│ └── index.html # Web interface
├── uploads/ # Folder for uploaded images
├── model_visualizations/ # Generated model visualizations
├── explained_visuals/ # Model explanation outputs
├── apple_model_visualizations/ # Additional visualization artifacts
├── train/ # Training dataset
│ ├── freshapples/ # Fresh apple images for training
│ └── rottenapples/ # Rotten apple images for training
└── test/ # Testing dataset
├── freshapples/ # Fresh apple images for testing
└── rottenapples/ # Rotten apple images for testing
- Python 3.8 or higher
- TensorFlow 2.x
- Flask
- OpenCV
- NumPy
- Matplotlib
- Pillow
- Gradio (for visualization interface)
For a complete list of dependencies, see requirements.txt.
- Clone this repository
- Create and activate a virtual environment (recommended):
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
- Install dependencies:
pip install -r requirements.txt
To train the classification model:
python train_model.pyThis will:
- Train the model on the images in the
train/directory - Evaluate the model on the images in the
test/directory - Save the trained model as
apple_classifier_model.h5 - Save the training history plot as
training_history.png - Generate performance metrics and confusion matrices
To start the Flask server:
python app.pyThen open a web browser and navigate to:
http://localhost:5000
- Upload an image of an apple using the "Select Image" button or by dragging and dropping
- Click "Analyze Image"
- View the results showing:
- Percentage confidence of the apple being fresh or rotten
- Visualization of model attention areas
- Explanation of the classification decision
The project includes several visualization tools:
-
Basic Visualization (
simple_visualize.py):python simple_visualize.py
- Generates basic model architecture diagrams
- Shows layer activation maps
- Displays feature maps
-
Advanced Visualization (
visualize_model.py):python visualize_model.py
- Creates detailed model architecture visualizations
- Shows filter visualizations
- Generates activation heatmaps
- Provides performance analysis graphs
-
Model Explainability (
explained_visuals.py):python explained_visuals.py
- Generates Grad-CAM visualizations
- Creates LIME explanations
- Shows feature importance maps
The model uses a CNN-based architecture optimized for apple freshness classification:
- Input Layer: 224x224x3 (RGB images)
- 4 Convolutional blocks:
- Conv2D layers with increasing filters (32, 64, 128, 256)
- BatchNormalization for training stability
- MaxPooling2D for spatial dimension reduction
- Dropout layers to prevent overfitting
- Global Average Pooling
- Dense layers with dropout
- Binary output with sigmoid activation
- Data Augmentation:
- Random rotation
- Horizontal/vertical flips
- Zoom variation
- Brightness adjustment
- Early Stopping to prevent overfitting
- Learning rate scheduling
- Batch size optimization
- Validation split: 20%
The model achieves:
- Training accuracy: ~95%
- Validation accuracy: ~93%
- Test set accuracy: ~92%
Detailed performance metrics and visualizations are generated during training and saved in the model_visualizations/ directory.
The application provides a REST API endpoint that can be integrated with OutSystems applications.
- URL:
https://api.fruitflow.site/api/predict/base64plain - Method:
POST - Content-Type:
application/json
- URL:
http://your-server:5000/analyze - Method:
POST - Content-Type:
application/json
{
"image": "base64_encoded_image_string"
}{
"classification": "fresh",
"filename": "voorbeeld.jpg",
"fresh_percentage": 87.23,
"rotten_percentage": 12.77,
"success": true,
"heatmap_data": "base64_encoded_string_hier",
"heatmap_overlay_data": "base64_encoded_string_hier",
"image_url": "https://yourdomain.com/uploads/filename.jpg"
}classification: String - Either "fresh" or "rotten"filename: String - Name of the processed image filefresh_percentage: Number - Confidence percentage for fresh classification (0-100)rotten_percentage: Number - Confidence percentage for rotten classification (0-100)success: Boolean - Indicates if the analysis was successfulheatmap_data: String - Base64 encoded heatmap visualizationheatmap_overlay_data: String - Base64 encoded heatmap overlay on original imageimage_url: String - URL to the processed image
-
In OutSystems Service Studio, create a REST API integration:
- Set the Method to
POST - Set the URL to:
https://api.fruitflow.site/api/predict/base64plain - Set Content-Type to
application/json - Configure the input structure as Text data type
- Set the request to be sent in the body
- Set the Method to
-
Create the input structure:
{
"image": "Text"
}- Create the output structure matching the response format above:
{
"classification": "Text",
"filename": "Text",
"fresh_percentage": "Decimal",
"rotten_percentage": "Decimal",
"success": "Boolean",
"heatmap_data": "Text",
"heatmap_overlay_data": "Text",
"image_url": "Text"
}- Use the REST API in your OutSystems application:
- Convert your image to base64 before sending
- Send the request using the REST API
- Handle the response in your application logic
// Example structure for the API call in OutSystems
var request = {
image: $base64EncodedImage
};
// Make the API call to the production endpoint
var response = SendRequest("POST", "https://api.fruitflow.site/api/predict/base64plain", request);
// Handle the response
if (response.success) {
// Use the classification results
ShowClassification(response.classification);
DisplayConfidence(response.fresh_percentage, response.rotten_percentage);
ShowHeatmap(response.heatmap_data);
}Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
- ✓ Commercial use
- ✓ Modification
- ✓ Distribution
- ✓ Private use
- ✓ Sublicense
The only requirement is to include the original copyright notice and license notice in any copy of the software/substantial portions of the software.
For the complete license text, please see the LICENSE file in the repository.