Skip to content

devxsubh/TrueMeds

Repository files navigation

Counterfeit Medicine Detection System

A comprehensive end-to-end machine learning system for detecting counterfeit medicines from images using deep learning. This project includes a complete training pipeline, REST API services, and a modern web frontend for real-time medicine authentication.

πŸ“‹ Table of Contents

🎯 Overview

This system uses Transfer Learning with a pre-trained ResNet-18 model to classify medicine images as either Authentic or Counterfeit. The solution is built with a microservices architecture, separating concerns into:

  • ML Service: FastAPI-based inference service for model predictions
  • Backend API: Express.js server handling authentication, user management, and ML service proxying
  • Frontend: React application with Vite for fast development and production builds

Use Cases

  • Pharmaceutical quality control
  • Supply chain verification
  • Consumer protection tools
  • Regulatory compliance checking

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Client    │─────▢│   Server     │─────▢│ ML Service  β”‚
β”‚  (React)    │◀─────│  (Express)   │◀─────│  (FastAPI)  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
     Port 3000           Port 5000            Port 8000

Data Flow:

  1. User uploads medicine image via React frontend
  2. Frontend sends request to Express backend (with authentication)
  3. Backend validates request and proxies to ML service
  4. ML service processes image through ResNet-18 model
  5. Prediction results flow back through the stack
  6. User sees classification result (Authentic/Counterfeit) with confidence score

✨ Features

Core Functionality

  • Image Classification: Binary classification (Authentic vs Counterfeit) using ResNet-18
  • Real-time Inference: Fast API responses with confidence scores and probability distributions
  • User Authentication: JWT-based authentication system with role-based access control
  • Image Upload: Secure file upload with validation and processing

Technical Features

  • Transfer Learning: Pre-trained ResNet-18 model fine-tuned on medicine dataset
  • RESTful APIs: Well-structured API endpoints following REST principles
  • Microservices: Decoupled services for scalability and maintainability
  • Docker Support: Containerized deployment for easy setup
  • Error Handling: Comprehensive error handling and validation
  • Logging: Structured logging for debugging and monitoring

πŸ“ Project Structure

AiMl/
β”œβ”€β”€ client/                          # React frontend application
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ components/             # Reusable React components
β”‚   β”‚   β”‚   └── UploadComponent.jsx # Image upload component
β”‚   β”‚   β”œβ”€β”€ pages/                  # Page components
β”‚   β”‚   β”œβ”€β”€ services/               # API client services
β”‚   β”‚   β”‚   └── api.js              # Axios API client
β”‚   β”‚   β”œβ”€β”€ App.jsx                 # Main app component
β”‚   β”‚   β”œβ”€β”€ main.jsx                # Entry point
β”‚   β”‚   └── index.css               # Global styles
β”‚   β”œβ”€β”€ public/                     # Static assets
β”‚   β”œβ”€β”€ index.html                  # HTML template
β”‚   β”œβ”€β”€ vite.config.js              # Vite configuration
β”‚   β”œβ”€β”€ package.json
β”‚   └── Dockerfile                   # Docker configuration
β”‚
β”œβ”€β”€ server/                          # Express.js backend API
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ config/                 # Configuration files
β”‚   β”‚   β”‚   β”œβ”€β”€ config.js           # Environment config
β”‚   β”‚   β”‚   β”œβ”€β”€ mongoose.js        # Database connection
β”‚   β”‚   β”‚   β”œβ”€β”€ passport.js        # JWT authentication
β”‚   β”‚   β”‚   └── logger.js          # Winston logger
β”‚   β”‚   β”œβ”€β”€ controllers/           # Request handlers
β”‚   β”‚   β”‚   β”œβ”€β”€ authController.js  # Authentication logic
β”‚   β”‚   β”‚   β”œβ”€β”€ mlController.js    # ML service integration
β”‚   β”‚   β”‚   β”œβ”€β”€ userController.js  # User management
β”‚   β”‚   β”‚   └── imageController.js # Image handling
β”‚   β”‚   β”œβ”€β”€ middlewares/           # Express middlewares
β”‚   β”‚   β”‚   β”œβ”€β”€ authenticate.js    # JWT verification
β”‚   β”‚   β”‚   β”œβ”€β”€ uploadImage.js     # File upload handling
β”‚   β”‚   β”‚   └── validate.js       # Request validation
β”‚   β”‚   β”œβ”€β”€ models/                # Mongoose models
β”‚   β”‚   β”‚   β”œβ”€β”€ userModel.js       # User schema
β”‚   β”‚   β”‚   β”œβ”€β”€ roleModel.js       # Role schema
β”‚   β”‚   β”‚   └── tokenModel.js      # Token schema
β”‚   β”‚   β”œβ”€β”€ routes/                # API routes
β”‚   β”‚   β”‚   └── v1/
β”‚   β”‚   β”‚       β”œβ”€β”€ authRoute.js  # Auth endpoints
β”‚   β”‚   β”‚       β”œβ”€β”€ mlRoute.js    # ML endpoints
β”‚   β”‚   β”‚       β”œβ”€β”€ userRoute.js   # User endpoints
β”‚   β”‚   β”‚       └── index.js      # Route aggregator
β”‚   β”‚   β”œβ”€β”€ services/              # Business logic
β”‚   β”‚   β”‚   β”œβ”€β”€ jwtService.js     # JWT utilities
β”‚   β”‚   β”‚   └── tokenService.js   # Token management
β”‚   β”‚   β”œβ”€β”€ utils/                 # Utility functions
β”‚   β”‚   β”‚   └── apiError.js       # Error handling
β”‚   β”‚   β”œβ”€β”€ validations/           # Validation schemas
β”‚   β”‚   β”‚   └── authValidation.js # Request validation
β”‚   β”‚   β”œβ”€β”€ app.js                 # Express app setup
β”‚   β”‚   └── index.js               # Server entry point
β”‚   β”œβ”€β”€ package.json
β”‚   β”œβ”€β”€ ecosystem.config.js         # PM2 configuration
β”‚   └── Dockerfile
β”‚
β”œβ”€β”€ ml_service/                     # ML training & inference service
β”‚   β”œβ”€β”€ main.ipynb                 # Jupyter notebook (training)
β”‚   β”œβ”€β”€ app.py                     # FastAPI inference service
β”‚   β”œβ”€β”€ utils.py                   # Preprocessing utilities
β”‚   β”œβ”€β”€ model/                      # Trained model files
β”‚   β”‚   └── best_cls_resnet18.pt   # ResNet-18 checkpoint
β”‚   β”œβ”€β”€ requirements.txt            # Python dependencies
β”‚   └── Dockerfile
β”‚
β”œβ”€β”€ data/                           # Dataset (gitignored)
β”‚   β”œβ”€β”€ raw/                        # Original dataset
β”‚   β”‚   β”œβ”€β”€ train/                  # Training images (89M)
β”‚   β”‚   β”œβ”€β”€ valid/                  # Validation images (14M)
β”‚   β”‚   └── test/                   # Test images (6.8M)
β”‚   └── processed/                  # Processed data (empty)
β”‚
β”œβ”€β”€ docker-compose.yml              # Docker Compose configuration
β”œβ”€β”€ .gitignore                      # Git ignore rules
└── README.md                       # This file

πŸ“¦ Prerequisites

Required Software

  • Python 3.10+ - For ML service
  • Node.js 20+ - For backend and frontend
  • npm or yarn - Package manager
  • MongoDB - Database (or use MongoDB Atlas)
  • Git - Version control

πŸš€ Quick Start

Option 1: Docker (Recommended)

The fastest way to get started:

# Clone the repository
git clone <repository-url>
cd AiMl

# Start all services
docker compose up --build

# Services will be available at:
# - Frontend: http://localhost:3000
# - Backend API: http://localhost:5000
# - ML Service: http://localhost:8000

Option 2: Manual Setup

See Detailed Setup section below.

πŸ”§ Detailed Setup

Step 1: Clone Repository

git clone <repository-url>
cd AiMl

Step 2: ML Service Setup

cd ml_service

# Create virtual environment
python -m venv .venv

# Activate virtual environment
# On macOS/Linux:
source .venv/bin/activate
# On Windows:
.venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

# Ensure model file exists
# The model should be at: ml_service/model/best_cls_resnet18.pt
# If not, train the model first (see Training section)

# Start the service
python app.py

The ML service will run on http://localhost:8000

Verify it's working:

curl http://localhost:8000/health

Step 3: Backend Server Setup

cd server

# Install dependencies
npm install

# Create .env file (copy from .env.example if available)
# Or set environment variables:
export ML_SERVICE_URL=http://localhost:8000
export DATABASE_URI=mongodb://127.0.0.1:27017/aiml_db
export JWT_ACCESS_TOKEN_SECRET_PRIVATE=<your-secret>
export JWT_ACCESS_TOKEN_SECRET_PUBLIC=<your-public-key>

# Generate JWT keys (if needed)
ssh-keygen -t rsa -P "" -b 2048 -m PEM -f storage/jwtRS256.key
ssh-keygen -e -m PEM -f storage/jwtRS256.key > storage/jwtRS256.key.pub
cat storage/jwtRS256.key | base64  # Use this for JWT_ACCESS_TOKEN_SECRET_PRIVATE
cat storage/jwtRS256.key.pub | base64  # Use this for JWT_ACCESS_TOKEN_SECRET_PUBLIC

# Start MongoDB (if running locally)
# macOS: brew services start mongodb-community
# Linux: sudo systemctl start mongod
# Windows: net start MongoDB

# Start the server
npm start
# Or for development with hot reload:
npm run dev

The server will run on http://localhost:5000 (or PORT from .env)

Verify it's working:

curl http://localhost:5000/api/v1/ml/health

Step 4: Frontend Client Setup

cd client

# Install dependencies
npm install

# Create .env file (optional)
# VITE_API_URL=http://localhost:5000

# Start development server
npm run dev

The frontend will run on http://localhost:3000

Step 5: Verify All Services

  1. ML Service: http://localhost:8000/health
  2. Backend API: http://localhost:5000/api/v1/ml/health
  3. Frontend: http://localhost:3000

πŸŽ“ Training the Model

Prerequisites

  • Jupyter Notebook installed
  • Dataset in data/raw/ directory
  • GPU recommended (but not required)

Training Steps

  1. Open Jupyter Notebook:

    cd ml_service
    jupyter notebook main.ipynb
  2. Run All Cells:

    • The notebook will:
      • Load and explore the dataset from data/raw/
      • Create image-level labels from detection boxes
      • Set up data loaders with augmentation
      • Initialize ResNet-18 model
      • Train for 10 epochs (configurable)
      • Evaluate on test set
      • Save best model to ml_service/model/best_cls_resnet18.pt
  3. Model Checkpoint:

    • Best model is saved based on validation accuracy
    • Location: ml_service/model/best_cls_resnet18.pt
    • Contains: model state, classes, and metadata

Training Configuration

Edit the notebook to adjust:

  • EPOCHS: Number of training epochs (default: 10)
  • BATCH_SIZE: Batch size for training (default: 32)
  • LEARNING_RATE: Optimizer learning rate (default: 0.001)
  • DATA_DIR: Dataset directory path

Expected Results

  • Training accuracy: ~85-95%
  • Validation accuracy: ~80-90%
  • Test accuracy: ~75-85%

Note: Results may vary based on dataset and training configuration

πŸ“‘ API Documentation

ML Service (FastAPI) - Port 8000

Health Check

GET /health

Response:

{
  "ok": true,
  "classes": ["authentic", "counterfeit"],
  "checkpoint": "/path/to/model/best_cls_resnet18.pt"
}

Classify Image

POST /classify
Content-Type: multipart/form-data

Request:

  • Form data with file field containing image (JPG, PNG, etc.)

Response:

{
  "label": "authentic",
  "confidence": 0.95,
  "probabilities": {
    "authentic": 0.95,
    "counterfeit": 0.05
  }
}

Example (cURL):

curl -X POST http://localhost:8000/classify \
  -F "file=@path/to/image.jpg"

Backend API (Express) - Port 5000

All endpoints are prefixed with /api/v1

Authentication Endpoints

Sign Up:

POST /api/v1/auth/signup
Content-Type: application/json

Request Body:

{
  "email": "user@example.com",
  "password": "securepassword",
  "name": "John Doe"
}

Sign In:

POST /api/v1/auth/signin
Content-Type: application/json

Request Body:

{
  "email": "user@example.com",
  "password": "securepassword"
}

Response:

{
  "user": {
    "id": "user_id",
    "email": "user@example.com",
    "name": "John Doe"
  },
  "tokens": {
    "access": {
      "token": "jwt_token_here",
      "expires": "2024-01-01T00:00:00.000Z"
    },
    "refresh": {
      "token": "refresh_token_here",
      "expires": "2024-01-02T00:00:00.000Z"
    }
  }
}

ML Endpoints

Health Check:

GET /api/v1/ml/health

Response:

{
  "ok": true,
  "classes": ["authentic", "counterfeit"],
  "checkpoint": "/path/to/model"
}

Classify Image:

POST /api/v1/ml/classify
Authorization: Bearer <jwt_token>
Content-Type: multipart/form-data

Request:

  • Header: Authorization: Bearer <jwt_token>
  • Form data: file field with image

Response:

{
  "success": true,
  "data": {
    "label": "authentic",
    "confidence": 0.95,
    "probabilities": {
      "authentic": 0.95,
      "counterfeit": 0.05
    }
  }
}

Example (cURL):

curl -X POST http://localhost:5000/api/v1/ml/classify \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -F "file=@path/to/image.jpg"

User Management Endpoints

Get All Users:

GET /api/v1/users
Authorization: Bearer <jwt_token>

Get User by ID:

GET /api/v1/users/:userId
Authorization: Bearer <jwt_token>

Update User:

PUT /api/v1/users/:userId
Authorization: Bearer <jwt_token>
Content-Type: application/json

Delete User:

DELETE /api/v1/users/:userId
Authorization: Bearer <jwt_token>

βš™οΈ Configuration

Environment Variables

ML Service

No environment variables required (uses default paths)

Backend Server (.env file)

# Application
NODE_ENV=development
APP_NAME=Counterfeit Medicine Detection API
HOST=0.0.0.0
PORT=5000

# Database
DATABASE_URI=mongodb://127.0.0.1:27017/aiml_db

# JWT Authentication
JWT_ACCESS_TOKEN_SECRET_PRIVATE=<base64-encoded-private-key>
JWT_ACCESS_TOKEN_SECRET_PUBLIC=<base64-encoded-public-key>
JWT_ACCESS_TOKEN_EXPIRATION_MINUTES=240

# Token Expiration
REFRESH_TOKEN_EXPIRATION_DAYS=1
VERIFY_EMAIL_TOKEN_EXPIRATION_MINUTES=60
RESET_PASSWORD_TOKEN_EXPIRATION_MINUTES=30

# Email (Optional - for email verification)
SMTP_HOST=smtp.googlemail.com
SMTP_PORT=465
SMTP_USERNAME=your-email@gmail.com
SMTP_PASSWORD=your-app-password
EMAIL_FROM=noreply@example.com

# URLs
FRONTEND_URL=http://localhost:3000
IMAGE_URL=http://localhost:5000/images

# ML Service
ML_SERVICE_URL=http://localhost:8000

Frontend Client (.env file)

VITE_API_URL=http://localhost:5000

Docker Configuration

Edit docker-compose.yml to customize:

  • Port mappings
  • Volume mounts
  • Environment variables
  • Resource limits

πŸ’» Development

Development Workflow

  1. Start ML Service:

    cd ml_service
    source .venv/bin/activate
    python app.py
  2. Start Backend (with hot reload):

    cd server
    npm run dev
  3. Start Frontend (with hot reload):

    cd client
    npm run dev

Code Structure

  • Backend: Follows MVC pattern with clear separation of concerns
  • Frontend: Component-based React architecture
  • ML Service: Simple FastAPI service with model loading

Testing

# Backend tests (if available)
cd server
npm test

# Frontend tests (if available)
cd client
npm test

Code Quality

# Backend linting
cd server
npm run lint

# Backend formatting
npm run format

🚒 Deployment

Production Build

Backend

cd server
npm run build
npm run prod  # Uses PM2

Frontend

cd client
npm run build
# Output in client/dist/

Docker Deployment

# Build and start all services
docker compose up -d --build

# View logs
docker compose logs -f

# Stop services
docker compose down

Security Considerations

  • Use HTTPS in production
  • Store secrets in environment variables (never commit)
  • Enable CORS only for trusted domains
  • Use rate limiting (already configured)
  • Validate all user inputs
  • Keep dependencies updated

πŸ› Troubleshooting

Common Issues

ML Service Not Starting

Problem: Model file not found

Error: [Errno 2] No such file or directory: 'model/best_cls_resnet18.pt'

Solution:

  • Train the model first (see Training section)
  • Or download pre-trained model
  • Check model path in app.py

Backend Can't Connect to ML Service

Problem: Connection refused error

Solution:

  • Verify ML service is running: curl http://localhost:8000/health
  • Check ML_SERVICE_URL in backend .env
  • Ensure no firewall blocking port 8000

Database Connection Error

Problem: MongoDB connection failed

Solution:

  • Verify MongoDB is running: mongosh or mongo
  • Check DATABASE_URI in .env
  • Ensure MongoDB is accessible from server

Frontend API Errors

Problem: CORS errors or 404s

Solution:

  • Check VITE_API_URL in frontend .env
  • Verify backend is running
  • Check browser console for detailed errors

Port Already in Use

Problem: Port 3000, 5000, or 8000 already in use

Solution:

# Find process using port
lsof -i :8000  # macOS/Linux
netstat -ano | findstr :8000  # Windows

# Kill process or change port in configuration

Getting Help

  1. Check logs:

    • Backend: server/logs/ or console output
    • ML Service: Console output
    • Docker: docker compose logs
  2. Verify all services are running

  3. Check environment variables

  4. Review API documentation

  5. Check GitHub issues (if applicable)

πŸ“Š Dataset

Dataset Information

  • Source: Roboflow Universe (Counterfeit_med_detection)
  • License: CC BY 4.0
  • Format: TensorFlow Object Detection format (CSV)
  • Size: ~110MB total
    • Training: 89MB (1,367 images)
    • Validation: 14MB (123 images)
    • Test: 6.8MB (65 images)

Dataset Structure

data/raw/
β”œβ”€β”€ train/
β”‚   β”œβ”€β”€ _annotations.csv    # Annotation file
β”‚   └── *.jpg               # Training images
β”œβ”€β”€ valid/
β”‚   β”œβ”€β”€ _annotations.csv    # Annotation file
β”‚   └── *.jpg               # Validation images
└── test/
    β”œβ”€β”€ _annotations.csv    # Annotation file
    └── *.jpg               # Test images

CSV Format

Each row in _annotations.csv contains:

filename,width,height,class,xmin,ymin,xmax,ymax
  • filename: Image filename
  • width, height: Image dimensions
  • class: Label (authentic/counterfeit)
  • xmin, ymin, xmax, ymax: Bounding box coordinates

πŸ“„ License

  • Dataset: CC BY 4.0 (Roboflow Universe)
  • Code: See repository license file

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors