Reputation: 20287
This example of reading video from file with cv2.VideoCapture
in python OpenCV runs out of memory:
import cv2
cap = cv2.VideoCapture('file.mp4')
while True:
ret, frame = cap.read()
It takes ~300 frames at 1920x1080 before it runs out. Tested in both OpenCV 3.0.0 beta and 2.4.8, running in latest Pythonxy on Windows 7 64-bit.
What needs to be added to this code to make it not run out of memory but rather free each frame before reading the next frame?
Upvotes: 11
Views: 3666
Reputation: 739
You can use scikit-image
which has a video loader and supports Opencv
and Gstreamer
backends as per documentation. I have never used Gstreamer
.
import cv2
from skimage.io import Video
cap = Video(videofile)
fc = cap.frame_count()
for i in np.arange(fc):
z = cap.get_index_frame(i)
Now use the frame z
for whatever you want to do!
Upvotes: 3
Reputation: 50657
Try
while True:
ret, frame = cap.read()
if(!frame)
break;
to make sure the frame
is valid to avoid reading frames forever even not valid.
Upvotes: 1