Last active
January 17, 2020 14:57
-
-
Save dmitrysarov/f1488454593c1b14b38979a4283348b5 to your computer and use it in GitHub Desktop.
How to write sequence of frames to video file in cv2
This file contains hidden or 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
def write_avi(frames, path): | |
''' | |
frames - 3D numpy array (seq, height, width) | |
path - result avi file path. Extantion of file have to be .avi | |
''' | |
def norm(x): | |
x = x - x.min() | |
x = x / x.max() | |
return (x*255).astype(np.uint8) | |
frames = norm(frames) | |
fourcc_string = 'MJPG' | |
fourcc = cv2.VideoWriter_fourcc(*fourcc_string) | |
out = cv2.VideoWriter(path, fourcc, fps, frames.shape[1:][::-1]) | |
for i in range(len(frames)): | |
out.write(np.repeat(frames[i][...,np.newaxis],3,-1)) | |
out.release() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment