在以下代码中,我试图加载视频文件并使用opencv提取帧。但是,代码需要多个GB的内存,直到崩溃。 “数据/记录/”中的文件仅几个MB。如何才能更有效地管理数据?
import numpy as np
import cv2
import os
dir_recordings = 'data/recordings/'
frames = []
for file in os.listdir(dir_recordings):
if file.endswith(".flv"):
file_path = os.path.join(dir_recordings, file)
print(file_path)
# Create a VideoCapture object and read from input file
cap = cv2.VideoCapture(file_path)
# Check if camera opened successfully
if (cap.isOpened() == False):
print("Error opening video stream or file")
# Read until video is completed
while (cap.isOpened()):
# Capture frame-by-frame
ret, frame = cap.read()
if ret == True:
frames.append(image)
else:
break
# When everything done, release the video capture object
cap.release()
保存np.arrayframes).tofile('data/myarray')
时,我注意到,如果我只选择观看视频,它的大小为300MB。另一方面,当我使用
from keras.datasets import mnist
(x_train, _), (x_test, _) = mnist.load_data()
即使mnist阵列中的单元格更多,我也没有出现内存问题。