Skip to content

Nduka99/image_processing_deployment

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🐱 Cat vs Dog Classifier

An end-to-end ML image classification web app deployed on AWS β€” 100% Free Tier ($0/month)

Upload any image and our AI will tell you whether it's a cat 🐱 or a dog 🐢 β€” complete with confidence probabilities!

Python FastAPI AWS Terraform Docker


🌐 Live Demo

Service URL
Frontend cat-vs-dog-frontend-235899055608.s3-website.eu-west-2.amazonaws.com
API Health af9aiibt3m.execute-api.eu-west-2.amazonaws.com/health

πŸ“ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Browser   │──────▢│  S3 Static   β”‚       β”‚ API Gateway │──────▢│  Lambda   β”‚
β”‚  (Frontend) β”‚       β”‚   Website    │──────▢│  (HTTP API) β”‚       β”‚ (Docker)  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜       β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜
                                                                         β”‚
                      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”            β”‚
                      β”‚  CloudWatch  │◀──────│     ECR      β”‚β—€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                      β”‚   Alarms     β”‚       β”‚ (Container)  β”‚
                      β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                             β”‚
                      β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”
                      β”‚  SNS Email   β”‚
                      β”‚   Alerts     β”‚
                      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Frontend β†’ Static HTML/CSS/JS hosted on S3 (drag-and-drop image upload with light/dark mode)
Backend β†’ Lambda (container image from ECR) behind API Gateway, running ONNX inference
Monitoring β†’ CloudWatch alarms β†’ SNS email alerts + AWS Budget guard
CI/CD β†’ GitHub Actions builds Docker, pushes to ECR, deploys frontend to S3


🧠 How It Works

ML Pipeline

  1. Image Preprocessing β€” Smart resize (128Γ—128), CLAHE contrast enhancement, bilateral filtering
  2. Feature Extraction β€” 10,221 handcrafted features from 8 families:
    • HOG (Histogram of Oriented Gradients) β€” fine + coarse
    • LBP (Local Binary Patterns) β€” multi-scale
    • Color Histograms (HSV + LAB)
    • Color Moments (mean, std, skew)
    • Haralick Texture Features (GLCM)
    • Hu Moments (shape descriptors)
    • Edge Features (Canny, Sobel, orientation histograms)
    • Gabor Filter Responses (multi-frequency, multi-orientation)
  3. StandardScaler β€” Normalizes features to match training distribution
  4. XGBoost (ONNX) β€” Gradient-boosted tree ensemble for binary classification
  5. Probability Output β€” Returns per-class probabilities (cat % vs dog %)
  6. OOD Detection β€” If neither class exceeds 51% confidence, the image is flagged as "unknown"

Model Performance

Metric Score
Accuracy >80%
F1 Score >80%
Model Size 2.03 MB (ONNX)
Inference Time ~3-5s (Lambda cold start: ~30s first request)

πŸ“ Project Structure

image_processing/
β”œβ”€β”€ api/                          # Backend API
β”‚   β”œβ”€β”€ main.py                   # FastAPI app with /health and /predict endpoints
β”‚   β”œβ”€β”€ lambda_handler.py         # AWS Lambda entry point
β”‚   β”œβ”€β”€ ml.py                     # ML pipeline (ONNX model + StandardScaler)
β”‚   β”œβ”€β”€ feature_extractor.py      # Image preprocessing & 10,221-feature extraction
β”‚   β”œβ”€β”€ schemas.py                # Pydantic response models
β”‚   └── dependencies.py           # Singleton model loader
β”œβ”€β”€ frontend/                     # Static frontend (hosted on S3)
β”‚   β”œβ”€β”€ index.html                # Main UI with drag-and-drop upload
β”‚   β”œβ”€β”€ index.css                 # Styles with light/dark mode
β”‚   └── app.js                    # Upload logic, probability bars, theme toggle
β”œβ”€β”€ model/                        # Trained model artifacts
β”‚   β”œβ”€β”€ xgboost_champion.onnx     # XGBoost model in ONNX format (2.03 MB)
β”‚   └── scaler.joblib             # Fitted StandardScaler (240 KB)
β”œβ”€β”€ infra/                        # Terraform Infrastructure as Code
β”‚   β”œβ”€β”€ main.tf                   # ECR, S3, Lambda, API Gateway, IAM
β”‚   β”œβ”€β”€ outputs.tf                # Deployment URLs
β”‚   └── monitoring.tf             # CloudWatch alarms, SNS, Budget guard
β”œβ”€β”€ .github/workflows/
β”‚   └── deploy.yml                # CI/CD: build β†’ ECR β†’ Lambda β†’ S3
β”œβ”€β”€ Dockerfile                    # Lambda Python 3.11 container
β”œβ”€β”€ requirements.txt              # Pinned Python dependencies
β”œβ”€β”€ .dockerignore                 # Excludes data/notebooks from Docker build
└── .gitignore                    # Excludes secrets, data, terraform state

πŸš€ Getting Started

Prerequisites

  • Python 3.11+
  • Docker Desktop
  • AWS CLI (configured with aws configure)
  • Terraform 1.x+

Local Development

# 1. Clone the repository
git clone https://github.com/Nduka99/image_processing_deployment.git
cd image_processing_deployment

# 2. Install dependencies
pip install -r requirements.txt

# 3. Start the API server
uvicorn api.main:app --host 127.0.0.1 --port 8000

# 4. Open the frontend
# Navigate to http://127.0.0.1:3000 (via a local HTTP server)
# Or open frontend/index.html directly

Deploy to AWS

# 1. Initialize and apply Terraform
cd infra
terraform init
terraform apply -auto-approve

# 2. Build and push Docker image
aws ecr get-login-password --region eu-west-2 | docker login --username AWS --password-stdin <ACCOUNT_ID>.dkr.ecr.eu-west-2.amazonaws.com
docker build -t <ECR_URL>:latest ..
docker push <ECR_URL>:latest

# 3. Upload frontend to S3 (with API URL injected)
# The GitHub Actions workflow handles this automatically on push to main

☁️ AWS Resources (20 total β€” all Free Tier)

# Service Resource Free Tier Limit
1-2 ECR Container registry + lifecycle policy 500 MB/month
3-6 S3 Bucket + website config + public access + policy 5 GB, 20K GET/month
7-8 IAM Lambda execution role + policy Always free
9 Lambda Container function (1024 MB, 60s) 1M requests, 400K GB-s/month
10-13 API Gateway HTTP API + integration + routes + stage 1M calls/month
14-15 SNS Alert topic + email subscription 1M publishes/month
16-19 CloudWatch 4 alarms (errors, duration, throttles, 5XX) 10 alarms free
20 Budgets Cost guard ($1/month threshold) Always free

πŸ”„ CI/CD Pipeline

GitHub Actions (.github/workflows/deploy.yml) triggers on every push to main:

  1. βœ… Checkout code
  2. βœ… Authenticate with AWS
  3. βœ… Build Docker image β†’ Push to ECR
  4. βœ… Update Lambda function code
  5. βœ… Inject API Gateway URL into frontend β†’ Sync to S3

Setup: Add AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY as GitHub Secrets in your repository settings.


πŸ›‘οΈ Monitoring & Alerts

Alarm Triggers When Action
Lambda Errors Any invocation error Email via SNS
Lambda Duration Average > 50s (timeout = 60s) Email via SNS
Lambda Throttles Any throttled invocations Email via SNS
API 5XX Errors 5+ server errors in 5 minutes Email via SNS
Budget Guard Monthly cost exceeds $1 Email notification

πŸ› οΈ Technology Stack

Layer Technology
ML Model XGBoost (ONNX Runtime)
Feature Extraction OpenCV, scikit-image, mahotas, scipy
Backend FastAPI + AWS Lambda
Frontend Vanilla HTML/CSS/JS
Infrastructure Terraform (IaC)
Containerization Docker (Lambda Python 3.11 base)
CI/CD GitHub Actions
Monitoring CloudWatch + SNS
Cloud AWS (S3, ECR, Lambda, API Gateway)

πŸ“„ License

This project was developed as part of an Applied AI assessment.


Built with ❀️ using an agentic AI deployment pipeline

About

Deploying a machine learning model to classify cat/dog images

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors