Skip to content Skip to sidebar Skip to footer

Increase The Capture And Stream Speed Of A Video Using OpenCV And Python

I need to take a video and analyze it frame-by-frame. This is what I have so far: ''' cap = cv2.VideoCapture(CAM) # CAM = path to the video cap.set(cv2.CAP_PROP_BUFFERSIZE, 1)

Solution 1:

The reason VideoCapture is so slow because the VideoCapture pipeline spends the most time on the reading and decoding the next frame. While the next frame is being read, decode, and returned the OpenCV application is completely blocked.

So you can use FileVideoStream which uses queue data structure to process the video concurrently.

  • The package you need to install:

  • For virtual environment: pip install imutils
  • For anaconda environment: conda install -c conda-forge imutils

Example code:

import cv2
import time
from imutils.video import FileVideoStream


fvs = FileVideoStream("test.mp4").start()

time.sleep(1.0)

while fvs.more():
    frame = fvs.read()

    cv2.imshow("Frame", frame)

Speed-Test


You can do speed-test using any example video using the below code. below code is designed for FileVideoStream test. Comment fvsvariable and uncomment cap variable to calculate VideoCapture speed. So far fvs more faster than cap variable.

from imutils.video import FileVideoStream
import time
import cv2

print("[INFO] starting video file thread...")
fvs = FileVideoStream("test.mp4").start()
cap = cv2.VideoCapture("test.mp4")
time.sleep(1.0)

start_time = time.time()

while fvs.more():
     # _, frame = cap.read()
     frame = fvs.read()

print("[INFO] elasped time: {:.2f}ms".format(time.time() - start_time))

Post a Comment for "Increase The Capture And Stream Speed Of A Video Using OpenCV And Python"