YodaVonBeck
Posts: 38
Joined: Wed Mar 16, 2016 10:03 am

Building a timelapse setup with PiCamera

Wed Sep 21, 2016 8:43 am

A friend of mine is building a house and asked me if I could come up with a solution to do a timelapse video.
I looked and there are offtheshelfe solution, but they are rather expensive and not as fun to build as a RPi setup :)

So I have the newest RPi and bought the PiCam.
I found this project : http://www.fotosyn.com/timelapse/ and had a go with the code.
Except for a small problem with the Print statement, I got it working.

The RPi setup will be left unattended for 1-2 weeks in a box.
The change I want to make is so it only takes pictures if its within a certain timeframe (to save space on the memory card)
The problem I cant see to figure out how to solve (Im am new at this) is how to set it up so the loop in the code will only run if its between 8:00AM and 4:00PM

This is the code now :

Code: Select all

import os
import time
import RPi.GPIO as GPIO
import logging
from datetime import datetime

# Grab the current datetime which will be used to generate dynamic folder names
d = datetime.now()
initYear = "%04d" % (d.year) 
initMonth = "%02d" % (d.month) 
initDate = "%02d" % (d.day)
initHour = "%02d" % (d.hour)
initMins = "%02d" % (d.minute)

# Define the location where you wish to save files. Set to HOME as default. 
# If you run a local web server on Apache you could set this to /var/www/ to make them 
# accessible via web browser.
folderToSave = "/home/timelapse/timelapse_" + str(initYear) + str(initMonth) + str(initDate) + str(initHour) + str(initMins)
os.mkdir(folderToSave)

# Set up a log file to store activities for any checks.
logging.basicConfig(filename=str(folderToSave) + ".log",level=logging.DEBUG)
logging.debug(" R A S P I L A P S E C A M -- Started Log for " + str(folderToSave))
logging.debug(" Support at http://fotosyn.com/timelapse/")

# Set the initial serial for saved images to 1
fileSerial = 1

# Run a WHILE Loop of infinitely
while True:
    
    d = datetime.now()
    if d.hour < 99:
        
        # Set FileSerialNumber to 000X using four digits
        fileSerialNumber = "%04d" % (fileSerial)
        
        # Capture the CURRENT time (not start time as set above) to insert into each capture image filename
        hour = "%02d" % (d.hour)
        mins = "%02d" % (d.minute)
        
        # Define the size of the image you wish to capture. 
        imgWidth = 800 # Max = 2592 
        imgHeight = 600 # Max = 1944
        print " ====================================== Saving file at " + hour + ":" + mins
        
        # Capture the image using raspistill. Set to capture with added sharpening, auto white balance and average metering mode
        # Change these settings where you see fit and to suit the conditions you are using the camera in
        os.system("raspistill -w " + str(imgWidth) + " -h " + str(imgHeight) + " -o " + str(folderToSave) + "/" + str(fileSerialNumber) + "_" + str(hour) + str(mins) +  ".jpg  -sh 40 -awb auto -mm average -v")

	# Write out to log file
	logging.debug(' Image saved: ' + str(folderToSave) + "/" + str(fileSerialNumber) + "_" + str(hour) + str(mins) +  ".jpg")

        # Increment the fileSerial
        fileSerial += 1
        
        # Wait 60 seconds (1 minute) before next capture
        time.sleep(60)
        
    else:
        
        # Just trapping out the WHILE Statement
        print " ====================================== Doing nothing at this time"
What would you suggest?

YodaVonBeck
Posts: 38
Joined: Wed Mar 16, 2016 10:03 am

Re: Building a timelapse setup with PiCamera

Wed Sep 21, 2016 7:10 pm

Could I amend it to this?:

Code: Select all

d = datetime.now()
    if 8<d.hour<16:
Would that run the While loop continuously, but only take a picture if d.hour was more than 8 AND less than 16?

YodaVonBeck
Posts: 38
Joined: Wed Mar 16, 2016 10:03 am

Re: Building a timelapse setup with PiCamera

Wed Sep 21, 2016 7:32 pm

Or could this be the best way?

Code: Select all

d = datetime.now()
    if (d.hour=>'8' and d.hour=<'16'):

YodaVonBeck
Posts: 38
Joined: Wed Mar 16, 2016 10:03 am

Re: Building a timelapse setup with PiCamera

Fri Sep 23, 2016 8:13 am

Little help :)

User avatar
bensimmo
Posts: 4550
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Building a timelapse setup with PiCamera

Fri Sep 23, 2016 8:38 am

I would move and use picamera for doing the timelapse
https://picamera.readthedocs.io/en/rele ... -sequences

and then look at gpiozero for the scheduling/timing, just because I know it has it
https://gpiozero.readthedocs.io/en/v1.3 ... #timeofday

They are well developed modules by raspberryPi people.

User avatar
ben_nuttall
Posts: 234
Joined: Sun Aug 19, 2012 11:19 am
Location: Cambridgeshire, UK
Contact: Website Twitter

Re: Building a timelapse setup with PiCamera

Tue Sep 27, 2016 10:46 pm

I see you import the RPi.GPIO module but don't seem to use it in your code. Do you intend to?
bensimmo wrote:I would move and use picamera for doing the timelapse
https://picamera.readthedocs.io/en/rele ... -sequences
Yes - I highly recommend picamera. Pure Python, no os.system calls - it just works, and works brilliantly. See the docs and https://www.raspberrypi.org/learning/ge ... -picamera/
bensimmo wrote:and then look at gpiozero for the scheduling/timing, just because I know it has it
https://gpiozero.readthedocs.io/en/v1.3 ... #timeofday
Unfortunately TimeOfDay is new, beta and (most importantly) doesn't work at present. I'd write a program which takes a single picture, and use cron to schedule it to run periodically.

But I should recommend you use it for general GPIO things :)
Former RPF staff. Author of gpiozero and creator of piwheels.

YodaVonBeck
Posts: 38
Joined: Wed Mar 16, 2016 10:03 am

Re: Building a timelapse setup with PiCamera

Wed Sep 28, 2016 1:33 pm

Thanks for the feedback, I got it working : http://www.silenthunter.dk/?page_id=693

Now I will try and figure out to use the picamera library :)

And you are right, there is no need in the current code to use the RPi.GPIO module, but I intend to add a feature where a diode will light up if pictures are being taken :D

Return to “Beginners”