Time Lapse - open ended

7 posts
by Brew » Tue Aug 06, 2013 7:24 am
Hi there, I have a project I'd like to do where I take time lapse for a period of time then switch to video then back to time lapse.

There are a fair few tutorials on scripting for X time take Y pictures.
Here's a great example from Andrew Mulholland https://github.com/gbaman/RaspberryPi-Camera/blob/master/Timelapse/Timelapse.py
However I'd like to try and create a python scrip that once initiated it would take keep taking pictures until stopped, I can then initiate the video then revert to running the still images.

I would also like to have the pictures named "images DDMMYY TIME.jpg" Can anyone give me any pointers on how to achieve this?
Posts: 19
Joined: Tue Aug 06, 2013 7:12 am
by kaos » Tue Aug 06, 2013 9:25 pm
I've just been doing something similar as a part of my project. I'm a Python noob myself, so you may know all about the following already, but in case it's of use:

To execute raspistill and raspivid (and indeed pretty much any linux command) you can use the subprocess.call command. You will have to start with
Code: Select all
import subprocess
to have access to it. Here is a sample raspivid command from my script:
Code: Select all
Note how the elements of the command are defined as individual strings, which are enclosed is square brackets to form a python list. Also note how the vidparams variable is inserted into the list. This is because vidparams is itself a list which I can use to insert additional parameters for raspivid. There is another way of using subprocess.call, where the whole command, including parameters, is defined as a single string, but that is, I understand, deprecated.
To get the current date and time for filename purposes, you can use the time.time() function, and datetime.datetime.fromtimestamp().strftime() function to convert it to a standard text representation. You will have to
Code: Select all
import time
import datetime
to have access to them. Sample:
Code: Select all
datetime.datetime.fromtimestamp(time.time()).strftime("%Y-%m-%d %H-%M-%S")
This will return you the current time and date in a form like "2013-08-06 20-57-40". Note that raspi doesn't have an on-board battery backed clock, but if it has a functioning network connection it will retrieve the correct time from time servers.

A couple of programs I found useful in conjunction with raspistill and raspivid are exiv2 and MP4Box.
I use exiv2 to extract thumbnails from raspistill images (requires -th parameter for raspistill). That way I can get both a preview and a full size version of the same image with minimal overhead.
MP4Box (part of the GPAC multimedia package) can be used to encapsulate the raw h264 file that raspivid outputs in an mp4 container. Some players, VLC for example, will play the raw h264 file, but many others require mp4 encapsulation, which is where MP4Box comes in.

Regards, Kári.
Posts: 108
Joined: Mon Mar 26, 2012 8:14 pm
by Brew » Fri Aug 09, 2013 11:40 am
Thanks kaos for that info so what I have now is

Code: Select all
import time
import datetime
import os

image_num = 1
date = datetime.datetime.fromtimestamp(time.time()).strftime("%Y-%m-%d %H-%M-%S")

strImage = str(image_num)
os.system("raspistill -o image" + strImage + ".jpg")
print date

How do i get the date and time to appear in the file name of the image?
Posts: 19
Joined: Tue Aug 06, 2013 7:12 am
by TAbbott » Fri Aug 09, 2013 5:46 pm
Hi Brew,

I used this, found elsewhere, to get the date/time to appear on the images:
Code: Select all
filename=$(date -u +"%Y%m%d-%H%M%S").jpg
raspistill  -o $filename

You need to put it in your own loop, something like this (also found elsewhere and hacked up)

Code: Select all

# loop
for (( j = 0 ; j < $total ; j ++ ))

# get the current time in seconds
now=`date +%s`

# modulo (remainder) to see how long until the next dt period
timeleft=`expr $dt - $now % $dt`
echo Waiting $left seconds for next $dt second period
sleep $timeleft

# take the photo
filename=$(date -u +"%Y%m%d-%H%M%S").jpg
raspistill -t 2000 -o $filename
echo Took Photo $j of $total called $filename


Good luck!
Posts: 8
Joined: Thu May 16, 2013 9:35 pm
by PiGraham » Fri Aug 09, 2013 6:15 pm
You should be able to run raspistill in timelapse mode with infinite timeout then end it by sending a signal.
--timelapse, -tl Timelapse mode.
The specific value is the time between shots in milliseconds.
Note you should specify %04d at the point in the filename where
you want a frame count number to appear. For example:
Code: Select all
raspistill -t 30000 -tl 2000 -o image%04d.jpg

will produce a capture every 2 seconds over a total period of
30s, named image1.jpg, image0002.jpg...image0015.jpg. Note
that the %04d indicates a four-digit number with leading zeros
added to pad to the required number of digits. So, for example,
%08d would result in an eight-digit number.

--timeout, -t Time before capture and shut down
The program will run for this length of time, then take the capture
(if output is specified). If not specified, this is set to five seconds.
Setting 0 will mean the application will run continuously until
stopped with Ctrl-C.

Code: Select all
raspistill -t 0 -tl 5000 -o image%04d.jpg

Will keep taking stills every 5 seconds until you stop it.

Stop it with:
Code: Select all
sudo pkill -SIGINT raspistill

which is equivalent to Ctrl-C at the terminal.

There may be better ways to mix video and still capture. If you just start raspivid after stopping raspistill you will have a bit of a gap.

You can stream HD video from raspivid and you can probably opt to either extract still frames and / or video sequences from that feed without dropping frames.

I haven't used it, but you may find gstreamer useful
Posts: 2478
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville
by kaos » Fri Aug 09, 2013 9:45 pm
Brew wrote:How do i get the date and time to appear in the file name of the image?

This should do it:

Code: Select all
date = datetime.datetime.fromtimestamp(time.time()).strftime("%Y-%m-%d_%H-%M-%S")

os.system("raspistill -o images_" + date + ".jpg")

This should give you something like "images_2013-08-09_21-38-16.jpg".

Note that I changed the spaces for underscores, both in the date format string and in the filename. That avoids any ambiguity in what the filename is, and what might be other parameters, both for the raspistill command, and also other file handling command (rm, mv, cp, etc.) that you may later want to use on the file.

Regards, Kári.
Posts: 108
Joined: Mon Mar 26, 2012 8:14 pm
by Brew » Tue Aug 13, 2013 5:45 am
Hi Guys,

Thanks for all the help on this below is what I've got working, it takes a picture named with the date and time then sleeps for 30 seconds and takes another picture. This loops indefinitely.
Code: Select all
import os
from time import sleep

   import time
   import datetime
   date = datetime.datetime.fromtimestamp(time.time()).strftime("_%Y-%m-%d_%H-%M-%S")
   os.system("raspistill -o images/image" + date + ".jpg")
#   print date
   sleep (30)
Posts: 19
Joined: Tue Aug 06, 2013 7:12 am