Last active
September 15, 2020 00:13
-
-
Save ethanrublee/fb86ebe4fc37700e7f902baf351d413b to your computer and use it in GitHub Desktop.
Teaching the kids how to code and make stop motion animations!
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
import numpy as np | |
import cv2 | |
import argparse | |
import os | |
import subprocess | |
parser = argparse.ArgumentParser() | |
parser.add_argument('dev', help='video device') | |
parser.add_argument('out', help='output folder') | |
args = parser.parse_args() | |
cap = cv2.VideoCapture(args.dev) | |
frame_count=0 | |
last_saved_frame = None | |
os.makedirs(args.out) | |
cv2.namedWindow('frame', 0) | |
while True: | |
# Capture frame-by-frame | |
ret, frame = cap.read() | |
if frame is None: | |
continue | |
if last_saved_frame is None: | |
last_saved_frame = frame | |
display = frame * 0.75 + last_saved_frame*0.25 | |
# Display the resulting frame | |
cv2.imshow('frame', display/255) | |
key = cv2.waitKey(10) & 0xFF | |
if key == ord(' '): | |
frame_count+=1 | |
last_saved_frame = frame[:] | |
out_file=os.path.join(args.out,'%05d.png'%frame_count) | |
print('saving %s'%out_file) | |
cv2.imwrite(out_file, frame) | |
elif key == ord('s'): | |
cmd="convert -delay 15 %s/*.png +dither +map %s.gif"%(args.out, args.out) | |
print(cmd) | |
subprocess.check_call(cmd, shell=True) | |
elif key != 255: | |
print('you pressed %s'%chr(key)) | |
if key == ord('q'): | |
break | |
# When everything done, release the capture | |
cap.release() | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Our first animations, made with convert: