This Flask API provides image classification for waste types using a trained deep learning model. The API can classify waste images into two categories: Organic and Recyclable.
- Image Classification: Upload waste images and get predictions with confidence scores
- Health Monitoring: Check API status and model availability
- Class Information: Retrieve supported waste categories
- Cross-Origin Support: CORS enabled for web applications
- Error Handling: Comprehensive error responses and logging
Classify a waste image from Base64 encoded data.
Request Body:
{
"image": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQ..."
}Response:
{
"success": true,
"result": {
"class": "Recyclable",
"confidence": 0.95,
"all_predictions": {
"Organic": 0.05,
"Recyclable": 0.95
}
}
}Check API health status and model availability.
Response:
{
"status": "healthy",
"model_loaded": true
}Get list of supported waste classes.
Response:
{
"classes": ["Organic", "Recyclable"]
}-
Clone the repository:
git clone <repository-url> cd waste_classification_api
-
Install dependencies:
pip install flask flask-cors tensorflow numpy opencv-python pillow
-
Ensure model file exists: Make sure
waste_model.kerasis in the project root directory. -
Run the application:
python app.py
The API will be available at http://localhost:5000
- Flask: Web framework for the API
- Flask-CORS: Cross-Origin Resource Sharing support
- TensorFlow: Deep learning framework for model inference
- NumPy: Numerical computing for array operations
- OpenCV: Computer vision library for image processing
- Pillow: Python Imaging Library for image handling
- Input Size: 224x224 pixels
- Categories: Organic, Recyclable
- Format: Keras model (.keras file)
- Preprocessing: RGB conversion, resizing, normalization (0-1 range)
curl -X POST http://localhost:5000/predict \
-H "Content-Type: application/json" \
-d '{"image": "data:image/jpeg;base64,<your-base64-image>"}'import requests
import base64
# Read and encode image
with open('waste_image.jpg', 'rb') as f:
image_data = base64.b64encode(f.read()).decode()
# Make prediction
response = requests.post('http://localhost:5000/predict',
json={'image': f'data:image/jpeg;base64,{image_data}'})
result = response.json()
print(f"Prediction: {result['result']['class']} ({result['result']['confidence']:.2f})")The API runs on:
- Host: 0.0.0.0 (all interfaces)
- Port: 5000
- Debug: Disabled for production
The API returns appropriate HTTP status codes:
200: Success400: Bad Request (missing image data)404: Endpoint not found500: Internal server error (model issues)
- JPEG
- PNG
- Other PIL-supported formats
- Base64 encoded images
- Model not loading: Ensure
waste_model.kerasexists in the project directory - Import errors: Install all required dependencies
- Image processing errors: Check image format and Base64 encoding
- CORS issues: Flask-CORS is enabled by default