Skip to content
TheChatArchiveThe Chat Archive
GPT 5.1 Codex Max· openaiHugging Face Frontier Reasoning Traces

Optimize YOLO object detection for video inference using multiprocessing. Decouple frame…

  1. User

    Optimize YOLO object detection for video inference using multiprocessing. Decouple frame reading from inference with a queue and implement frame skipping to drop old frames and jump to latest when inference falls behind real-time.

  2. Assistant

    Providing multiprocessing YOLO video inference example

    Here's a minimal Python example using multiprocessing to decouple frame capture and YOLOv8 inference with frame skipping to keep real-time performance. It uses the ultralytics library and a queue to drop old frames when inference lags behind. Below is a minimal example showing how to decouple video capture from YOLO inference using Python’s multiprocessing. The capture process puts frames into a queue, and the inference process pulls only the newest frame (dropping older ones) so that inference stays close to real time.

    This example uses the ultralytics YOLO models (e.g. YOLOv8). Install with:

    pip install ultralytics opencv-python
    

    Script (e.g., realtime_yolo_mp.py):

    import cv2
    import time
    import multiprocessing as mp
    from ultralytics import YOLO
    
    def frame_reader(video_source, frame_queue, max_queue=5):
        cap = cv2.VideoCapture(video_source)
        if not cap.isOpened():
            print("Error: Unable to open video source:", video_source)
            return
    
        while True:
            ret, frame = cap.read()
            if not ret:
                print("End of video or cannot grab frame.")
                break
    
            # If queue is full, drop the oldest frame
            if frame_queue.full():
                try:
                    frame_queue.get_nowait()  # discard
                except:
                    pass
    
            frame_queue.put(frame)
    
        cap.release()
        print("Frame reader exiting...")
    
    def yolo_inference(frame_queue, display=True):
        # Load YOLO model (choose your model file, e.g. 'yolov8n.pt')
        model = YOLO("yolov8n.pt")
    
        while True:
            if frame_queue.empty():
                # No frame available yet
                time.sleep(0.001)
                continue
    
            # Skip old frames to get the most recent
            frame = frame_queue.get()
            while not frame_queue.empty():
                frame = frame_queue.get()
    
            # Run inference
            results = model.predict(source=frame, imgsz=640, verbose=False)[0]
    
            # (Optional) draw results on frame
            annotated = results.plot()
    
            if display:
                cv2.imshow("YOLO Inference", annotated)
                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break
    
        cv2.destroyAllWindows()
        print("Inference process exiting...")
    
    if __name__ == "__main__":
        mp.set_start_method("spawn", force=True)  # safer on some platforms
        frame_queue = mp.Queue(maxsize=5)  # adjust maxsize as needed
    
        # 0 for webcam, or replace with a video file path
        video_source = 0
    
        reader_proc = mp.Process(target=frame_reader, args=(video_source, frame_queue))
        infer_proc = mp.Process(target=yolo_inference, args=(frame_queue, True))
    
        reader_proc.start()
        infer_proc.start()
    
        reader_proc.join()
        infer_proc.join()
    

    How it works:

    1. frame_reader runs in its own process, continually reading frames from the video source. If the queue is full, it discards the oldest frame to avoid stalling.
    2. yolo_inference runs in another process, reading only the most recent frame (while not frame_queue.empty(): frame = frame_queue.get()), effectively skipping old frames when the queue lags behind.
    3. This lets you keep the inference loop close to real-time even if YOLO occasionally slows down.

    You can adjust maxsize on the queue and the model size (yolov8n.pt, yolov8s.pt, etc.) to balance speed and accuracy.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.