Odisha Computer
Application Centre
Real-Time PCB
Component Detection
Under the Supervision of Mr. Sambit Subhasish Sahu
ct
oje
Pr
ion
is
r V
te
pu
om
C
Prepared By :
Manas R. Das,
Electrical & Computer Engineer
Ajay Binay Institute of Technology,
CDA, Cuttack
Fig. 1: Project Workflow Timeline
Abstract:
This project presents an end-to-end pipeline for the automatic detection and classification of
Printed Circuit Board (PCB) components from high-resolution images. The methodology
integrates classical computer vision techniques with advanced deep learning for robust and
scalable industrial inspection. The workflow encompasses image preprocessing, including
resizing, grayscale conversion, noise reduction, and contrast enhancement to optimize images
for analysis. It then employs contour detection and segmentation to isolate potential
component regions. For component identification, the project leverages both traditional
template matching for standard components (e.g., resistors, capacitors, diodes) and a rule-
based numerical operation approach using features like mean color, area, aspect ratio, and
white pixel ratio. Furthermore, a deep learning-based object detection model, specifically
YOLOv8, is utilized and trained to accurately detect and classify various PCB components.
The project includes a comprehensive evaluation of the detection and classification
performance using metrics such as precision, recall, and F1-score. Emphasizing scalability for
real-time inspection, the solution demonstrates optimizations for efficient processing and
provides instructions for deployment using Streamlit, making it suitable for practical, real-
time industrial applications.
Introduction:
Printed Circuit Boards (PCBs) are the backbone of modern electronic devices, containing a
variety of components such as resistors, capacitors, and integrated circuits (ICs). Manual
inspection of PCBs is time-consuming and prone to human error, especially in high-volume
manufacturing environments. Automating the detection and classification of PCB
components can significantly enhance quality control, reduce costs, and improve production
efficiency.
Objective & Scope:
The objective of this project was to design and implement a real-time computer vision system
capable of automatically detecting and classifying PCB components from live camera feeds.
The system identifies and annotates components such as resistors, capacitors, and ICs, etc.
providing immediate visual feedback for inspection and quality assurance.
Tools Used in This Project:
Fig. 2: Tools & Technologies
Project Architecture:
Fig. 3 Project Architecture
Methodology:
A) Data Handling:
A search was conducted online on Public Dataset providing
domains (e.g. Kaggle, Roboflow, Google Datasets, etc.) The set
of data had to contain images of various components
fabricated into a Printed Circuit Board (PCB). The images if
pre-labeled would be very useful in-order to save our time. If
not pre-labeled then we got to label the expected contour of
components ensuring accuracy via different labelling tools
available in the market (e.g. labelme, CVAT, etc.).
Since we’re using Ultralytics-YOLO we must arrange the
dataset to proper yolo-format. The dataset has to contain
testing, training & validation series of labeled & non-labeled
image (binary format) data & a compulsory [Link] having
all the object classes & original author information.
The fig. 4 represents the proper yolo-formatted dataset. Fig. 4 Dataset Format
Fig. 5 PCB Electronic Components Dataset
The dataset used in this project was taken from Kaggle which was originally available on
roboflow. More information available in the “References” section.
The whole project is created in the form of a Jupyter Python Notebook named “PCB-
[Link]” for the ease of handling & portability, This notebook demonstrates an end-
to-end pipeline for automatic detection and classification of PCB (Printed Circuit Board)
components from high-resolution images using computer vision and deep learning techniques
with a focus on scalability for real-time industrial inspection shown as follows:
1. Import Required Libraries
Import essential libraries for image processing, visualization, and deep learning.
We use:
OpenCV and NumPy for computer vision tasks
Matplotlib for visualization
Ultralytics YOLO for deep learning-based object detection.
2. Load and Visualize PCB Image
Load a high-resolution PCB image and display it for initial inspection. Replace the image
path with your own PCB image as needed. I this case we used an image of Arduino UNO R3
IoT Board named ‘[Link]’ present in the same working directory.
3. Image Preprocessing
Apply preprocessing steps such as:
Resizing
Grayscale Conversion
Noise Reduction
Contrast Enhancement
to prepare the image for analysis.
4. Contour Detection and Segmentation
Detect contours to segment potential component regions from the PCB image using OpenCV.
5. Template Matching for Component Identification
Apply template matching to identify standard components such as resistors, capacitors, and
diodes. You need to provide template images for each component type. Here we have the
image of a sample capacitor 'capacitor_template.png' in the same working directory.
6. Numpy Logic for Component Identification
Apply Numerical Operation to identify standard components such as resistors, capacitors,
and diodes on the basis of pixel area, aspect ratio & white-balance after image processing.
However, this process may not be very efficient since lighting condition & distance form
detector camera may affect the process of detection & might be misleading one.
Hence, Template Matching & Traditional Numpy Implementation is mostly not preferred.
At this point a Deep Learning Classification model got an upper hand over these two method
of PCB components detection. It’s discussed in the next section.
The Numpy logic used for this project is provided below. The process involves:
1. Importing OpenCV & NumPy
2. Image Processing using OpenCV
3. Feature Extraction with OpenCV & NumPy
4. Component classification using Python control structure
5. Contour Detection using OpenCv
6. Real-time Component detection using OpenCv
7. Deep Learning-Based Object Detection
Use a pre-trained or custom-trained deep learning model (e.g., YOLOv8) to detect and
classify PCB components. For demonstration, we use a pre-trained YOLOv8 model via
Ultralytics. Base version of YOLO may not detect components accurately as when we used
yolov5s pretained model.
There for use of a custom trained model is suitable for more accuracy & precision. However
it may also depend upon the model size, parameter range, number of training epoch, dataset
architecture, system architecture, CPU & GPU also the development environment & various
other factors like lighting, distance, etc.
5 epochs completed in 4.529 hours. (Vary from system to system.)
Optimizer stripped from runs\detect\train2\weights\[Link], 6.2MB
This will create the directory: runs\detect\train
Here inside the train folder there present the custom trained model [Link] inside the weights
folder & it also contains the arguments list in yaml format, evaluation plots, results & training
& validation batches.
8. Annotate Detected Components
Draw bounding boxes and labels on the image for each detected
component using the deep learning model's output. We saved this as
‘uno_comp.png’ in our working directory.
Fig. 6 Custom Model
9. Evaluate Detection Accuracy
Calculating metrices such as precision, recall, and F1-score to evaluate the detection and
classification performance.
A. Confusion Matrix:
Key Observations and Insights from This Matrix:
Good Performance: The model performs well at classifying "IC" (80%), "Relay" (62%), and
especially "Background" (84%).
Poor Performance: The model struggles significantly with "Capacitor" (only 14% correct),
"LED" (only 0.01% correct, which seems like a typo, perhaps it is 0.1), and "Resistor" (16%
correct).
Common Confusions:
Capacitors are often mistaken for background.
ICs are confused with potentiometers and transistors.
LEDs are frequently misclassified as ICs.
Resistors are confused with connectors.
Connectors are often labeled as background.
Diodes are confused with inductors and potentiometers.
Many classes are incorrectly identified as "background" (e.g., Capacitor, Connector,
Transistor), suggesting the model might be overly biased towards the background class or
struggles to detect objects when they are present.
Class Imbalance (Potential Issue): The strong performance on "Background" and the high
number of misclassifications into "Background" for other classes might suggest a class
imbalance in the training data, where "background" samples are much more prevalent, or the
model has learned to classify objects as background when uncertain.
B. F1 Curve:
Key Observations and Insights from This Specific F1-Confidence Curve:
1. Optimal Overall Threshold: The model achieves its best average F1-score (0.43) when the
confidence threshold is set to approximately 0.243. This means that if you want to
optimize the overall balance of precision and recall, you should set your model's decision
threshold around 0.243.
2. Varying Performance Across Classes:
IC (Orange Line): This class stands out with the highest F1-score, peaking around
0.75 at a relatively low confidence threshold (around 0.2-0.3). This suggests the model
is very good at identifying ICs.
Relay (Light Blue Line): This class also shows good performance, with an F1-score
peaking around 0.6-0.7.
LED (Green Line): The LED class also shows a relatively high F1-score, peaking
around 0.6-0.7.
Lower Performing Classes: Many other classes (e.g., Capacitor, Resistor, Diode,
Inductor, Potentiometer, Transistor, Connector) have significantly lower F1-scores,
generally peaking below 0.4.
Capacitor (Blue Line): This class has a very low F1-score that drops off quickly,
indicating poor performance and sensitivity to the confidence threshold.
Resistor (Red Line): Similar to Capacitor, it shows poor performance.
3. Sensitivity to Threshold:
For classes like "IC" and "Relay", the F1-score remains relatively high over a broader
range of confidence thresholds before dropping off. This implies that the model's
predictions for these classes are more stable.
For classes with lower F1-scores (e.g., Capacitor, Resistor), the curves are often
steeper or consistently low, indicating that their F1-score is highly sensitive to the
threshold or consistently poor regardless of the threshold.
4. Trade-offs at Different Thresholds:
If you choose a very low confidence threshold (e.g., close to 0.0), the model will make
many predictions, potentially increasing recall but also introducing many false positives
(lowering precision and thus F1).
If you choose a very high confidence threshold (e.g., close to 1.0), the model will only
make predictions when it's very certain, potentially increasing precision but missing many
true positives (lowering recall and thus F1). This is evident by all curves converging to 0
F1-score at a confidence of 1.0.
C. Precision-Recall Curve:
Key Observations and Insights from The PR Curve:
1. Strong Classes: The model performs exceptionally well for "IC" and "Relay", maintaining
high precision even as recall increases. This suggests the model has learned robust features
for these classes.
2. Weak Classes: The model struggles significantly with "Capacitor" and "Resistor", among
others. Their PR curves are very close to the bottom-left corner, indicating that the model
either has low precision (many false positives) or low recall (missing many true positives),
or both, for these categories.
3. Overall Model Performance: The overall mAP of 0.401 is decent but indicates room for
improvement, especially for the weaker classes. The overall curve shows that as recall
increases, precision drops steadily, which is expected, but the rate of drop is quite
significant, particularly after recall values of around 0.3-0.4.
4. Actionable Insights:
For "IC" and "Relay", the model is performing quite well.
For classes with low AP (e.g., Capacitor, Resistor), efforts should focus on:
More training data: Specifically, more diverse and representative examples of these
components.
Improved feature engineering: Designing features that better distinguish these classes.
Addressing class imbalance: If these are minority classes, techniques like
oversampling, undersampling, or using focal loss could be beneficial.
Analyzing misclassifications: Looking at where the model makes errors (using the
confusion matrix) can provide clues.
Relationship between the curves and overall model performance:
Precision-Confidence and Recall-Confidence show the components that make up the F1-
Confidence curve. The F1-score is a balance of precision and recall.
IC and Relay are strong performers across all metrics (high precision, high recall, high F1,
high AP).
Capacitor and Resistor are consistently weak performers (low precision, low recall, low
F1, low AP), indicating significant challenges for the model in detecting and correctly
classifying these components.
The "all classes" lines on both confidence curves clearly illustrate the trade-off. To get
high precision (Precision-Confidence curve approaching 1.0), you need a high confidence
threshold, but this will drastically reduce your recall (Recall-Confidence curve
approaching 0.0). Conversely, to get high recall (Recall-Confidence curve starting high),
you need a low confidence threshold, but this will reduce your precision.
Overall Result Analysis:
Based on these plots, the model training is progressing very well:
Losses are consistently decreasing on both training and validation sets, indicating that the
model is learning effectively and not simply memorizing the training data (no signs of
overfitting yet in these 5 epochs).
Performance metrics (Precision, Recall, mAP) are all significantly increasing on the
validation set, which means the model is getting better at its core task of accurately
detecting and classifying objects.
10. Scalability for Real-Time Inspection
Discuss and demonstrate code optimizations or batching for real-time or near real-time PCB
inspection scenarios.
For Streamlit Deployment:
Create:
- "[Link]" file for listing Dependencies
- "[Link]" file for Deployment Code
Install dependencies:
pip install -r [Link]
Run using following command:
python -m streamlit run [Link]
Or simply
streamlit run [Link]
[Link]
import streamlit as st
import cv2
from ultralytics import YOLO
import numpy as np
[Link]("YOLOv8 PCB Component Detection")
# Load model once
@st.cache_resource
def load_model():
return YOLO('runs/detect/train2/weights/[Link]')
model = load_model()
# Webcam or image upload
option = [Link]("Choose input source:", ("Webcam", "Image Upload"))
if option == "Webcam":
run = [Link]('Start Webcam')
FRAME_WINDOW = [Link]([])
cap = None
if run:
cap = [Link](0)
while run:
ret, frame = [Link]()
if not ret:
[Link]("Failed to grab frame")
break
results = model(frame)[0]
for box in [Link]:
x1, y1, x2, y2 = map(int, [Link][0])
label = [Link][int([Link][0])]
[Link](frame, (x1, y1), (x2, y2), (0,255,0), 2)
[Link](frame, label, (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255,0,0), 2)
FRAME_WINDOW.image([Link](frame, cv2.COLOR_BGR2RGB))
if cap:
[Link]()
else:
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
file_bytes = [Link](bytearray(uploaded_file.read()), dtype=np.uint8)
img = [Link](file_bytes, 1)
results = model(img)[0]
for box in [Link]:
x1, y1, x2, y2 = map(int, [Link][0])
label = [Link][int([Link][0])]
[Link](img, (x1, y1), (x2, y2), (0,255,0), 2)
[Link](img, label, (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255,0,0), 2)
[Link]([Link](img, cv2.COLOR_BGR2RGB), caption="Detected Components", use_column_width=True)
Outcomes:
A functional prototype that can detect and classify basic PCB components in real time.
Immediate visual feedback with annotated bounding boxes and labels for each detected
component.
A modular codebase that can be extended with advanced features, such as more advanced
& large scaled machine learning-based classification or support for additional component
types.
The project talks about 3 different methodologies to detect the PCB components in real
time:
a. Template Matching
b. NumPy Logic
c. Deep Learning Model
This is in the increasing order of accuracy where Template Matching is the least reliable
method, NumPy Logic is constraints to Environmental Conditions (e.g. Lighting &
Distance from Detector Camera) & Deep Learning Model is the most reliable with higher
training epoch & large dataset training.
Significance & Future Outlook:
This project demonstrates the feasibility of using computer vision for automated PCB
inspection. It provides a foundation for further development, including integration with
highly trained deep learning models for higher accuracy and deployment in industrial settings
for large-scale quality control.
Conclusion:
By leveraging computer vision techniques, this project automates and improve the reliability
of PCB component inspection, paving the way for smarter and more efficient electronics
manufacturing processes.
References:
1. Dataset Used:
roboflow:
workspace: rahul-lmk51
project: mydataset-ohm6o-zlegu
version: 1
license: CC BY 4.0
url: [Link]
2. YOLOv8 ( Source: [Link] )