Building a Real-Time Defect Detection System

Case Study Tutorial

Project Overview

In this case study, we’ll walk through building a complete real-time defect detection system for a manufacturing line, from data collection to deployment.

Requirements

  • Process 30 parts per minute
  • Detect 5 different defect types
  • < 1% false positive rate
  • 99% uptime

System Architecture

1
2
[Camera] → [Image Capture] → [Preprocessing] → [AI Model] 
    → [Post-processing] → [Decision Engine] → [Alert System]

Phase 1: Data Collection (Week 1-2)

Camera Setup

  • Resolution: 2048x2048 pixels
  • Frame rate: 60 FPS
  • Lighting: LED ring light, 5500K
  • Position: 30cm above conveyor

Data Gathering Strategy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import cv2
from datetime import datetime

def capture_training_data():
    cap = cv2.VideoCapture(0)
    frame_count = 0
    
    while frame_count < 5000:
        ret, frame = cap.read()
        if ret:
            timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
            filename = f"data/raw_{timestamp}_{frame_count}.jpg"
            cv2.imwrite(filename, frame)
            frame_count += 1
            
            if frame_count % 100 == 0:
                print(f"Captured {frame_count} images")

Phase 2: Data Annotation (Week 2-3)

Labeling Process

  1. Use CVAT for batch annotation
  2. Define clear defect categories
  3. Multiple annotators for quality
  4. Regular calibration meetings

Quality Checks

  • Inter-annotator agreement > 95%
  • Regular spot checks
  • Clear edge case guidelines

Phase 3: Model Development (Week 3-5)

Model Selection

After testing multiple architectures:

  • YOLOv8m: Best balance of speed/accuracy
  • Input size: 640x640
  • Augmentation: Heavy for robustness

Training Configuration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from ultralytics import YOLO

model = YOLO('yolov8m.pt')

results = model.train(
    data='defects.yaml',
    epochs=150,
    imgsz=640,
    batch=32,
    patience=20,
    optimizer='AdamW',
    lr0=0.001,
    weight_decay=0.0005,
    augment=True,
    mosaic=1.0,
    mixup=0.1
)

Results

  • Precision: 97.8%
  • Recall: 96.5%
  • mAP@50: 98.2%
  • Inference time: 22ms

Phase 4: Optimization (Week 5-6)

TensorRT Conversion

1
2
3
4
5
6
7
8
# Export to TensorRT for 3x speedup
model.export(format='engine', device=0, half=True)

# Load optimized model
model_trt = YOLO('best.engine')

# Inference time improved
# Before: 22ms → After: 7ms

Post-Processing Pipeline

1
2
3
4
5
6
7
8
9
def filter_predictions(results, conf_threshold=0.7):
    """Remove low-confidence and overlapping detections"""
    filtered = []
    for box in results.boxes:
        if box.conf > conf_threshold:
            filtered.append(box)
    
    # Non-maximum suppression
    return nms(filtered, iou_threshold=0.45)

Phase 5: Deployment (Week 6-7)

Hardware Setup

  • GPU: NVIDIA Jetson AGX Xavier
  • RAM: 32GB
  • Storage: 512GB NVMe SSD
  • I/O: GPIO for PLC integration

Production Pipeline

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class DefectDetector:
    def __init__(self, model_path):
        self.model = YOLO(model_path)
        self.queue = Queue(maxsize=10)
        
    def process_frame(self, frame):
        # Preprocess
        img = self.preprocess(frame)
        
        # Detect
        results = self.model(img)
        
        # Post-process
        defects = self.filter_predictions(results)
        
        # Log results
        self.log_detection(defects)
        
        return defects
    
    def run(self):
        # Main processing loop
        while True:
            frame = self.queue.get()
            defects = self.process_frame(frame)
            
            if defects:
                self.trigger_alert(defects)

Phase 6: Monitoring (Ongoing)

Key Metrics Dashboard

  • Throughput (parts/minute)
  • Detection rate
  • False positive/negative rates
  • System uptime
  • Processing latency

Continuous Improvement

1
2
3
4
5
6
# Collect edge cases
def log_uncertain_cases(results):
    for r in results:
        if 0.5 < r.conf < 0.75:
            # Save for review and retraining
            save_for_review(r)

Results & Impact

Performance

  • Processing speed: 45 parts/minute (150% of requirement)
  • Detection accuracy: 98.5%
  • False positive rate: 0.7%
  • Uptime: 99.6%

Business Impact

  • 40% reduction in defective products shipped
  • 60% faster quality control
  • $500K annual savings
  • ROI achieved in 4 months

Lessons Learned

  1. Start simple: Begin with classical CV, add AI where needed
  2. Production data is gold: Lab data ≠ production data
  3. Monitoring is crucial: What gets measured gets improved
  4. Budget for iteration: First model is never the final model

Conclusion

Building a production defect detection system requires careful planning, rigorous testing, and continuous iteration. But the impact on quality and efficiency makes it well worth the effort.

Want to discuss your defect detection project? Get in touch!

Code Repository

Full source code available at: github.com/detectdefects/realtime-defect-detection

Don't Miss the Next Insight

Weekly updates on computer vision, defect detection, and practical AI implementation.

Was this article helpful?

Your feedback helps improve future content

James Lions

James Lions

AI & Computer Vision enthusiast exploring the future of automated defect detection

Discussion