Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
7701a38
Add Colab notebook and updates
AradhyaStuti Dec 27, 2025
b24691f
Add Colab notebook
Dec 27, 2025
8db692a
Add images to repository
Dec 27, 2025
0614cb2
Add trained.ipynb from Colab
AradhyaStuti Jan 14, 2026
6a7d184
inference.ipynb from Colab
AradhyaStuti Jan 14, 2026
af9e9c5
Rename file
AradhyaStuti Jan 16, 2026
13d5356
Rename file
AradhyaStuti Jan 16, 2026
e076658
Remove README.md
AradhyaStuti Jan 16, 2026
a55bdf6
Organize Image Processing files
AradhyaStuti Jan 16, 2026
6a1114e
Upload full project folder to my-branch (ignore venv)
AradhyaStuti Jan 20, 2026
a42729e
Merge branch 'my-branch' of https://github.com/AradhyaStuti/AI_PCB_De…
AradhyaStuti Jan 20, 2026
ffe3fd9
Organize project in folder: streamlit PCB DEFECT DETECTION AND CLASSI…
AradhyaStuti Jan 20, 2026
a206acf
Delete .gitignore
AradhyaStuti Jan 20, 2026
bc0c659
Added cnn-images folder with two images
AradhyaStuti Jan 20, 2026
5e77557
Added only cnn-images folder with two images
AradhyaStuti Jan 20, 2026
955c1ea
Delete __pycache__ directory
AradhyaStuti Jan 20, 2026
95c2ef9
Move README.md and image folder to root directory
AradhyaStuti Jan 20, 2026
5b30911
Refactor to FAANG-level production quality
AradhyaStuti Mar 23, 2026
065356f
Restore deleted files and FAANG-level refactor
AradhyaStuti Mar 23, 2026
0c82f39
Add comprehensive README with banner and documentation
AradhyaStuti Mar 23, 2026
2ddb7fa
Add comprehensive README with milestones and documentation
AradhyaStuti Mar 23, 2026
ef75bdd
Add internship contributions and changes section to README
AradhyaStuti Mar 23, 2026
4d98f88
Separate internship work and self-improvements in README
AradhyaStuti Mar 23, 2026
5c15880
Add REST API, Docker, env config, persistent logging, 21 tests, updat…
AradhyaStuti Mar 24, 2026
3991efa
update
AradhyaStuti Mar 30, 2026
40236ad
update
AradhyaStuti Mar 30, 2026
1fa86ef
Update README with clearer structure and improved documentation
AradhyaStuti Apr 18, 2026
e7cca78
Humanize code style, tighten gitignore, drop tracked log file
AradhyaStuti Apr 21, 2026
a69bfef
update
AradhyaStuti Apr 21, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.git
.github
__pycache__
*.pyc
*.pyo
.pytest_cache
.mypy_cache
.ruff_cache
logs/
.env
*.ipynb
Image Processing/
image/
16 changes: 16 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# PCB Defect Detection - environment configuration.
# Copy to .env and edit what you need. All keys are prefixed with PCB_.
# Requires python-dotenv for automatic loading.

# Paths (leave blank to use the project-relative defaults).
PCB_MODEL_PATH=
PCB_GOLDEN_DIR=
PCB_LOG_DIR=logs

# API
PCB_API_HOST=0.0.0.0
PCB_API_PORT=8000
PCB_MAX_UPLOAD_MB=10

# Logging: DEBUG, INFO, WARNING, ERROR, CRITICAL
PCB_LOG_LEVEL=INFO
46 changes: 46 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: CI

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.11"
- run: pip install ruff
- run: ruff check .
- run: ruff format --check .

typecheck:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.11"
- run: pip install mypy torch torchvision Pillow numpy imagehash scikit-image matplotlib streamlit fastapi pydantic uvicorn python-dotenv
- run: mypy inference_new.py app.py api.py config.py

test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.11"
- run: pip install torch torchvision Pillow numpy imagehash scikit-image matplotlib streamlit pytest fastapi uvicorn python-multipart python-dotenv httpx --index-url https://download.pytorch.org/whl/cpu --extra-index-url https://pypi.org/simple
- run: pytest -v

docker:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: docker/setup-buildx-action@v3
- run: docker build --tag pcb-defect-detection:ci .
34 changes: 34 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Python
__pycache__/
*.py[cod]
*.egg-info/
dist/
build/

# Tool caches
.pytest_cache/
.ruff_cache/
.mypy_cache/

# Virtual envs
my_virtual_env/
.venv/
venv/

# Model weights (large binaries)
model/*.pth

# Logs
logs/
*.log

# Env files
.env

# IDE
.vscode/
.idea/

# OS
Thumbs.db
.DS_Store
24 changes: 24 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# syntax=docker/dockerfile:1
FROM python:3.11-slim

# System libs needed by scikit-image / Pillow, plus curl for the healthcheck.
RUN apt-get update && apt-get install -y --no-install-recommends \
libglib2.0-0 \
curl \
&& rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Deps first so they stay cached across source changes.
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

RUN mkdir -p logs

# 8000 = REST API, 8501 = Streamlit.
EXPOSE 8000
EXPOSE 8501

CMD ["uvicorn", "api:app", "--host", "0.0.0.0", "--port", "8000"]
1 change: 1 addition & 0 deletions Image Processing/ImageProcessing.ipynb

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 0 additions & 21 deletions LICENSE

This file was deleted.

Binary file added PCB_USED/01.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added PCB_USED/04.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added PCB_USED/05.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added PCB_USED/06.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added PCB_USED/07.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added PCB_USED/08.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added PCB_USED/09.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added PCB_USED/10.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added PCB_USED/11.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added PCB_USED/12.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
158 changes: 157 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,157 @@
# AI_PCB_Defect_Detection_Classification_System
# AI PCB Defect Detection and Classification
A small system for finding and labeling defects on printed circuit boards. It
compares a test image against a defect-free reference, flags the regions that
look wrong, and classifies each one with a CNN.

Built during my internship and then cleaned up afterwards so it can actually be
run, tested, and deployed.


## What it does

Given a PCB image, the pipeline:

1. Picks the closest golden (defect-free) reference from a folder of known
boards, using perceptual hashing.
2. Slides a window across the image and compares each patch to the matching
patch on the reference using SSIM.
3. Sends the patches that differ through a ResNet-50 classifier trained on the
DeepPCB dataset.
4. Runs Non-Max Suppression so overlapping detections collapse into one.

Output is a list of boxes with labels and confidences, and an annotated copy of
the input image.


## Defect classes

From the DeepPCB dataset:

- missing_hole
- mouse_bite
- open_circuit
- short
- spur
- spurious_copper


## Stack

- PyTorch / torchvision (ResNet-50)
- scikit-image (SSIM), ImageHash (pHash), NumPy, Pillow
- Streamlit for the UI, FastAPI for the API
- pytest, ruff, mypy
- Docker + docker-compose
- GitHub Actions for CI


## Layout

```
app.py Streamlit UI
api.py FastAPI endpoints
inference_new.py Detection pipeline
config.py Paths, ports, logging, env vars
model/ Trained weights (.pth, gitignored)
PCB_USED/ Golden reference images
tests/ pytest suite
```


## Running it

Clone and install:

```bash
git clone https://github.com/AradhyaStuti/AI_PCB_Defect_Detection_Classification_System.git
cd AI_PCB_Defect_Detection_Classification_System
python -m venv venv
venv\Scripts\activate # on Windows
pip install -r requirements.txt
```

Then either the UI:

```bash
streamlit run app.py
```

or the API:

```bash
uvicorn api:app --reload
```

You need the trained weights at `model/best_resnet50_pcb_defects_50epochs.pth`
and some golden images in `PCB_USED/`. Paths can be overridden via env vars
(see `.env.example`).


### Docker

```bash
docker-compose up --build
```

Brings up the API on `:8000` and the Streamlit UI on `:8501`.


## API

```
GET /health health check
POST /detect upload a PCB image, get back detections
```

`/detect` takes a multipart file upload and returns JSON with the defect
count, inference time, and a list of `{label, confidence, box}` entries.


## Tests

```bash
pytest -v
```

The tests mock the model, so you don't need the weights to run them.


## Config

All runtime settings are environment variables prefixed with `PCB_`. Copy
`.env.example` to `.env` and edit what you need. See `config.py` for the full
list.


## What I did during the internship

- Got familiar with the DeepPCB dataset and the defect categories
- Wrote the preprocessing and region-extraction logic
- Trained the ResNet-50 classifier (`trained.ipynb`)
- Put together the detection pipeline (`inference.ipynb`)
- Built a Streamlit UI to test it interactively


## What I added afterwards

- Moved config into a single file driven by env vars
- Rewrote the inference pipeline as a class so it loads the model once
- Added batching so classification isn't one-patch-at-a-time
- Added a FastAPI layer and a Dockerfile / compose setup
- Wrote a pytest suite and a GitHub Actions workflow
- Added logging to a rotating file


## Things I'd still like to try

- Transformer-based backbones (ViT, Swin)
- Multi-scale windowing for defects of different sizes
- Real PCB images instead of only the DeepPCB ones


## Author

Aradhya Stuti

- https://github.com/AradhyaStuti
- https://www.linkedin.com/in/aradhya-stuti-9b2b9529a
Loading