Skip to content

PCB-Defect-Detection-and-Classification/PCB-Defect-Detection-and-Classification

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

60 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

PCB Defect Detection & Classification System

Internship Project: AI-Based PCB Defect Detection
Phase: Milestone 1 - Image Processing & Dataset Preparation

πŸ“– Project Overview

The objective of this project is to develop an automated system capable of identifying and classifying defects on Printed Circuit Boards (PCB). In the electronics manufacturing industry, manual inspection is slow and prone to error. This solution leverages Computer Vision (for localization) and Deep Learning (for classification) to automate quality assurance.

This repository contains the deliverables for Milestone 1, which focuses on building the foundational image processing pipeline and preparing the data for the neural network.

🎯 Milestone 1 Objectives

The goal of this phase was to implement Modules 1 & 2 from the project roadmap:

  • Module 1 (Defect Localization):
    Implement a reference-based subtraction algorithm to detect anomalies between a "Golden Template" and a "Test Image."

  • Module 2 (ROI Extraction):
    Prepare a clean, labeled dataset by extracting Ground Truth Regions of Interest (ROIs) from the DeepPCB dataset to train the CNN in Milestone 2.

βš™οΈ Methodology & Technical Approach

1. The Detection Pipeline (Module 1)

This module implements the core logic that identifies defects using image subtraction. The process relies on comparing a perfect reference image to the board being inspected.

Algorithm Steps:

  1. Image Alignment: Use computer vision techniques (ORB/Homography) to align the test image precisely with the template.
  2. Image Subtraction: Calculate pixel-wise absolute difference: Diff(x,y) = |Template(x,y) - Test(x,y)|
  3. Grayscale Conversion: Simplifies subsequent processing steps.
  4. Otsu's Thresholding: Automatically finds the optimal threshold to separate the difference regions (potential defects) from image noise.
  5. Morphological Filtering: Apply Opening (to remove small white noise) and Dilation/Closing (to solidify defect regions).
  6. Contour Extraction: Draw bounding boxes around the connected components identified as detected defects.

Outcome: Demonstrates that computer vision alone can localize defects like "Missing Holes" or "Spurs" without human intervention.

2. The Training Data Generator (Module 2)

This module generates labeled ROIs for training a Convolutional Neural Network (CNN).

Process:

  1. Parse XML annotation files from the DeepPCB dataset.
  2. Extract verified bounding box coordinates (xmin, ymin, xmax, ymax).
  3. Crop the corresponding regions of interest (ROIs) from the raw images.
  4. Automatically sort crops into class-specific folders (e.g., Mouse_bite, Open_circuit, Short).

Outcome: A dataset of 2,953 high-quality, pre-labeled images ready for CNN training.

πŸ’» Setup & Usage

Prerequisites

  • Python 3.10 or higher
  • DeepPCB Dataset (must include images, PCB_USED, and Annotations)

πŸ“Š Results

Output 1: Visual Verification (Module 1)

The system successfully identifies defects in testing. It generates Difference Maps and Binary Masks highlighting defects.

Output 2: Labeled Dataset (Module 2)

Processed 693 image pairs from DeepPCB. Generated a balanced dataset:

Defect Class Samples Extracted
Missing Hole ~497
Mouse Bite ~492
Open Circuit ~482
Short ~491
Spur ~488
Spurious Copper ~503
Total ~2,953

πŸ” Example: Missing Hole Defect

Image

πŸ“– Project Overview (Continued)

With the image processing pipeline established in Milestone 1, the project now moves into the intelligence phase. While computer vision localizes potential anomalies, Milestone 2 implements a Deep Learning classification engine to distinguish between specific defect types (e.g., Short, Open Circuit, Mouse Bite) and reduce false positives.


🎯 Milestone 2 Objectives

The goal of this phase was to implement Modules 3 & 4 from the project roadmap:

  • Module 3 (Transfer Learning): Adapt a pre-trained Convolutional Neural Network (CNN) to classify PCB defect ROIs.
  • Module 4 (Testing & Evaluation): Benchmark the model's accuracy and performance to ensure it meets industrial standards (β‰₯ 95% accuracy).

βš™οΈ Methodology & Technical Approach

1. Data Augmentation & Refinement

To ensure the model is robust against variations in lighting and positioning, the 2,953 ROIs extracted in Milestone 1 underwent a transformation pipeline:

  • Geometric Transformations: Random rotations, horizontal/vertical flips, and slight shifts to simulate real-world camera misalignments.
  • Normalization: Scaling pixel values and resizing images to a standard input size (e.g., 224x224) compatible with the model architecture.

2. Model Architecture (Module 3)

The system utilizes Transfer Learning to leverage features learned from large-scale datasets.

  • Base Model: A pre-trained architecture (such as ResNet or VGG) was used as the feature extractor.
  • Custom Head: The final fully connected layers were replaced with a specialized classifier matching the 6 defect categories identified in the DeepPCB dataset.
  • Optimization: Employed the Adam optimizer and Categorical Cross-Entropy loss function to minimize classification errors during training.

3. Training & Validation Pipeline

  • Split: The dataset was divided into Training (80%), Validation (10%), and Test (10%) sets to monitor for overfitting.
  • Environment: Training was conducted using high-performance libraries like PyTorch or TensorFlow within a GPU-accelerated environment.

πŸ“Š Results

Output 1: Model Performance

The system achieved high reliability in classifying defects. The validation phase focused on ensuring the model could distinguish between visually similar defects like β€œSpurs” and β€œSpurious Copper.”

  • Target Accuracy: β‰₯ 95% test accuracy.

    image alt

  • Inference Speed: Processed images in ≀ 3 seconds per image, meeting the target for real-time application needs.

Output 2: Evaluation Metrics

Detailed performance was tracked using:

  • Confusion Matrix: To identify specific classes where the model might confuse one defect for another.

image alt

  • Loss/Accuracy Curves: Demonstrating steady convergence during the training epochs.

image alt

πŸ” Example: Classification Outcome

  • Input: ROI localized as a "Missing Hole" via Milestone 1 subtraction.
  • Deep Learning Prediction: Classifies as "Missing_hole" with 98.4% confidence.

πŸ“˜ Milestone 3: Frontend & Backend Integration

Phase: Milestone 3
Focus: Application Development & System Integration

🎯 Objective:
Build a production-ready Web UI for real-time [Insert Project Name, e.g., Medical Imaging / Infrastructure] analysis.

πŸ› οΈ Tech Stack:
Streamlit, OpenCV, TensorFlow/PyTorch, PIL, [Insert other libraries]


πŸ“– Milestone Overview

This final milestone serves as the "Bridge" β€” integrating the high-performance models developed in Milestones 1 & 2 into a seamless, interactive experience.
The system transforms raw data into actionable insights, allowing non-technical stakeholders to perform complex AI diagnostics with a single click.


πŸ’» Module 5: Web UI (Frontend)

The frontend is built on Streamlit, designed for speed and clarity. It focuses on reducing cognitive load by presenting complex AI outputs through intuitive visualizations.

✨ Key Features

  • Dynamic Parameter Tuning: A sidebar allows users to adjust sensitivity (thresholds) on the fly, instantly updating UI results.

    image alt

  • Smart Media Uploader: Supports drag-and-drop for [Images / Video / Data files] with real-time validation.

    image alt

  • Interactive Overlays: Uses custom CSS/JavaScript components to overlay AI predictions directly onto the source media.

    image alt

  • One-Click Export: A dedicated reporting engine compiles results into a professional PDF or CSV summary.

    image alt


βš™οΈ Module 6: Backend & Inference Engine

The backend (orchestrated in app.py or engine.py) manages the lifecycle of a request β€” from pre-processing to model inference and post-analysis.

πŸš€ The "Dual-Stream" Processing Strategy

To ensure both speed and accuracy, the backend utilizes a tiered approach:

  1. Preprocessing Stream: Normalizes input data (resizing, denoising, color correction) so that the AI sees data in its optimal form.
  2. Inference Stream: Routes cleaned data through the trained models.
  3. Post-Processing Logic: Applies Non-Maximum Suppression (NMS) or custom logic to filter noise and format the coordinates for the frontend.

πŸ–ΌοΈ Application Showcase

1. The Dashboard (Initial State)

A minimalist entry point featuring a secure upload zone and a System Readiness check to ensure AI models are preloaded in cache.

2. Real-Time Processing

Displays a Processing state as users interact, showcasing the AI's ability to handle high-resolution inputs with low latency.

3. Visual Analysis Map

The core view where AI predictions (bounding boxes / heatmaps) are rendered. Each detection is color-coded by category for instant recognition.

4. Comparative Analysis

Includes a Side-by-Side view or Overlay Toggle allowing users to compare the original input with the AI’s interpreted output.

5. Analytical Results & Logs

A structured data table breaks down the confidence of every prediction, providing transparency for professional-grade tools.


βœ… Final Project Status

Milestone Objective Status Result
M1 [e.g., Data Extraction] βœ… Complete [e.g., 99% Precision]
M2 [e.g., Model Training] βœ… Complete [e.g., 98% mAP]
M3 System Integration βœ… Complete Fully Functional App

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published