This guide walks you through every step required to get the PCB Defect Detection system running — from installing Python dependencies and training the classification model, to uploading images and exporting annotated results through the web interface.
No programming experience is required to operate the web application. Training the model does require downloading the DeepPCB dataset and running a batch script.
| Requirement | Minimum | Recommended |
|---|---|---|
| Operating System | Windows 10 | Windows 11 |
| Python | 3.12 | 3.12 |
| RAM | 8 GB | 16 GB |
| Disk space | 5 GB (dataset + venv) | 10 GB |
| CPU | Any modern dual-core | 8-core, 3 GHz+ |
| GPU | Not required | NVIDIA (for faster training) |
| Browser | Chrome / Edge | Chrome |
Python must be added to the system PATH during installation. When the Python installer runs, check the box labelled "Add Python to PATH" before clicking Install.
Option A: Clone from GitHub
git clone https://github.com/Cheekurthi-Vamsi/PCB-Defect-Detection-and-Classification-System.git
cd PCB-Defect-Detection-and-Classification-SystemOption B: Download ZIP
- Visit the GitHub repository page.
- Click Code → Download ZIP.
- Extract to a folder of your choice (e.g.
C:\Users\<you>\Desktop\PCB).
Open a command prompt inside the project folder and run:
py -3.12 -m venv venvThis creates an isolated venv\ directory containing a clean Python 3.12 environment.
venv\Scripts\pip install -r requirements.txtPackages installed:
| Package | Purpose |
|---|---|
tensorflow-cpu |
Neural network inference and training |
opencv-python |
Image processing (alignment, thresholding, contours) |
streamlit |
Web application framework |
Pillow |
PIL image loading and export |
numpy |
Array operations |
pandas |
DataFrame operations |
matplotlib / seaborn |
Training plots and confusion matrix |
scikit-learn |
Classification metrics |
imutils |
Contour utilities |
Installation typically takes 5–10 minutes depending on internet speed.
The system was trained and evaluated on the DeepPCB dataset, a publicly available collection of PCB image pairs annotated with six defect classes.
Download the DeepPCB dataset from:
https://github.com/Charmve/Surface-Defect-Detection/tree/master/DeepPCB
Extract the dataset into:
Image Subtraction\DeepPCB_dataset\
The expected folder structure is:
DeepPCB_dataset/
├── Missing_hole/
│ ├── 00041000_temp.jpg ← template (defect-free reference)
│ ├── 00041000_test.jpg ← test (potentially defective)
│ ├── 00041001_temp.jpg
│ └── ...
├── Mouse_bite/
├── Open_circuit/
├── Short/
├── Spur/
└── Spurious_copper/
Important: The six folder names must exactly match the class names listed above (case-sensitive).
The full training pipeline is automated in a single batch script that runs three consecutive processing stages.
Double-click run_pipeline.bat in the project root folder.
# Stage 1: Image alignment, subtraction & mask generation
venv\Scripts\python "Image Subtraction\image_subraction.py"
# Stage 2: Contour detection, ROI extraction & dataset splitting
venv\Scripts\python "Contour Detection and ROI Extraction\roi_ext.py"
# Stage 3: EfficientNetB0 training
venv\Scripts\python "Model Training and Evaluation\Train.py"Stage 1 — Image Processing & Mask Generation
Reads every template/test image pair, aligns the test image to the template using ORB feature matching and homography, computes the absolute pixel difference, applies Otsu's thresholding to produce a binary defect mask, and saves aligned images, difference maps, and highlighted overlays. Typically completes in 2–10 minutes.
Stage 2 — ROI Extraction & Dataset Split
Reads the binary defect masks produced in Stage 1, detects defect contours, crops 128×128 ROI patches around each defect centroid, and splits them into training, validation, and test sets (70 / 15 / 15%, seed=42). Outputs are saved as labeled image files in a hierarchical folder structure ready for Keras ImageDataGenerator. Typically completes in 1–5 minutes.
Stage 3 — EfficientNetB0 Training
Loads the ROI patches and trains the EfficientNetB0 classifier in two phases: a warm-up phase with the backbone frozen, followed by a fine-tuning phase where the top 30 backbone layers are unfrozen at a lower learning rate. Training curves (accuracy + loss), confusion matrix, and a full classification report are saved to module3_output\. Training typically takes 30 to 90 minutes on CPU, or 5–15 minutes with a compatible GPU.
All outputs are saved to Model Training and Evaluation\module3_output\:
| File | Description |
|---|---|
best_model.keras |
Best model checkpoint (highest validation accuracy) |
efficientnet_pcb_defect.keras |
Final epoch model |
training_log.csv |
Accuracy and loss for each training epoch |
training_curves.png |
Accuracy and loss over time plots |
confusion_matrix.png |
6×6 classification heatmap |
evaluation_report.txt |
Full per-class precision, recall, F1, and accuracy summary |
To measure real-world prediction performance on image pairs not used during training:
- Create the directory
Model Training and Evaluation\test_images\ - Add paired images following this naming convention:
<id>_temp.jpg ← reference image <id>_test.jpg ← defective image - (Optional) Edit
test_ground_truth.jsonto add known defect locations. - Run the evaluation script:
venv\Scripts\python "Model Training and Evaluation\Single_Test.py"
Outputs are saved to module4_output\ and include annotated images, scatter plots of detected defects, a confusion matrix, and a final evaluation report with accuracy, F1, false-positive rate, and false-negative rate.
Option A: Double-click run_app.bat
Option B: Run in a terminal:
venv\Scripts\streamlit run app.pyThe terminal will display:
You can now view your Streamlit app in your browser.
Local URL: http://localhost:8501
The browser opens automatically. If it does not, navigate to http://localhost:8501 manually.
The sidebar on the left shows the Model Path field. By default it points to:
Model Training and Evaluation\module3_output\best_model.keras
If you saved the model elsewhere, update this field before running inference.
Click the "Upload PCB Image" drag-and-drop zone, or drag a file onto it directly.
Supported formats: JPG, JPEG, PNG, BMP, TIF
Recommended: Clear, top-down, high-resolution PCB photograph
A preview of the uploaded image appears below the uploader immediately after selection.
Click the blue "🚀 Run Defect Detection" button.
A loading spinner appears while the pipeline executes. Processing typically completes in under 3 seconds on a modern CPU.
Four summary tiles appear at the top of the results area:
| Tile | What It Shows |
|---|---|
| Defects Found | Total number of defect regions detected in the image |
| Defect Types | Number of distinct defect classes present |
| Avg Confidence | Mean probability score across all detections (%) |
| Inference Time | Total pipeline execution time (milliseconds) |
A colored status banner shows whether the run met the ≤ 3 000 ms performance target:
- ✅ Green — Inference completed within the target
⚠️ Yellow — Inference exceeded 3 000 ms (consider using GPU or reducing image size)
| Panel | Description |
|---|---|
| Original Input | Your uploaded PCB image |
| Defect Mask | Binary (black/white) segmentation map — white regions are candidate defect areas |
| Anomaly Heatmap | Red-tinted overlay on defect regions for quick visual inspection |
| Annotated Output ✅ | Final image with color-coded bounding boxes and defect class labels |
The annotated image is also displayed at full width below the four panels for detailed inspection.
A table lists every detected defect with:
| Column | Content |
|---|---|
| # | Detection index |
| Defect Class | Color-coded badge (e.g., Missing Hole, Short) |
| Confidence | Percentage with mini progress bar |
| Centroid (cx, cy) | Pixel coordinates of the defect center |
| Bounding Box | x, y, width, height in pixels |
A bar chart summarizes the frequency of each defect type detected in the uploaded image.
Four download buttons appear in the Export Results section:
| Button | Output File | Contents |
|---|---|---|
| 📥 Download Annotated Image | pcb_annotated_<timestamp>.png |
Image with bounding boxes and labels |
| 📥 Download CSV Log | pcb_predictions_<timestamp>.csv |
All detections as a spreadsheet |
| 📥 Download Defect Mask | pcb_mask_<timestamp>.png |
Binary segmentation mask |
| 📥 Download Evaluation Report | pcb_eval_report_<timestamp>.txt |
Performance gate + full defect summary |
Click any button to save the corresponding file to your browser's download location.
| Sidebar Element | Description |
|---|---|
| PCB Defect AI logo | Application branding |
| Model Path | Text input to specify the .keras model file path |
| Pipeline Steps | Visual numbered indicator of the 5 inference stages |
| Defect Classes | Color-coded badge legend for all 6 defect types |
Cause: The application launched before the model was trained.
Fix:
- Ensure the DeepPCB dataset is placed in
Image Subtraction\DeepPCB_dataset\. - Run
run_pipeline.batand wait for all stages to complete. - Verify the file
Model Training and Evaluation\module3_output\best_model.kerasexists. - Restart the app.
Potential causes and fixes:
| Cause | Fix |
|---|---|
| Low-contrast image | Use a higher-resolution, well-lit PCB photograph |
| Very uniform board | Adaptive thresholding returns few anomalies on uniformly populated boards — inspect the mask panel |
| All contours filtered | The detected regions may have been below the 80 px² minimum area — try a closer crop |
| Cause | Fix |
|---|---|
| Large image (>4 MP) | Resize the image to 1–2 MP before uploading |
| CPU-only machine | Install tensorflow-gpu if an NVIDIA GPU is available |
| First run (model cold load) | Subsequent runs are faster due to model caching |
- Verify Python 3.12 is installed:
py --version - Confirm all dependencies installed:
venv\Scripts\pip show streamlit - Check
requirements.txtinstalled cleanly with no error output. - Review the terminal/command prompt for the full Python traceback.
- Confirm the dataset is extracted to
Image Subtraction\DeepPCB_dataset\. - Folder names are case-sensitive:
Missing_hole,Mouse_bite,Open_circuit,Short,Spur,Spurious_copper. - Do not rename or restructure the dataset folders.
- Stage 2 reads from
Image Subtraction\module1_output\— ensure Stage 1 completed successfully. - Check that the
defect_masks\andaligned\subfolders contain.pngfiles.
For technical users, the complete execution flow during a single-image inference call is:
1. Decode uploaded bytes → OpenCV BGR array
2. Convert to grayscale → apply 5×5 median blur
3. Adaptive Gaussian thresholding (blockSize=31, C=8) → local anomaly map
4. Canny edge detection (low=40, high=160) → structural edge map
5. Bitwise OR (anomaly map + edge map) → combined binary map
6. Morphological closing (5×5) → fill defect blob holes
7. Morphological opening (3×3) → remove isolated noise pixels
8. findContours (RETR_EXTERNAL) → filter by area [80, 50000] px²
9. For each contour:
a. Compute centroid (cx, cy) and bounding rectangle
b. Crop 128×128 patch centred on (cx, cy)
c. Normalize [0, 255] → [0.0, 1.0] float32
d. Run EfficientNetB0 forward pass → 6-class softmax
e. Record class name, confidence, full probability vector
10. Annotate original image with colored bounding boxes and class labels
11. Return results dict → display in UI → enable export
Typical runtime breakdown (CPU):
- Steps 1–8: 20–50 ms
- Step 9 (per contour): 30–80 ms
- Step 10–11: < 5 ms