Skip to content

Instantly share code, notes, and snippets.

@tolleiv tolleiv/cap.py
Last active Apr 15, 2018

Embed
What would you like to do?
Fixed syntax and added documentation for the timelapse script from https://reps.cc/?p=85
#!/usr/bin/env python
import numpy as np
import cv2
from skimage.measure import compare_ssim as ssim
import time
# Pick a source - either a file or a (exiting) device
cap = cv2.VideoCapture('/dev/video0')
ret,previous = cap.read()
best_frame = previous
number = 50
file = 0
while(True):
i = 0
best_s = 0
while(i<=number):
i += 1
ret, frame = cap.read()
s = ssim(frame, previous, multichannel=True)
if(s > best_s):
best_s = s
best_frame = frame
cv2.imwrite('folder/%04d.png' % file,best_frame)
file += 1
previous = best_frame
cap.release()
cv2.destroyAllWindows()
# Installing conda through Berryconda (https://github.com/jjhelmus/berryconda)
wget https://github.com/jjhelmus/berryconda/releases/download/v2.0.0/Berryconda3-2.0.0-Linux-armv7l.sh
chmod +x Berryconda3-2.0.0-Linux-armv7l.sh
./Berryconda3-2.0.0-Linux-armv7l.sh -b -f
# Install the prerequisites for the script above
conda install -c menpo opencv ffmpeg scikit-image
# Run the script as long as you have to
berryconda3/bin/python cap.py
# Rendering the timelapse video
ffmpeg -r 1 -i folder/%04d.png -c:v libx264 -vf "fps=50,setpts=0.25*PTS" -pix_fmt yuv420p out.mp4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.