Skip to content

Instantly share code, notes, and snippets.

Created November 3, 2015 14:43
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/b45a9b7fd95b79c49fe0 to your computer and use it in GitHub Desktop.
Save anonymous/b45a9b7fd95b79c49fe0 to your computer and use it in GitHub Desktop.
import cv2
import numpy as np
def get_frame_averages(video_path):
vc = cv2.VideoCapture(video)
if vc.isOpened():
rval, frame = vc.read()
else:
rval = False
frames = []
while rval:
rval, frame = vc.read()
frame = np.reshape(frame, (-1, frame.shape[2]))
av = np.average(frame, axis=0)
frames.append(av)
vc.release()
return np.array(frames)
def squish_averages(averages, n):
new_image = []
frame_per_bucket = len(averages) / width or 1
for frames in np.split(averages, range(frame_per_bucket, len(averages), frame_per_bucket)):
av = np.average(frames, axis=0)
new_image.append(av)
return np.array(new_image)
video = 'test.mp4' # input video
imagePath = 'test.png' # output image
height = 1080 # height of output image
width = 1920 # width of output image
frames = get_frame_averages(video)
image = squish_averages(frames, width)
n, d = image.shape
image = np.repeat(image, height, axis=0)
image = np.reshape(image, (n, height, d))
cv2.imwrite(imagePath, image.transpose((1, 0, 2)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment