Skip to content

Instantly share code, notes, and snippets.

@khatbahusain
Forked from dsalaj/extract_frames.py
Created April 2, 2022 09:08
Show Gist options
  • Save khatbahusain/0e81fb5d7c32252462f956af8792c4bf to your computer and use it in GitHub Desktop.
Save khatbahusain/0e81fb5d7c32252462f956af8792c4bf to your computer and use it in GitHub Desktop.
Extract and downsample frames from video to numpy array
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