Ask Your Question

Revision history [back]

How to save Background Subtraction MOG to reuse between executions

I have the classic Python code for Background Substraction MOG (OpenCV 2.x) and it works well:

backsub = cv2.BackgroundSubtractorMOG()
capture = cv2.VideoCapture(filename)
While True:
    ret, frame = capture.read()
    fgmask = backsub.apply(frame, None, 0,005)

I know there are some parts missing, like checking if frame is something and so, but this is the backbone, and it does work.

What I would like to do is to save the BackgroundSubtractorMOG in some file, and then reuse again when I execute the code some time later.

I have an script that records 1 minute videos and then it runs the backsub. The problem with my current method is that I lose around 3s of video while it tries to guess what is background and what is not, so that is why I would like to reuse the previous information, to be more efficient.

I tried to pickle backsub, and it says it cannot be pickled.