Skip to content

Instantly share code, notes, and snippets.

@printminion
Last active January 2, 2017 21:01
Show Gist options
  • Save printminion/51768d20fa0b28c1fcfc17f159e56bcc to your computer and use it in GitHub Desktop.
Save printminion/51768d20fa0b28c1fcfc17f159e56bcc to your computer and use it in GitHub Desktop.
Shoot timelaps with the py Camera with raspberry Zero and move it to Google Cloud Storage

#Add two chronjobs for shooting images and uploadng to GCS

sudo crontab -e
#start camera script on startup
@reboot sudo -u pi /usr/bin/python /home/pi/timelaps/timelaps.py 2>&1 | /usr/bin/logger -t TL_CAM

#run movetogcs.sh file once per minute - ony one instance is allowed 
* * * * * /usr/bin/flock -n /tmp/movetogcs.lockfile sudo -u pi /home/pi/timelaps/movetogcs.sh 2>&1 | /usr/bin/logger -t TL_MOVETOGCS

##todo

  • add ini file with configuration data
  • add overlay to images before uploading to gcs
  • create video from local files
    • create video from gcs files
  • upload to youtube channel
#!/usr/bin/env bash
# @desc move files to Google Cloud Storage
# @author Misha M.-Kupriyanov https://google.com/+MishaMKupriyanov
# @link https://gist.github.com/printminion/51768d20fa0b28c1fcfc17f159e56bcc
# @depends https://github.com/printminion/gcsbackup
shopt -s globstar
cd /home/pi/gcsbackup/
DATA_PATH=/home/pi/timelaps_data/
BUCKET_ID=<PUT_YOUR_BUCKET_ID_HERE>
for file_absolute in ${DATA_PATH}**/*.jpg ; do
filename=${file_absolute##*/}
#echo $file_absolute
#file path without DATA_PATH
replace_with=''
file_relative="${file_absolute/$DATA_PATH/$replace_with}"
#echo $file_relative
python ./gcsbackup.py upload $file_absolute gs://$BUCKET_ID/data/$file_relative && rm $file_absolute
#copy bucket file to last.jpg
python ./gcsbackup.py copy gs://$BUCKET_ID/data/$file_relative gs://$BUCKET_ID/last.jpg
#make public
python ./gcsbackup.py predefinedAcl publicRead gs://$BUCKET_ID/last.jpg
done;
#remove empty files
echo "remove empty files..."
find $DATA_PATH -size 0 -print0 | xargs -0 rm
#remove empty folders
echo "remove empty folders..."
find $DATA_PATH -type d -empty -delete
#!/usr/bin/python
# -*- coding: utf-8 -*-
# @desc Shoot timelaps with the py Camera with raspberry Zero
# @author Misha M.-Kupriyanov https://google.com/+MishaMKupriyanov
# @link https://gist.github.com/printminion/51768d20fa0b28c1fcfc17f159e56bcc
# 1) install picamera
# sudo apt-get update
# sudo apt-get install python-picamera
#
# 2) activate picamera
#
#
# 3) add this script to "autostart"
# sudo crontab -e
# @reboot sudo -u pi /usr/bin/python /home/pi/timelaps/timelaps.py
#
# or save to custom path
# @reboot sudo -u pi /usr/bin/python /home/pi/timelaps/timelaps.py /home/pi/custom_data/
import sys
import os
import time
import picamera
VIDEO_DAYS = 2
FRAMES_PER_HOUR = 60
FRAMES = FRAMES_PER_HOUR * 24 * VIDEO_DAYS
DATA_OUTPUT_DIR = "/home/pi/timelaps_data/"
if len(sys.argv) > 1:
DATA_OUTPUT_DIR = sys.argv[1]
if not os.path.isdir(DATA_OUTPUT_DIR):
print "Path does not exist: %s" % DATA_OUTPUT_DIR
sys.exit(1)
def capture_frame(frame):
with picamera.PiCamera() as cam:
cam.resolution = (3280, 2464)
time.sleep(2)
time_str = time.strftime("%Y%m%d-%H%M%S")
outout_path = "%s/%s/%s/%s/" % (
DATA_OUTPUT_DIR,
time.strftime("%Y"),
time.strftime("%m"),
time.strftime("%d")
)
if not os.path.exists(outout_path):
print "Path does bot exits. Create: %s" % outout_path
os.makedirs(outout_path)
capture_file_path = '%s/%s.jpg' % (outout_path, time_str)
cam.capture(capture_file_path)
print "Shot: %s" % capture_file_path
# Capture the images
for frame in range(FRAMES):
# Note the time before the capture
start = time.time()
capture_frame(frame)
# Wait for the next capture. Note that we take into
# account the length of time it took to capture the
# image when calculating the delay
time.sleep(
int(60 * 60 / FRAMES_PER_HOUR) - (time.time() - start)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment