如何優化 Python 即時電腦視覺推理流程以進行邊緣監測
Optimizing Real-Time Computer Vision Inference Pipelines in Python for Edge Monitoring
這篇指南示範如何在 Python 中以影格抽樣和非同步推理,優化邊緣監測的即時電腦視覺流程。透過容量為 1 的佇列,只處理最新影格並自動丟棄過時影格;另以輕量記錄追蹤推理延遲和影格丟棄率。
The Latency Bottleneck in Edge Vision
Running object detection models like YOLOv8 on edge hardware requires careful memory and frame-rate management. Processing every single frame from a camera stream often saturates the CPU/GPU without adding meaningful diagnostic value—especially when monitoring slow-changing physical environments like plant health or industrial gauges.
In this guide, we will implement a frame-sampling and asynchronous inference wrapper in Python to keep latency low and resource usage predictable.
Designing a Non-Blocking Inference Queue
Instead of running model inference directly inside the camera capture loop, we decouple frame capture from model execution using a bounded queue.Queue of size 1. This ensures the model always evaluates the most recent frame while dropping stale frames automatically.
import threading
import queue
import time
class EdgeInferencePipeline:
def __init__(self, model, target_fps: float = 2.0):
self.model = model
self.interval = 1.0 / target_fps
self.frame_queue = queue.Queue(maxsize=1)
self.running = False
def push_frame(self, frame):
if self.frame_queue.full():
try:
self.frame_queue.get_nowait()
except queue.Empty:
pass
self.frame_queue.put(frame)
def _worker(self):
while self.running:
start_time = time.perf_counter()
try:
frame = self.frame_queue.get(timeout=1.0)
results = self.model.predict(frame, verbose=False)
self.handle_detections(results)
except queue.Empty:
continue
elapsed = time.perf_counter() - start_time
sleep_time = max(0.0, self.interval - elapsed)
time.sleep(sleep_time)
def handle_detections(self, results):
for r in results:
boxes = r.boxes
print(f"Detected {len(boxes)} objects at {time.strftime('%X')}")
def start(self):
self.running = True
threading.Thread(target=self._worker, daemon=True).start()
def stop(self):
self.running = False
Adding Basic Telemetry Metrics
To monitor how healthy our inference service is in production, we should track average inference duration and frame drop rates:
def measure_inference(model, sample_image):
t0 = time.perf_counter()
_ = model.predict(sample_image, verbose=False)
latency_ms = (time.perf_counter() - t0) * 1000
print(f"Inference latency: {latency_ms:.2f} ms")
return latency_ms
Conclusion
Decoupling frame capture from model inference prevents memory leaks and keeps edge hardware responsive. Combined with lightweight latency logging, you can reliably deploy computer vision models on local hardware without cloud round-trip delays.
來源:DEV Community · dev.to