Created
December 21, 2018 03:25
-
-
Save nickymarino/9d716e97f4043924badd7a03d759e99f to your computer and use it in GitHub Desktop.
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 # opencv.org | |
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() | |
av = np.average(frame, axis=1) | |
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 file path | |
imagePath = 'test.png' # output image path | |
width = 1920 # width of resulting image | |
frames = get_frame_averages(video) | |
image = squish_averages(frames, width) | |
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