CV2.imwrite causing latency in program

All,

I have a python code using cv2.imwrite to record images from a capture card (or videos, but I think that opencv can only record as frames from what I know). However, the function is causing a buildup latency in all the data displayed to my GUI (I have some other data being transferred and displayed from a local C++ server). That problem is gone or can be neglect after I command out cv2.imwrite.

Since I do need to save the image for recording purposes, I was wondering if there is a way I can get around of the latency issue caused by cv2.imwrite

Hello, are you able to use threads?

So you can offload the image saving to a different thread instead of waiting for it to finish

Something like this (this is like a pseudocode):

import cv2
import threading
import queue

image_queue = queue.Queue()

# then you can put on the queue on each capture
filename = 'test_filename.png'
image_queue.put((frame, filename))
# And show it later
cv2.imshow('frame', frame)

# then take them on another thread to save them:
def save_images():
    while True:
        image, filename = image_queue.get()
        if image is None:
            break
        # Save the image
        cv2.imwrite(filename, image)
        image_queue.task_done()
threading.Thread(target=save_images, daemon=True).start()

Thank you. I have converted my code to a multi thread code to avoid the slow down caused by cv2.imwrite.