A production-ready system for automated detection and classification of defects in printed circuit board (PCB) images using deep learning and scalable APIs. This repository contains trained YOLOv8 model weights, an end-to-end inference pipeline, comprehensive analysis notebooks, and containerized deployment for local and cloud demonstration.
- Objective: Accurate, automated detection and localization of PCB manufacturing defects.
- Model: YOLOv8 Nano, trained on a custom multi-class defect dataset.
- API: FastAPI-based REST endpoint supporting image upload and robust error handling.
- Deployment: Docker container available; public cloud deployment for demonstration (Render).
- Notebooks: Includes EDA and training analysis workflows.
- Architecture: YOLOv8 Nano (Ultralytics)
- Training Data: ~600 annotated PCB images (train, validation, test splits combined)
- Classes: 7 distinct PCB defect types
- Performance:
- mAP@0.50: 99.49%
- mAP@0.50-0.95: 83.08%
- Precision: 99.33%
- Recall: 99.99%
- Model Size: 6.2 MB
A robust FastAPI service wraps the detector with a clear REST API.
- Live API:
https://pcb-defect-detection-6moo.onrender.com- Interactive docs available at: https://pcb-defect-detection-6moo.onrender.com/docs
POST /predict
Upload an image and receive detected defect locations and labels as a JSON response.GET /health
Service health and model status.GET /docs
Interactive Swagger (OpenAPI) documentation.
-
Navigate to the interactive documentation:
https://pcb-defect-detection-6moo.onrender.com/docs -
Locate the
POST /predictendpoint and click to expand it. -
Click "Try it out" button in the top-right corner of the endpoint section.
-
Upload your PCB image:
- Click "Choose File" next to the
fileparameter - Select a PCB image from your local machine
- The filename will appear once selected
- Click "Choose File" next to the
-
Click "Execute" to run inference.
- Note: Inference typically takes 20-30 seconds on the free-tier Render deployment
-
View Results:
- Scroll down to the "Response body" section
- JSON response includes:
num_detections: Total number of defects founddetections: Array of detected defects with:class_name: Defect type classificationconfidence: Detection confidence scorebounding_box: Spatial coordinates of the defect
{
"filename": "pcb_sample.jpg",
"image_size": {"width": 640, "height": 480},
"num_detections": 2,
"confidence_threshold": 0.5,
"detections": [
{
"class_id": 0,
"class_name": "defect_1",
"confidence": 0.94,
"bounding_box": {"x1": 120, "y1": 85, "x2": 195, "y2": 140}
},
{
"class_id": 3,
"class_name": "defect_4",
"confidence": 0.89,
"bounding_box": {"x1": 310, "y1": 220, "x2": 380, "y2": 275}
}
]
}import requests
url = "https://pcb-defect-detection-6moo.onrender.com/predict"
files = {"file": open("pcb_image.png", "rb")}
response = requests.post(url, files=files)
print(response.json())- Prerequisites: Python 3.11 or later.
- Install dependencies:
pip install -r requirements.txt - Run API server locally:
Access the API at http://localhost:8000/docs
uvicorn api.main:app --host 0.0.0.0 --port 8000
- Build and run with Docker:
docker build -f docker/Dockerfile -t pcb-defect-api . docker run -p 8000:8000 pcb-defect-api
The repository includes comprehensive Jupyter notebooks for analysis and training:
01_eda.ipynb: Exploratory data analysis and dataset statistics02_training_analysis.ipynb: Training metrics, loss curves, and model evaluation
Located in the notebooks/ directory.
- End-to-end notebooks provided under
notebooks/for EDA, data preparation, and training analysis. - To support additional classes or datasets, update the training notebooks and data configuration.
- Model weights and configuration are easily replaced for further fine-tuning or evaluation.
MIT License.
Key technologies: YOLOv8 (Ultralytics), FastAPI, Docker, Render.