Is there a way to export/checkpoint OpenCV Background Subtraction for later use?
I have some very long video files to process which require background removal. I would like to cut the video into small chunks and process each chunk separately. However, in doing this I would need to generate a new background subtraction model for each small video chunk. This would eat into my usable data in these very long videos.
Can I checkpoint this black box somehow? If so, how can I start up a video with this exported checkpoint information?
Version Info
- Python 3.6.4
- OpenCV 3.4.1
(Answers from C++ users welcome, though Python is preferred)
MWE
import numpy as np
import cv2
FGBG = cv2.bgsegm.createBackgroundSubtractorMOG(100, 7, 0.5, 5)
MAT = np.ones((3, 3), np.uint8)
pos_frame = 0
while pos_frame < 1000:
cap = cv2.VideoCapture(the_file)
cap.set(cv2.CAP_PROP_POS_FRAMES, pos_frame)
ret, frame = cap.read()
if ret:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
frame = cv2.GaussianBlur(frame, (5, 5), 0)
frame = FGBG.apply(frame, learningRate=.05)
frame = cv2.morphologyEx(frame, cv2.MORPH_GRADIENT, MAT)
cv2.waitKey(1)
pos_frame += 1
# This is a placeholder for what I want to do.
cv2.somehowexportthebackgroundinfofromabove()