This is a FastAPI-based API for garbage classification using the CLIP (Contrastive Language-Image Pretraining) model. It performs zero-shot image classification, meaning it can classify images into categories it wasn't specifically trained on.
- FastAPI: Modern, fast web framework for building APIs with Python 3.7+
- CLIP Model: OpenAI's Contrastive Language-Image Pre-training model from Hugging Face
- PyTorch: Deep learning framework
- Transformers: Hugging Face library for state-of-the-art NLP and vision models
- Pillow: Python Imaging Library for image processing
- Uvicorn: ASGI server for running the FastAPI application
The application follows a simple architecture:
- FastAPI handles HTTP requests and responses
- Images are processed using Pillow
- CLIP model performs zero-shot classification
- Results are returned as JSON with confidence scores
- Python 3.7+
- pip package manager
# Create a virtual environment
python -m venv v_garbage_classifier
# Activate the virtual environment
source v_garbage_classifier/bin/activate # On Linux/Mac
# or
v_garbage_classifier\Scripts\activate # On Windows
# Install dependencies
pip install -r requirements.txt
# Using uvicorn directly
uvicorn app.main:app --host 0.0.0.0 --port 8123
# Or using Python
python -m uvicorn app.main:app --host 0.0.0.0 --port 8123
# Build the Docker image
docker build -t garbage-classifier .
# Run the container
docker run -p 8123:8123 garbage-classifier
- API Documentation: http://localhost:8123/api/docs
- Base URL: http://localhost:8123
- Returns a welcome message
- Used to verify the API is running
- Accepts an image file and optional comma-separated categories
- Returns classification results with confidence scores
file
(required): Image file of garbage itemcategories
(optional): Comma-separated list of categories to classify against
- plastic bottle
- glass bottle
- paper
- cardboard
- metal can
- organic waste
- electronic waste
- textile
curl -X POST "http://localhost:8123/classify" \
-H "accept: application/json" \
-H "Content-Type: multipart/form-data" \
-F "[email protected]"
{
"top_class": "electronic waste",
"confidence": 0.33171185851097107,
"predictions": {
"plastic bottle": 0.24136486649513245,
"glass bottle": 0.0022902777418494225,
"paper": 0.0015287415590137243,
"cardboard": 0.0012100355233997107,
"metal can": 0.24120794236660004,
"organic waste": 0.18023031949996948,
"electronic waste": 0.33171185851097107,
"textile": 0.00045594872790388763
}
}
app/main.py
: Main FastAPI application and endpointsapp/clip_model.py
: CLIP model loading and classification logicapp/utils.py
: Utility functions for image processingapp/test.py
: Simple test endpointrequirements.txt
: Python dependenciesdockerfile
: Docker configuration for containerization
To modify the default categories, update the DEFAULT_CLASSES
array in app/main.py
.
The application uses the openai/clip-vit-base-patch32
model from Hugging Face, which is loaded once at startup for efficiency.
The API includes basic error handling that logs exceptions and returns error messages in JSON format.
Simple testing can be done using curl commands or the interactive API documentation at /api/docs
.