简体   繁体   中英

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)
    while cap.isOpened():
        ret, capture = cap.read()
        cv2.cvtColor(capture, frame, cv2.COLOR_BGR2GRAY)
        cv2.imshow('frame', capture)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
        analyze_frame(frame)
    cap.release()

'''

This works, but it's incredibly slow. Is there any way that I can get it closer to real-time?

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 fvs variable 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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM