Created
February 1, 2019 09:24
-
-
Save dsalaj/b9143289784fbd7f741518301e6037ce to your computer and use it in GitHub Desktop.
Extract and downsample frames from video to numpy array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import numpy as np | |
print(cv2.__version__) | |
vidcap = cv2.VideoCapture('vid.mp4') | |
success,image = vidcap.read() | |
count = 0 | |
success = True | |
frames = [] | |
while success: | |
frames.append(image) | |
# cv2.imwrite("frame%d.jpg" % count, image) # save frame as JPEG file if you want | |
success,image = vidcap.read() | |
# print('Read a new frame: ', success) | |
count += 1 | |
print(count, " frames extracted") | |
frames = np.array(frames) | |
print("data shape =\t", frames.shape) | |
def rgb2gray(rgb): | |
return np.dot(rgb[...,:3], [0.299, 0.587, 0.114]) | |
# downsample | |
from scipy import ndimage | |
ds_frames = ndimage.interpolation.zoom(frames,(1., 0.2, 0.2, 1.)) | |
ds_frames = rgb2gray(ds_frames) | |
print("downsampled shape =\t", ds_frames.shape) | |
np.save("frames_data.npy", ds_frames) | |
print("downsampled frames stored to frames_data.npy") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment