So my first very simple project on the TX2 was less than satisfactory. I work with videos and one of the first pre-processing steps is background subtraction. Currently on a PC, I use CV2 MOG background subtraction (cv2.createBackgroundSubtractorMOG2), after a bit of research I decided to try VPI background subtraction (vpi.BackgroundSubtractor) on the TX2. The results were not what I expected. With CV2 MOG on the TX2, the processing of a 1.6 MB file (about 170 800x600 frames) took about 4.7 seconds. VPI CUDA subtraction took 5.7 seconds on the TX2, VPI CPU subtraction took 15 seconds. Is CV2 MOG background subtraction a much faster process than what VPI background subtraction uses? Am I not using VPI background subtraction correctly? The faster the better! Any pointers would help. Below are the two scripts I used:
VPI
import cv2
import vpi
import time
cap = cv2.VideoCapture(“path/to/video.mp4”)
videosize = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
with vpi.Backend.CUDA:
cuda_sub = vpi.BackgroundSubtractor(videosize, vpi.Format.BGR8)
start_time=time.time()
while True:
ret, frame = cap.read()
if ret != True:
break
mask, image = cuda_sub(vpi.asimage(frame, vpi.Format.BGR8), learnrate=0.01)
execution_time = (time.time() - start_time)
print("Execution time: " + str(execution_time))
cap.release()
CV2 MOG
import cv2
import vpi
import time
cap = cv2.VideoCapture("“path/to/video.mp4”)
subtractor = cv2.createBackgroundSubtractorMOG2(history=10, varThreshold=25, detectShadows=False)
start_time=time.time()
while True:
ret, frame = cap.read()
if ret != True:
break
frame_mask = subtractor.apply(frame)
execution_time = (time.time() - start_time)
print("Execution time: " + str(execution_time))
cap.release()