This repository features a complete, end-to-end Computer Vision pipeline designed to automate quality control on Printed Circuit Boards (PCBs). By leveraging a Deep Semantic Segmentation framework, the system detects, isolates, and highlights manufacturing defects (such as short circuits, missing traces, or solder bridges) through an interactive web-based interface.
The production-ready application is powered by Gradio, providing an intuitive interface for quality control inspectors. It allows users to upload high-resolution PCB images, adjust defect confidence thresholds in real-time, and instantly view localized anomalies.
When a flawless PCB is uploaded, the system processes the image through the network and ensures that no pixel probability exceeds the set threshold. It outputs a clean status with a low global anomaly score, minimizing false positives.
When a defective board is inspected, the model generates a dense pixel-wise probability map. The post-processing pipeline detects active contours and draws automated bounding boxes directly around the faulty regions, providing immediate visual feedback.
- Deep Learning Segmentation: Leverages a U-Net architecture with a ResNet34 encoder (via
segmentation_models_pytorch) for high-precision defect localization. - Interactive UI: Built with Gradio, allowing inspectors to upload PCB images, adjust defect confidence thresholds in real-time, and view binary masks.
- Visual Fault Isolation: Highlights detected anomalies automatically with clean, red bounding boxes directly on the original board image.
- Recent History Tracking: Saves and displays the last 5 inspected images within the session, enabling quick click-to-reload historical comparisons.
The core of this system is built upon Semantic Segmentation, treating defect detection not just as a global classification problem, but as a pixel-level identification task.
- Architecture: U-Net (Encoder-Decoder framework optimized for capturing both high-level semantic context and precise low-level spatial localization).
- Backbone / Encoder: ResNet34 (Pre-trained weights are leveraged to extract rich feature hierarchies from the PCB surfaces efficiently).
- Activation: Pixel-level probability mapping is achieved via a Sigmoid layer outputting continuous values between
0.0(Normal) and1.0(Defect). - Post-Processing Pipeline: Binary masks generated from the threshold slider are converted into spatial coordinates using OpenCV's topological contour algorithm (
cv2.findContourswithRETR_EXTERNAL), isolating each defect into structural bounding boxes.
The training workflow was implemented inside Google Colab using an NVIDIA T4 GPU. The data preparation pipeline emphasizes robustness against varying factory lighting conditions and camera angles.
To train the segmentation network, each raw PCB image is mapped against a binary ground-truth mask. The image below (dataset_sample.png) displays a sample pair from the dataset where defects are precisely labeled in white against a black background, establishing the absolute ground truth for supervision.
The network was trained using a combined loss function tailored for unbalanced segmentation data (where defects occupy a tiny fraction of the board). As analyzed in the metric curves below (train_loss.png), the training and validation loss converged stably over the epochs, ensuring high dice-coefficients and strict generalization without overfitting.
After training, the network generates dense pixel-wise probability maps that are converted into binary masks and localized defect regions. The image below (model_result.png) demonstrates the model's ability to accurately identify and isolate PCB anomalies while preserving the surrounding normal structures.
main.py: The production deployment script hosting the inference engine, preprocessing blocks, and the Gradio UI layout.PCBAnomalyDetector_Model.ipynb: The exhaustive Google Colab notebook documenting dataset preprocessing, model training, and weight serialization.requirements.txt: List of precise Python package dependencies required to mirror the runtime environment.
Deploy and run the interactive PCB quality inspection workspace locally by following these steps:
First, clone this repository to your local drive and navigate into the project root directory:
git clone https://github.com/YOUR_USERNAME/PCB-Anomaly-Detection.git
cd PCB-Anomaly-DetectionEnsure you have Python 3.8 or higher ready. Install all essential Deep Learning and Vision packages via pip:
pip install -r requirements.txtGPU Note: To accelerate inference using an NVIDIA graphics card, verify your PyTorch setup aligns with your local CUDA driver version.
Deep learning model weight files (.pth) are too heavy to be tracked directly inside GitHub repositories.
- Run and execute the training blocks in the provided
PCBAnomalyDetector_Model.ipynbnotebook on Google Colab. - At the final cell, download the trained weights file named
best_pcb_model.pth. - Move/Paste the downloaded
best_pcb_model.pthfile directly into the root folder of this project (in the same directory alongsidemain.py).
Execute the production script to spin up the local server hosting the user interface:
python main.pyUpon execution, a local URL link (typically http://127.0.0.1:7860) will appear in your terminal. Click or open this link in any browser to launch your smart inspection workflow!




