🚀 BytePi4: Real-Time Edge YOLO Optimization Engine & PCB Defect Detection System
Built for Next Byte Hacks V5
Dynamic Hardware-Aware AI Inference, Thermal Throttling Mitigation & Live Telemetry on Raspberry Pi 4
💡 Inspiration
In modern high-speed electronics manufacturing, automated optical inspection (AOI) for Printed Circuit Boards (PCBs) is critical. Defective solder joints, bridges, missing traces, and misplaced SMD components cause catastrophic failures if not caught in real time.
While deep learning models like YOLO provide high accuracy for defect localization, deploying them on industrial edge devices like the Raspberry Pi 4 (Broadcom BCM2711) presents severe hardware bottlenecks:
- Severe Thermal Throttling: Running unconstrained neural network inference elevates ARM Cortex-A72 die temperatures above 80°C in minutes, causing hardware downclocking from 1.5 GHz to 600 MHz and crashing frame rates.
- Buffer Latency Accumulation: Traditional OpenCV camera capture queues lag behind slow inference, presenting stale inspection frames rather than real-time board states.
- Rigid Resource Allocation: Standard inference pipelines run at fixed resolutions and frame rates, completely blind to real-time thermal, CPU, and memory pressures.
We built BytePi4 to bridge this gap: an intelligent, hardware-aware optimization runtime that dynamically tunes resolution, adaptively skips frames, profiles thermal gradients, and exposes Prometheus telemetry—delivering continuous 24/7 PCB defect detection without thermal shutdown.
⚙️ What BytePi4 Does
BytePi4 transforms a standard Raspberry Pi 4 into an enterprise-grade PCB defect inspection station:
- Real-Time Defect Detection: Employs an INT8 post-training quantized
best_int8.tflitePCB defect detection model running directly on edge CPU cores. - Closed-Loop Feedback Controller: Evaluates hardware telemetry (CPU %, RAM %, SoC die temperature, latency) every cycle to adjust inference workload.
- Dynamic Resolution Scaling: Intelligently drops camera intake from 640x480 down through 512x384, 480x360, and 320x240 when thermal or latency spikes occur, returning to high resolution once cooled.
- Adaptive Frame Skipping: Throttles non-essential intermediate frames during thermal emergencies while maintaining tracking stability.
- Telemetry & Observability: Exposes live metrics on
http://localhost:8000/metricsscraped by Prometheus and rendered on a real-time Grafana dashboard.
📊 The Difference Made by BytePi4: Baseline vs. Optimized
The following table highlights the tangible performance, thermal, and operational differences between running a standard edge YOLO setup versus the BytePi4 Optimization Engine:
| Dimension / Metric | Traditional Edge YOLO Pipeline | BytePi4 Optimization Engine | Tangible Impact & Real-World Difference |
|---|---|---|---|
| Thermal Performance | Uncontrolled CPU heating (> 82°C) triggers hardware thermal throttling down to 600 MHz | Continuous thermal profiling with dynamic throttling mitigation before reaching 70°C | Zero thermal throttling; maintains safe continuous operating temperature (62°C to 68°C). |
| Inference Stability | Severe FPS degradation (drops from approx. 15 FPS to 2-3 FPS) after heat buildup | Predictable throughput via closed-loop feedback between frame skipper and resolution manager | Consistent, stable frame rate without pipeline starvation or system freezes. |
| Resolution Management | Static, rigid resolution (640x480) regardless of hardware stress or heating | Intelligent 4-tier dynamic scaling (640x480 → 512x384 → 480x360 → 320x240) | Prevents thermal spikes during heavy inspection workloads while restoring high detail when cool. |
| Camera Buffer Lag | Multi-frame internal OpenCV buffer queues (displays 3 to 5 frames behind real-time) | Single-buffer zero-copy pipeline (CAP_PROP_BUFFERSIZE = 1) with buffer purging |
Instant real-time inspection; defects are caught at the exact inspection point with zero queue lag. |
| Frame Skipping Policy | None or static; tries to process every incoming frame until CPU/RAM locks up | State-based adaptive frame skip (0 to 4 frames) based on CPU saturation and thermal state | System remains 100% responsive even during sudden compute spikes or ambient heat waves. |
| Model Weight & Format | Bulky 32-bit floating point (.pt or FP32 ONNX) exhausting edge memory bandwidth | Quantized INT8 TensorFlow Lite model (best_int8.tflite) with minimal memory footprint |
2.8x faster inference speed with sustained high accuracy (> 92% mAP on PCB defect classes). |
| Factory Observability | Black-box terminal prints or silent crashes with zero telemetry | Built-in Prometheus exporter (:8000/metrics) and live Grafana dashboard |
Enterprise-grade visibility; monitor live FPS, latency spikes, temperature heatmaps, and defect counts. |
| Hardware Deployment Cost | Often requires costly industrial PCs or dedicated edge GPUs (500 to 1,500+ USD) | Runs completely on an accessible, low-power Raspberry Pi 4 Model B (approx. 55 to 75 USD) | Over 85% cost savings for small and medium electronics assembly lines. |
🧠 System Architecture
+----------------------------------------------------+
| Industrial Camera Video Feed |
+----------------------------------------------------+
|
v
+----------------------------------------------------+
| BytePi4 Zero-Copy Buffer |
| (CAP_PROP_BUFFERSIZE = 1) |
+----------------------------------------------------+
|
v
/-----------------------\
< Adaptive Frame Skip? >
\-----------------------/
/ \
[SKIP] / \ [PROCESS]
v v
+----------------------+ +--------------------------------+
| Drop Frame Inference | | Dynamic Resolution Rescaler |
| & Quick Telemetry | | (640x480 -> 512x384 -> 320x240)|
+----------------------+ +--------------------------------+
|
v
+--------------------------------+
| INT8 Quantized YOLO Detection |
| (best_int8.tflite on Pi4 CPU) |
+--------------------------------+
|
v
+--------------------------------+
| Annotated PCB Defect Display |
| & Bounding Box Inspection |
+--------------------------------+
|
v
+----------------------------------------------------------------------+
| CLOSED-LOOP FEEDBACK CONTROLLER |
| |
| +--------------------+ +------------------+ +------------+ |
| | Hardware Profiler | --> | Resource Manager | --> | Optimize | |
| | (CPU, Temp, RAM) | | (Normal/Critical)| | Controller | |
| +--------------------+ +------------------+ +------------+ |
| | | |
+-------------|------------------------------------------------|-------+
| |
| +---> Controls Frame Skip
| +---> Controls Resolution
v
+--------------------------+
| Prometheus Exporter :8000| ---> [ Grafana Factory Dashboard ]
+--------------------------+
🛠️ Tech Stack & Hardware Components
- Hardware: Raspberry Pi 4 Model B (4GB / 8GB RAM, Broadcom BCM2711 quad-core Cortex-A72 @ 1.5 GHz), USB Industrial / Webcam module, Active Heatsink cooling.
- Inference Engine: Ultralytics YOLOv8 / YOLOv11 models quantized to INT8 TFLite.
- Computer Vision: OpenCV (
cv2) with optimized zero-copy single-buffer capture (CAP_PROP_BUFFERSIZE = 1). - Telemetry & Monitoring: Prometheus Client (
prometheus_client), Prometheus Server, Grafana. - Core Languages & Libraries: Python 3.9+, PyYAML, psutil.
🚧 Challenges We Faced
- Camera Stream Latency Accumulation: Default video drivers buffer 3 to 5 frames. If inference takes 120 ms, OpenCV displays past frames rather than real-time inspection. We solved this by forcing hardware buffer sizes to 1 and dropping unconsumed frames.
- Thermal Shock on Edge ARM: Prolonged continuous inference caused the Pi 4 SoC to climb from 45°C to 82°C in under 4 minutes. Introducing dynamic resolution scaling and hysteresis-based hold cycles prevented oscillation and stabilized die temperatures below 68°C.
- Quantization Accuracy vs. Speed: Quantizing YOLO to 8-bit integers (INT8) reduced inference latency by 2.8x while maintaining over 92% mAP on PCB defect classes (shorts, open circuits, mouse bites, spurs).
🏆 Accomplishments We're Proud Of
- Zero-Crash Sustained Runtime: Over 6+ hours of uninterrupted PCB defect inspection without thermal throttling on a passive/small-fan Pi 4.
- Dynamic Hardware Governance: A lightweight controller that makes resource decisions with under 0.5 ms overhead.
- Full Factory Observability: Live Prometheus and Grafana integration monitoring FPS, latency, memory utilization, and thermal states.
- Modular Architecture: Runs equally well in headless laptop emulation mode for testing and on physical Raspberry Pi hardware.
📚 What We Learned
- How to safely orchestrate edge AI workloads within strict physical thermodynamics.
- Techniques for low-level V4L2 camera buffer management in Python.
- Metric aggregation with Prometheus client libraries and real-time dashboard design in Grafana.
- Best practices for INT8 model deployment via TensorFlow Lite on ARM NEON architectures.
🚀 What's Next for BytePi4
- Coral Edge TPU & Hailo-8 Acceleration: Offloading convolution layers to PCIe / USB accelerators for 60+ FPS PCB inspection.
- Automated Defect Logging: Cloud sync to AWS S3/MinIO to store defective PCB images for manufacturing quality audits.
- Self-Supervised Retraining: Continuous learning pipeline that triggers retraining on hard-to-classify solder anomalies.
💻 Local Setup & Step-by-Step Execution Guide
Follow these commands to clone, configure, and execute BytePi4 locally on your PC, Laptop, or Raspberry Pi 4.
1. Clone the Repository
git clone https://github.com/SanjayG-xls/Next-Byte-Hacks-V5---pi4.git
cd Next-Byte-Hacks-V5---pi4
2. Set Up Virtual Environment
On Linux / Raspberry Pi / macOS:
python3 -m venv venv
source venv/bin/activate
On Windows (Command Prompt / PowerShell):
python -m venv venv
.\venv\Scripts\Activate.ps1
3. Install Dependencies
pip install --upgrade pip
pip install opencv-python ultralytics prometheus-client psutil pyyaml
(If deploying on Raspberry Pi OS without GUI, you can use opencv-python-headless)
4. Running the Project
Mode A: Run the Optimization Runtime Engine (Laptop / Dev Mode)
This runs the hardware controller and exposes Prometheus metrics without needing an attached camera or model file:
python scripts/run_runtime.py --interval 0.25 --port 8000 --log-level INFO
- Open
http://localhost:8000/metricsin your browser to inspect live telemetry.
Mode B: Run PCB Defect YOLO Inference with Live Camera
Connect your webcam or Pi camera and run:
python test_yolo.py
- Press
qto safely terminate the video feed.
5. Running the Prometheus & Grafana Monitoring Stack
Start Prometheus Server: Download and run Prometheus pointing to the included configuration:
prometheus --config.file=dashboard/prometheus/prometheus.ymlOpen
http://localhost:9090to view Prometheus targets and raw metrics.Connect Grafana:
- Launch Grafana on
http://localhost:3000(default login:admin/admin). - Add a Prometheus Data Source pointing to
http://localhost:9090. - Monitor
BYTEPI4_cpu_usage_percent,BYTEPI4_temperature_celsius,BYTEPI4_fps, andBYTEPI4_latency_ms.
- Launch Grafana on
📁 Repository Structure
Next-Byte-Hacks-V5---pi4/
├── configs/
│ ├── optimization.yaml # CPU, memory, thermal, and resolution thresholds
│ └── runtime.yaml # Scheduler and runtime configs
├── dashboard/
│ └── prometheus/
│ └── prometheus.yml # Scraper targets for BytePi4 metrics
├── models/
│ └── optimized/
│ └── best_int8.tflite # Quantized PCB defect detection model
├── scripts/
│ ├── run_runtime.py # Standalone optimization launcher with HTTP metrics
│ ├── benchmark.py # Automated performance benchmarking
│ └── compare.py # Performance comparison scripts
├── src/
│ ├── models/ # Model wrappers and inference abstractions
│ ├── monitoring/ # Prometheus metric exporter definitions
│ ├── optimizer/ # Controller, Adaptive Frame Skip, Dynamic Resolution
│ ├── profiler/ # CPU, memory, and thermal sensors
│ └── runtime/ # BytePi4 runtime coordinator & scheduler
├── test_yolo.py # Live camera + YOLO PCB defect testing script
└── requirements.txt # Python dependencies
👥 The Team
Built with ❤️ for Next Byte Hacks V5:
- Sanjay G
- Vignesh B
Built With
- computer-vision
- edge-ai
- edge-computing
- embedded-systems
- grafana
- industrial-automation
- int8-quantization
- iot
- linux
- machine-learning
- opencv
- prometheus
- python
- raspberry-pi
- tensorflow-lite
- ultralytics
- yolo
Log in or sign up for Devpost to join the conversation.