I'm reading in video data using Python OpenCV and want to store K number of frames. Currently, I have loop that does the following (pseudo code):
frame_list = 1:K
frame_buffer = list(map(ReadFrameNumber, frame_list))
I now have a list, frame_buffer
, that is K frames in length with the data being an NxMx3 numpy array. This is all fine and dandy, but now I want to restructure the data so I can effectively use scikit-learn to try some models out. In order to do this, I need to create a numpy array that can be structured as an ((N*M*3) x (K)) or as a ((K) x (N*M*3) matrix. I can successfully do this, but the data is being copied which makes this the function VERY slow. I am using a combination of numpy.ravel
, numpy.asarray
, and numpy.transpose
to accomplish my slow method. I essentially just want a new view to the data.
Here is what I am doing now, and this is NOT working (it takes way too long):
def RearrangeData(data):
b = list(map(np.ravel, data))
b = np.asarray(b, dtype=np.float32)
return b
UPDATE: This is how I am reading frames from opencv:
import numpy as np
import cv2
K= 10
num_frames = K
cap = cv2.VideoCapture(filename)
def PopulateBuffer(num):
cap.set(cv2.CAP_PROP_POS_FRAMES, num)
ret, frame = cap.read()
if not ret:
print("Fatal Error: Could not read/decode frame %d" % num)
exit(-1)
return frame
frame_nums = list(range(0, int(num_frames)))
return (list(map(PopulateBuffer, frame_nums)))