Increase the capture and stream speed of a video using OpenCV and Python [duplicate]
2024/11/17 13:28:27
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 videocap.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'):breakanalyze_frame(frame)cap.release()
'''
This works, but it's incredibly slow. Is there any way that I can get it closer to real-time?
Answer
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 FileVideoStreamfvs = 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 cv2print("[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))
This question already has answers here:Making python/tkinter label widget update?(5 answers)Closed 8 years ago.I have a python script which I have written for a Raspberry Pi project, the script reads …
I have python 3.5 and I cant install pyHook. I tried every method possible. pip, open the cmd directly from the folder, downloaded almost all the pyHook versions. Still cant install it.I get this error…
This question already has answers here:The tilde operator in Python(10 answers)Closed last year.Is there a function that takes a number with binary numeral a, and does the NOT?
(For example, the funct…
I am trying to scrape data from here but getting error.
I have taken code from here Scraping using Selenium and pythonThis code was working perfectly fine but now I am getting errorwait.until(EC.visibi…
I am trying to allow the user to do this:Lets say initially the text says:"hello world hello earth"when the user searches for "hello" it should display:|hello| world |hello| earthhe…
Im actually looking for the opposite of this question:
Converting string into datetimeI have a list of datetime objects and I want to create a human-friendly string
from them, e.g., "Jan 27 and 3…
This question already has answers here:Removing non numeric characters from a string in Python(9 answers)Closed 5 years ago.I have the following dataframe column>>> df2[Age]1 25
2 35
3 …
I have problem with pygame and python 3 in ubuntu 12.10. When i tried load an image i got this error: pygame.error: File is not a Windows BMP fileBut in python 2.7 all works fine.
I use code from http:…
--- Question closedIt was my mistake, my uWSGI startup script switches to a different virtualenv.--- Original questionIm trying to publish push notifications from my Flask app server to Android APP. Se…