Skip to content

shubham333k/DefectX-AI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

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

Repository files navigation

πŸ” DefectX AI - Explainable AI Quality Control System

AI-Powered Manufacturing Defect Detection with Visual Explainability (Grad-CAM)

Python YOLO Streamlit PyTorch License


πŸ“‹ Project Overview

DefectX AI is a production-ready, explainable AI system for automated manufacturing quality control. It combines state-of-the-art YOLO object detection with Gradient-weighted Class Activation Mapping (Grad-CAM) to not only identify defects in manufacturing products but also provide visual explanations for its decisions, building trust in AI-powered inspection systems.

🎯 Key Differentiator: Explainable AI (XAI)

Unlike traditional defect detection systems that operate as "black boxes," DefectX AI provides Grad-CAM heatmaps that highlight the exact regions of an image that influenced the model's decision. This transparency is critical for:

  • Quality Assurance Teams: Understanding why a product was flagged
  • Regulatory Compliance: Demonstrating AI decision-making processes
  • Continuous Improvement: Identifying patterns in false positives


πŸ“Έ Dashboard Preview

Screenshot 2026-07-03 002750 ) ---

πŸš€ Key Features

Feature Description Impact
πŸ” Real-Time Detection YOLOv8-based defect detection with 25ms inference on CPU Real-time production line integration
🧠 Grad-CAM XAI Visual heatmaps explaining model attention regions Trust & transparency in AI decisions
πŸ“Š Interactive Dashboard Streamlit-based analytics with defect trends Data-driven quality decisions
πŸ“Ή Video Processing Frame-by-frame analysis with batch export Complete production line monitoring
πŸ“₯ Export Reports CSV logs + annotated images + summary reports Audit trails & documentation
⚑ CPU Optimized Efficient inference without GPU requirements Cost-effective deployment

🏭 Supported Defect Classes

  • βœ… Good Product - Passes quality standards
  • πŸ”΄ Crack - Structural damage (Critical)
  • 🟠 Scratch - Surface damage (High/Medium)
  • 🟑 Dent - Deformation defects
  • πŸ”΅ Misalignment - Positioning errors
  • 🟣 Contamination - Foreign particles
  • πŸ”΅ Discoloration - Color/texture anomalies
  • πŸ”΄ Missing Component - Assembly errors (Critical)

πŸ› οΈ Tech Stack

Component Technology Purpose
Detection Engine Ultralytics YOLOv8/v11 Object detection backbone
Explainability Grad-CAM + PyTorch Hooks XAI visual explanations
Web Framework Streamlit 1.32+ Interactive dashboard
Computer Vision OpenCV + Pillow Image/video processing
Analytics Plotly + Pandas + Seaborn Data visualization
ML Framework PyTorch 2.2+ Deep learning operations
Export CSV + JSON + PNG Report generation

πŸ“¦ Project Structure

DefectX AI/
β”œβ”€β”€ app.py                 # Main Streamlit application
β”œβ”€β”€ utils.py               # Core detection + XAI utilities
β”œβ”€β”€ requirements.txt       # Python dependencies
β”œβ”€β”€ README.md             # Documentation
β”œβ”€β”€ models/               # YOLO model storage
β”‚   └── yolov8n.pt       # Default nano model (auto-downloaded)
β”œβ”€β”€ logs/                 # Detection history (JSON)
β”œβ”€β”€ outputs/              # Annotated images & reports
β”‚   β”œβ”€β”€ annotated/       # Detection + Grad-CAM exports
β”‚   └── batch_exports/   # Batch processing results
└── assets/               # Static assets & sample images

⚑ Quick Start

Prerequisites

  • Python 3.8 or higher
  • 4GB+ RAM (8GB recommended for video processing)
  • Windows/Linux/macOS

Installation

# Clone the repository
git clone https://github.com/shubham333k/DefectX-AI.git
cd DefectX-AI

# Create virtual environment
python -m venv venv

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

# Install dependencies
pip install -r requirements.txt

Running the Application

# Launch Streamlit dashboard
streamlit run app.py

# The application will open in your browser at:
# http://localhost:8501

πŸ“Έ Usage Guide

1. Single Image Inspection

  1. Launch the application
  2. Click "Initialize Model" in sidebar (downloads YOLO on first run)
  3. Select "Single Image" upload type
  4. Upload a product image (PNG/JPG)
  5. View results: Original β†’ Detection β†’ Grad-CAM Explanation
  6. Download annotated images and comparison view

2. Batch Processing

  1. Select "Batch Images" upload type
  2. Upload multiple images
  3. View summary statistics and individual results
  4. Export CSV report with all detections

3. Video Analysis

  1. Select "Video" upload type
  2. Upload production line footage (MP4/AVI)
  3. System processes frames at configurable intervals
  4. Download annotated video and frame-by-frame reports

4. Model Configuration

  • YOLO Model: Choose from Nano (fastest) to XLarge (most accurate)
  • Confidence Threshold: 0.1 - 0.9 (default: 0.25)
  • Grad-CAM Intensity: Adjust heatmap transparency
  • Device: CPU or CUDA (if GPU available)

πŸ“Š Performance Benchmarks

Model Input Size CPU Inference mAP Use Case
YOLOv8n 640x640 ~25ms 37.3 Real-time demo
YOLOv8s 640x640 ~45ms 44.9 Balanced
YOLOv8m 640x640 ~90ms 50.2 Higher accuracy
YOLOv8l 640x640 ~150ms 52.9 Quality priority

Benchmarks measured on Intel i7 / AMD Ryzen 7 processors


πŸŽ“ Explainable AI (XAI) Implementation

Grad-CAM Architecture

Input Image
    ↓
YOLO Backbone (CSPDarknet)
    ↓
Feature Extraction Layers
    ↓
[Hook: Capture Activations] ←───────┐
    ↓                               β”‚
Detection Head                      β”‚
    ↓                               β”‚
Predictions (Class + BBox)          β”‚
    ↓                               β”‚
Backpropagate Class Score           β”‚
    ↓                               β”‚
[Hook: Capture Gradients] β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    ↓
Gradient Γ— Activation = Grad-CAM
    ↓
Heatmap Overlay on Image

Why Grad-CAM for Manufacturing?

  1. Localization Accuracy: Pinpoints exact defect regions, not just general areas
  2. Model Validation: Confirms model focuses on actual defects, not background noise
  3. False Positive Analysis: Identifies why good products might be flagged
  4. Training Insights: Reveals if model learned relevant features

πŸ“ˆ Sample Results

Detection Output

Image: pcb_board_042.jpg
━━━━━━━━━━━━━━━━━━━━━━━━━━━━
βœ… Quality Check: FAILED
πŸ”΄ Defects Found: 2
⏱️ Processing Time: 0.032s

Detection #1:
  Type: Crack
  Confidence: 94.2%
  Severity: Critical
  Location: (124, 256) - (180, 320)

Detection #2:
  Type: Scratch
  Confidence: 78.5%
  Severity: High
  Location: (400, 120) - (450, 200)

Analytics Dashboard

  • Defect rate trends over time
  • Severity distribution (Critical/High/Medium/Low)
  • Defect type frequency analysis
  • Confidence score distributions
  • Production quality metrics

πŸ”§ Customization

Training Custom Defect Models

from ultralytics import YOLO

# Load pre-trained model
model = YOLO('yolov8n.pt')

# Train on custom defect dataset
model.train(
    data='defects_dataset.yaml',
    epochs=100,
    imgsz=640,
    batch=16,
    name='custom_defect_model'
)

Adding New Defect Classes

Edit utils.py:

DEFAULT_DEFECT_CLASSES = {
    0: 'Good Product',
    1: 'Scratch',
    2: 'Crack',
    # ... add your custom classes
    8: 'Your Custom Defect'
}

πŸ† Why This Project Matters

Demonstrates Expertise In:

  1. Computer Vision: Object detection, image processing, video analytics
  2. Deep Learning: CNN architectures, feature extraction, backpropagation
  3. Explainable AI: Grad-CAM implementation, model interpretability, trustworthiness
  4. MLOps: Model deployment, versioning, performance optimization
  5. Full-Stack ML: Backend (PyTorch) + Frontend (Streamlit) integration
  6. Domain Application: Manufacturing QA, industrial automation, quality engineering

Industry Applications:

  • πŸ”§ Automotive: Detecting dents, scratches on car parts
  • πŸ“± Electronics: PCB inspection, component verification
  • 🍾 Food & Beverage: Bottle inspection, contamination detection
  • πŸ—οΈ Construction: Material defect identification
  • πŸ‘• Textile: Fabric quality control, defect classification

πŸ“ API Reference

DefectDetector Class

detector = DefectDetector(
    model_path='path/to/model.pt',  # or None for default
    model_name='yolov8n.pt',        # YOLO variant
    conf_threshold=0.25,            # Detection threshold
    device='cpu'                    # 'cpu' or 'cuda'
)

# Detect defects
result = detector.detect(image)
# Returns: {detections, annotated_image, original_image, total_defects}

# Generate Grad-CAM
gradcam = detector.generate_gradcam(image, alpha=0.5)

# Process batch
results = detector.process_batch(['img1.jpg', 'img2.jpg'])

# Get analytics
analytics = detector.get_analytics()

🀝 Contributing

  1. Fork the repository
  2. Create feature branch (git checkout -b feature/AmazingFeature)
  3. Commit changes (git commit -m 'Add AmazingFeature')
  4. Push to branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

πŸ“„ License

Distributed under the MIT License. See LICENSE for more information.


πŸ“§ Contact

Project Maintainer: AI/ML Engineer
Email: shubhamjhanjhot333k@gmail.com
LinkedIn:[https://www.linkedin.com/in/shubhamkumar-aiml/]
GitHub: @shubham333k


πŸ™ Acknowledgments


πŸ” DefectX AI - Making Manufacturing Quality Control Transparent with Explainable AI

Built with ❀️ for AI/ML Engineers passionate about industrial applications

About

Automated industrial quality control system for real-time defect detection using Computer Vision.

Topics

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages