This project implements an automated defect detection system for injection-molded parts (Type A and Type B pieces) using a ResNet18 architecture. It is designed to be trained on high-performance hardware and deployed on a Raspberry Pi 5 with yolo for real-time edge defect detection.
This project implements a multi-stage automated defect detection pipeline for industrial quality control. Moving beyond simple classification, the system utilizes a cascading YOLOv8 architecture for precise object localization, cropping, and defect segmentation.
The pipeline is designed for high-performance training on GPU-enabled workstations and real-time edge deployment on a Raspberry Pi 5.
The system operates through a structured vision pipeline:
- Acquisition: Dual-stream image capture via Raspberry Pi 5.
- Detection & Cropping: A YOLOv8 detection model identifies the piece and crops the region of interest (ROI) to eliminate background noise.
- Classification: Resnet18 model classification to identify specific defects (cracks, short molds, or flash) on Type A and Type B pieces.
- Decision: The system evaluates the segmentation mask area to categorize the piece as 'Good' or 'Defective'.
Defect Detection/
├── models/ # Exported .pth and .pt (YOLOv8) model weights
├── notebooks_and_scripts/
│ ├── model/ # General model utilities and data processing
│ ├── pi_setup/ # Pi camera configuration and dual-streaming logic
│ ├── segmentation/ # Training logic for YOLOv8 segmentation models
│ └── yolo/ # Training logic for YOLOv8 detection/cropping models
├── reports/ # Project documentation, PPTs, and industrial reports
├── .gitignore
├── README.md
├── requirements.txt # Dependencies for Python 3.14
└── requirements12.txt # Dependencies for Python 3.12 (Compatibility Mode)
- Clone the Repository
git clone https://github.com/KrP77/defect_detection.git
cd "Defect Detection"- Set Up Virtual Environment On Windows :
python -m venv .venv
.venv\Scripts\activate
pip install -r requirements.txt
python -m venv .venv12
.venv12\Scripts\activate
pip install -r requirements12.txt- Authenticate with Hugging Face:
Because the dataset is large (~50GB), we are using Hugging Face to host our data.
Repository: KrP77-HF/defect_detection/
To download the datasets:
huggingface-cli login(Paste your Access Token with 'Read' or 'Write' permissions when prompted) Because the dataset is large (~10GB), we are using Hugging Face to host our data.
The system is optimized for the Raspberry Pi 5 using the IMX708 (Camera Module 3). To balance real-time monitoring with high-accuracy detection, the project implements a Dual-Stream Processing Pipeline.
To avoid lag in the user interface while maintaining high-resolution inspection, the camera handles two simultaneous streams:
- Preview Stream (Low Res / High FPS): A lightweight stream used for the live operator dashboard. This ensures the visual feedback is smooth and responsive.
- Capture Stream (4K Resolution): High-definition frames are triggered for the actual inspection. This provides the fine detail necessary for the models to identify tiny surface defects.
- Acquisition: The 4K capture stream feeds a frame into the pipeline.
- Detection & Localization (YOLOv8): A YOLO model identifies the location of the plastic piece within the 4K frame.
- Dynamic Cropping: The system crops the image to the bounding box of the piece, discarding the background to reduce computational load.
- Classification (ResNet): The cropped "Region of Interest" is passed to a ResNet-based classifier to determine the quality status (Pass/Fail).
- Visual Feedback: The final classification result is mapped back onto the Low-Res Preview Stream, drawing a real-time colored bounding box (Green for Good, Red for Defective) over the live feed.
- Hardware: Raspberry Pi 5 (8GB recommended), Camera Module 3.
- Software Stack: *
libcamera: For high-level camera control.OpenCV: For frame manipulation and UI overlays.PyTorch / ONNX Runtime: For optimized model inference on the Pi's CPU/GPU.