Skip to content

Uncategorized

Autonomous defect recognition from scratch | with Python

Access community, courses and source codes
Logo

AI Vision Academy

Access the code of this tutorial, computer vision courses and an exclusive community on AI Vision Academy

  • Access to over 50+ source codes from Pysource.com/blog
  • Dedicated video courses about computer vision
  • Access to an exclusive community of professionals
  • Real-World AI Projects – Get hands-on experience building practical AI Computer Vision solutions with a structured path.
  • Monthly Coaching Calls – get support and any of your questions answered

Subscribe to our newsletter to learn more

In the fast-paced world of manufacturing and production, real-time defect detection is not just a luxury—it’s a necessity. Whether you’re working with conveyor belt systems or automating quality control, integrating computer vision can drastically enhance efficiency and accuracy.

In this guide, I’ll walk you through how to build a defect detection system from scratch using practical, beginner-friendly methods. This tutorial is based on a real-world implementation and includes all the essentials you need to replicate the system on your own.

Why Real-Time Defect Detection Matters

Automating defect detection helps:

  • Maintain consistent product quality.
  • Reduce human error in quality checks.
  • Save costs associated with manual inspection.

Step-by-Step Overview

1. Data Collection and Preparation

The first step involves collecting video samples of your product on a conveyor belt. Capture videos that include:

  • Products in ideal condition.
  • Products with simulated or real defects (e.g., scratches, missing labels, wrong caps).

Even if you don’t have real defective items, you can create defects manually (e.g., using a marker or removing parts) to begin building your training dataset.

Tips:

  • Record under consistent lighting conditions.
  • Use a stable camera setup.
  • Ensure the product is clearly visible.

2. Object Detection Setup

Start by isolating the product from its background. This significantly reduces noise and increases model accuracy.

Steps:

  • Extract frames from the recorded video.
  • Use annotation tools (e.g., Roboflow, MakeSense.ai) to draw bounding boxes around each object of interest (e.g., bottle, label, cap).
  • Assign class labels to each object.

You will train a YOLO (You Only Look Once) model using these annotated images. YOLO is effective due to its speed and ability to process images in real-time.

3. Training the Detection Model

Using your labeled dataset, train a YOLO object detection model.

Tools:

  • Jupyter Notebook (for reproducibility and clarity).
  • Ultralytics’ YOLOv8 (or the latest version).

Process:

  • Load your annotated dataset into the notebook.
  • Select the detection project type.
  • Train the model and monitor performance metrics.

This produces a .pt file (e.g., best.pt), which is the trained model ready for deployment.

4. Real-Time Video Analysis with OpenCV

Load your YOLO model into a Python script using OpenCV to process your video in real time.

Core steps:

  • Read the video frame-by-frame.
  • Apply the YOLO model to detect objects.
  • Draw bounding boxes around detected items.
  • Display frames for visual verification.

This confirms that your model correctly identifies the product components under real-world conditions.

5. Implement Object Tracking

Detection alone isn’t sufficient. You need to track objects across frames to:

  • Assign unique IDs to products.
  • Avoid repeated inspections of the same item.

Method:

  • Use a Multi-Object Tracking algorithm (e.g., SORT, Deep SORT).
  • Update object states in real-time using bounding box data.
  • Maintain consistency of object IDs across frames.

6. Frame Extraction and ROI Handling

To inspect specific parts, define a virtual line (e.g., mid-screen). When an object crosses this line:

  • Crop the object’s region of interest (ROI).
  • Save this image for further classification.

Advantages:

  • Simplifies classification by reducing input variability.
  • Ensures inspection occurs when the object is fully visible.

7. Classification of Detected Components

Train separate classification models for each component (e.g., label, cap, bottle) to identify defect types.

Preparation:

  • Sort images into folders by class (e.g., good, scratch, wrong label).
  • Train classification models using the same Jupyter interface.

Classification Output:

  • Label each ROI as good or defective.
  • Use multiple categories for detailed defect types.

8. System Integration and Feedback Loop

Combine detection, tracking, and classification into a unified pipeline.

Actions:

  • Display green boxes for good items and red for defective ones.
  • Log classification results for quality reports.
  • Optionally integrate with automation systems (e.g., trigger alerts, stop conveyor belts).

Why Join Our Skool Community?

This guide gives a surface-level view of what’s possible. Inside our AI Vision Academy, you’ll get:

  • Access to full, in-depth courses on computer vision and AI.
  • Exclusive tools and Jupyter Notebooks to simplify implementation.
  • Support and feedback from me and other practitioners.
  • Real-world projects and collaborative learning opportunities.

Whether you’re a beginner or looking to refine your skills, our academy equips you with the practical know-how to build robust AI systems from scratch.

Start Building Today
Don’t wait for the perfect dataset or ideal conditions. Begin with what you have. As shown, even a few annotated images and basic video footage are enough to kickstart your journey in defect detection systems.

👉 Join the AI Vision Academy Today