run opencv single threaded in camera tracking processes (#24640)

OpenCV's default thread pool is sized to every core, and its idle threads spin while it works on the small motion frames and detection regions in each camera tracking process. On a 16 thread machine replaying 720p clips at 5 fps, tracking CPU dropped from 27-50 ms to 5-8 ms per frame with identical output and no change in wall time. Frames from ffmpeg are now also read directly into shared memory with readinto(), which skips one copy per frame.
This commit is contained in:
Josh Hawkins 2026-10-11 22:01:58 -05:00 committed by GitHub
parent e7164882f5
commit bc06b8c753
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 1 deletions

View File

@ -144,6 +144,11 @@ class CameraTracker(FrigateProcess):
def run(self) -> None:
self.pre_run_setup(self.log_config)
# OpenCV only sees small images here, and its default per-core thread
# pool burns ~5x the CPU of running them inline
cv2.setNumThreads(1)
frame_queue = self.camera_metrics.frame_queue
frame_shape = self.config.frame_shape

View File

@ -79,7 +79,8 @@ def capture_frames(
frame_name = f"{config.name}_frame{frame_index}"
frame_buffer = frame_manager.write(frame_name)
try:
frame_buffer[:] = ffmpeg_process.stdout.read(frame_size)
if ffmpeg_process.stdout.readinto(frame_buffer) != frame_size:
raise EOFError("incomplete frame")
except Exception:
# shutdown has been initiated
if stop_event.is_set():