Code Monkey home page Code Monkey logo

pi-timelapse's Introduction

Raspberry Pi Time-Lapse App

Build Status

There are a ton of different Time-Lapse scripts and apps built for the Raspberry Pi, but I wanted to make a more customized setup for my own needs.

Here's an example time-lapse video I recorded of cirrus clouds in the sky outside my window (click to view on YouTube):

Cirrus clouds on a sunny day - Raspberry Pi Zero W time-lapse by Jeff Geerling

There are many other examples in my Timelapses playlist on YouTube.

Usage

For an in-depth overview, see my blog post Raspberry Pi Zero W as a headless time-lapse camera.

First, make sure the camera interface is enabled—if you don't, you'll see the message Camera is not enabled. Try running 'sudo raspi-config':

  1. Run sudo raspi-config
  2. Go to 'Interfacing Options'
  3. Select 'Camera'
  4. Select 'Yes' for enabling the camera
  5. Select 'Finish' in the main menu and then 'Yes' to reboot the Pi

Now, set up this timelapse app on your Raspberry Pi:

  1. Install dependencies: sudo apt-get install -y git python-picamera python-yaml
  2. Clone this repository to your Pi: git clone https://github.com/geerlingguy/pi-timelapse.git
  3. Copy example.config.yml to config.yml.
  4. Configure the timelapse by modifying values in config.yml.
  5. In the Terminal, cd into this project directory and run python timelapse.py.

After the capture is completed, the images will be stored in a directory named series-[current date].

Run on Raspberry Pi Startup and manage timelapses via Systemd

This project includes a Systemd unit file that allows the timelapse script to be managed like any other service on the system (e.g. start with systemctl start timelapse, stop with systemctl stop timelapse).

To use this feature, do the following:

  1. In your config.yml, set the total_images variable to a large number—as large as you want, within Python's limitations. This way you won't start a timelapse and it stops after very few images are taken.
  2. Copy the timelapse.service file into the Systemd unit file location: sudo cp timelapse.service /etc/systemd/system/timelapse.service.
  3. Reload the Systemd daemon (sudo systemctl daemon-reload) to load in the new unit file.
  4. Choose how you want to manage the timelapse service:
    1. To start a timelapse at system boot: sudo systemctl enable timelapse (disable to turn off, is-enabled to check current status)
    2. To start a timelapse at any time: sudo systemctl start timelapse (if one is not already running)
    3. To stop a timelapse in progress: sudo systemctl stop timelapse

Note: You should not try running a timelapse via the Python script directly and via Systemd at the same time. This could do weird things, and is not a typical mode of operation!

Creating animated gifs or videos

Animated gifs

Requirements: You should install ImageMagick (sudo apt-get -y install imagemagick)

If you have create_gif set to True in config.yml, the Pi will also generate an animated gif immediately following the conclusion of the capture.

Note: Animated gif generation can take a very long time on slower Pis, like the Pi Zero, A+, or original A or B.

Videos

Requirements: You should install FFmpeg (which is actually avconv on Raspbian — sudo apt-get -y install libav-tools)

If you have create_video set to True in config.yml, the Pi will also generate a video immediately following the conclusion of the capture.

Note: Video generation can take a very long time on slower Pis, like the Pi Zero, A+, or original A or B.

Manually Compiling Videos with ffmpeg

You can use ffmpeg on other platforms (or avconv on the Pi) to put together image sequences after the fact. For example, to take a sequence like image00001.jpg to image00248.jpg and generate an MP4 video:

ffmpeg -framerate 20 -i image%05d.jpg -vf format=yuv420p timelapse.mp4

And if you wanted to start the video in the middle of the sequence (e.g. instead of starting at image00001.jpg, start at image00024.jpg), you can pass the -start_number option:

ffmpeg -framerate 20 -start_number 634 -i image%05d.jpg -vf format=yuv420p timelapse.mp4

These commands assume you're inside the folder containing all the images, and output a file named timelapse.mp4 in the same directory.

Manual Settings

For a more pleasing timelapse, it's best to lock in manual settings for exposure and white balance (otherwise the video has a lot of inconsistency from frame to frame). This project allows almost complete control over manual exposure settings through variables in config.yml, and below are listed some rules of thumb for your own settings.

Read more about the Raspberry Pi's Camera hardware.

Resolution

The most common and useful Pi Camera resolutions (assuming a V2 camera module—V1 modules have different optimal resolutions) are listed below:

Size (width x height) Aspect Common name
3280 x 2464 4:3 (max resolution)
1920 x 1080 16:9 1080p
1280 x 720 16:9 720p (2x2 binning)
640 x 480 4:3 480p (2x2 binning)

Binning allows the Pi to sample a grid of four pixels and downsample the average values to one pixel. This makes for a slightly more color-accurate and sharp picture at a lower resolution than if the Pi were to skip pixels when generating the image.

ISO

ISO is basically an indication of 'light sensitivity'. Without getting too deep in the weeds, you should use lower ISO values (60 (V2 camera only), 100, 200) in bright situations, and higher ISO values (400, 800) in dark situations. There's a lot more to it than that, and as you find out creative ways to use shutter speed and ISO together, those rules go out the window, but for starters, you can choose the following manual values to lock in a particular ISO on the Pi Camera:

  • 60 (not available on V1 camera module)
  • 100
  • 200
  • 400
  • 800

Shutter Speed

Most photographers are familiar with the fractional values for common shutter speeds (1s, 1/10s, 1/30s, 1/60s, etc.), so here's a table to help convert some of the most common shutter speeds into microseconds (the value used in config.yml):

Fractional Shutter Speed µs
6 seconds (max) 6000000
1 second 1000000
1/8 125000
1/15 66666
1/30 33333
1/60 16666
1/125 8000
1/250 4000
1/500 2000
1/500 2000
1/1000 1000
1/2000 500

White Balance

White balance values on the Raspberry Pi camera are set by adjusting the red and blue gain values—the green value is constant. You need to amplify red and blue certain amounts to set a specific color temperature, and here are some of the settings that worked in specific situations for my camera. Note that you might need to adjust/eyeball things a little better for your own camera, as some unit-to-unit variance is to be expected on such an inexpensive little camera!

White Balance Setting Color Temperature (approx) red_gain blue_gain
Clear blue sky 8000K+ 1.5 1.5
Cloudy sky / overcast 6500K 1.5 1.2
Daylight 5500K 1.5 1.45
Fluorescent / 'cool' 4000K 1.3 1.75
Incandescent / 'warm' 2700K 1.25 1.9
Candle <2000K TODO TODO

Note: These values will be updated over time as I find more time to calibrate my Pi camera against a few DSLRs and other devices which are much more accurate! Please file an issue if you can help make these mappings better, or find a nicer way to adjust calibrations rather than a red_gain and blue_gain value.

Rotation

Depending on the placement of your camera, the picture taken could be upside down. To correct this, set rotation to a value of 0 (no rotation), or 90, 180 or 270 degrees to rotate the image.

License

MIT License.

Author

This project is maintained by Jeff Geerling, author of Ansible for DevOps.

pi-timelapse's People

Contributors

dulus0 avatar geerlingguy avatar hulmgulm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pi-timelapse's Issues

Get together some ideas of mounting the Pi in a semi-weatherproof enclosure

See: http://fotosyn.com/blog/simple-timelapse-camera-using-raspberry-pi-and-a-coffee-tin#video=

Basically, I would like to find a way to fit my Pi Zero W, the camera, and a 30,000 mAh battery pack inside some sort of tin or box that is at least mostly weatherproof and would allow the Pi to be outside for long periods of time.

Bonus points if it can be dustproof too, so I could use the same rig in my house when I do DIY projects.

And finally, if I can find a way to make it mountable (e.g. using clamps, tripod screw, etc.), that's even better.

Timing is not precise due to use of `sleep`

Something that's totally logical now, but wasn't when I was originally writing the code: If you want to calculate more-or-less precise timings (e.g. 1 picture every 1 second, or 1 picture every 10 seconds), and try to do so to make a specific frame rate, etc... then that's not going to happen if the logic goes:

take_and_store_picture_with_picamera()
sleep(x)

...because take_and_store_picture_with_picamera() takes a non-zero amount of time.

On the Pi Zero W, it actually takes .5s - 1s, and that significantly impacts the time delay in the timelapse.

So I'd like to find a way to more precisely hit the time interval defined in config.yml. Maybe set a timer and have it hit a capture on each interval? Is there a way to thread the actual picture taking in Python? I could whip it up more easily in Node.js or Go, but maybe there's an easy way to tackle this in Python too.

Add example crontab entries to start and stop timelapses at certain times of day

For example:

# Start timelapse at 07:30 CST (12:30 UTC)
30 12 * * * systemctl start timelapse

# End timelapse at 22:00 CST (03:00 UTC)
00 3 * * * systemctl stop timelapse

I'm also looking into the best way to, after the timelapse completes, automatically upload the series somewhere for faster processing. Might just set up an FTP server and have an FTP command in another crontab entry.

Show Animated GIF and Video MP4 commands to aid debugging

Add message for each image capture to understand when image capture takes place and when Animated GIF and Video MP4 creation takes place. To clearly show users what is taking place display the actual commands issued for Animated GIF and Video MP4 creation. This is in preparation to debugging the "TODO These may not get called after the end of the capture_image process..."

Adding

Capture picture: 0
Capture picture: 1
Capture picture: 2
Capture picture: 3
Capture picture: 4
Capture picture: 5
Capture picture: 6
Capture picture: 7
Capture picture: 8
Capture picture: 9

For example, change from

Creating animated gif.
Creating video.

to

Creating animated gif: convert -delay 10 -loop 0 /home/pi/Desktop/pi-timelapse/series-2018-12-20_18-43-32/image*.jpg /home/pi/Desktop/pi-timelapse/series-2018-12-20_18-43-32-timelapse.gif
Creating video: avconv -framerate 20 -i /home/pi/Desktop/pi-timelapse/series-2018-12-20_18-43-32/image%05d.jpg -vf format=yuv420p /home/pi/Desktop/pi-timelapse/series-2018-12-20_18-43-32/timelapse.mp4

How to edit the YAML?

I'm trying to specify a resolution and a white balance, however whenever I try to do something other than the default { }, the program throws an exception. What is the proper way to modify the .yml so that it uses the individual values for resolution or for white balance?

Add configuration / webserver setup so you can browse images

As I've now been using this little app on numerous Pis for projects around the house, and I've also seen some of the nifty timelapse work done by Matthias Wandel (see https://github.com/Matthias-Wandel/imgcomp), I figured it would be nice to have some way of viewing results (and maybe also starting/stopping the timelapses, and maybe even tweaking configuration) via a web UI.

Right now, the process I have for every timelapse is:

  1. Set up Pi, turn it on.
  2. Go to laptop which can SSH into Pi. Log in, kick off a timelapse.
  3. Open up SFTP app, view some photos from the series.
  4. Adjust things if need be, then restart timelapse.
  5. Back in SFTP app, check again.
  6. [Rinse and repeat until all settings are correct]
  7. Start off timelapse, finally.

And it can be even more frustrating if I have to adjust the Pi's focus, especially if my laptop's upstairs and my rig is downstairs or outside. It takes minutes to get it set up.

It would be nice if, at a minimum, I had a web UI that let me browse the current timelapse folders (e.g. via Apache, just listing the directory contents and allowing pictures to be viewed).

And then, it would also be nice to allow starting/stopping timelapse series.

And finally, allowing configuration to be set, then when it is started/stopped/saved, it would write a new config.yml file with the updated settings.

Maybe Python, PHP, or Node.js to keep things simple? Or even a Go app, for some learning and fun, and easier deployment?

Add capability to use with something like Sleepy Pi

Sleepy Pi is a HAT that allows more control over the Pi's power cycle; it includes and RTC and some circuits that can be programmed to boot the Pi on an interval or in response to activity.

I could have the script have a mode where it's called on start up, takes one picture in series, then shuts down the Pi, something like that.

It would likely allow me to extend the battery life into weeks or months instead of 5-7 days, which I currently can get.

Update for compatibility with High Quality Camera

I just pre-ordered the Raspberry Pi High Quality Camera and plan on rigging it up with a nicer C or CS mount lens, to make a slightly more stable (but probably a bit more heavy) timelapse rig I can pop onto a tripod mount and put anywhere.

I also have a C to F-mount adapter, so it might be interesting to see how well my 300mm Nikon lens performs with the ~5.7x crop factor, making it a whopping 1710mm f/5.6 lens!

GIFs aren't animated

First off, thank you for making this script! I'm so excited to get this project going.

I was able to follow your tutorial and run timelapse.py, and I see that it produced a number of images and a gif file, but the .gif seems like it's just a single image. When I run the timelapse.py script, the message Creating animated gif appears before the Time-lapse capture complete! message.

I'm using raspian buster lite on a pi zero w.

On first install on Raspbian Lite, getting 'PiCameraError: Camera is not enabled.'

PiCameraError: Camera is not enabled. Try running 'sudo raspi-config' and ensure that the camera has been enabled.

I got the above error when I started this up on a freshly-imaged Pi Zero W today. Apparently it's not enabled by default, but in my docs for this project I don't have a line anywhere about "configure the Pi and enable the camera". So I think I should add something like:

Note: The Pi Camera hardware must be enabled in the Pi hardware configuration. If you get an error run sudo raspi-config, choose 'Interfacing Options', then 'Pi Camera', and enable it.

Document how to batch-rename then ffmpeg-process a batch of images separately

I seem to do this a lot... so it would be nice to have it documented so I don't have to re-remember all the time:

# Rename images in current dir from `image00042.jpg` to instead start from `image00000.jpg`.
# (On macOS, `brew install rename`)
# TODO: Test this.
rename 's/\d+/sprintf("%05d", $&)/e' *.jpg

# Create timelapse.mp4 in current directory starting from `image00000.jpg`.
ffmpeg -framerate 20 -i image%05d.jpg -vf format=yuv420p timelapse.mp4

(Change framerate to suit your preferences.)

Suggest full-res + downscale for true HD images?

Check out this comment thread on reddit: https://www.reddit.com/r/raspberry_pi/comments/5za7hv/raspberry_pi_zero_w_as_a_headless_timelapse_camera/dewzjve/

In it, /u/XBrav suggests using a higher resolution and downscaling, then cropping, if you want the best HD footage possible (since the sensor mode for 1080p doesn't do any pixel binning).

This might be a good option to try to have by default.

Maybe set things up so if the resolution is 1920x1080, either images are just automatically captured full res and downscaled after, or there's a post-capture pipeline for each image that downscales it on the fly (the latter might not be effective on a slow Pi Zero/W!).

Getting error "could not open vchiq service"

On the latest version of Raspbian/pi-camera/etc.:

mmalipc: mmal_vc_init_fd: could not open vchiq service
mmal: mmal_vc_component_create: failed to initialise mmal ipc for 'vc.camera_info' (7:EIO)
mmal: mmal_component_create_core: could not create component 'vc.camera_info' (7)
Exception in thread Thread-2:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 1073, in run
    self.function(*self.args, **self.kwargs)
  File "timelapse.py", line 66, in capture_image
    camera = PiCamera()
  File "/usr/lib/python2.7/dist-packages/picamera/camera.py", line 367, in __init__
    with mo.MMALCameraInfo() as camera_info:
  File "/usr/lib/python2.7/dist-packages/picamera/mmalobj.py", line 2346, in __init__
    super(MMALCameraInfo, self).__init__()
  File "/usr/lib/python2.7/dist-packages/picamera/mmalobj.py", line 633, in __init__
    prefix="Failed to create MMAL component %s" % self.component_type)
  File "/usr/lib/python2.7/dist-packages/picamera/exc.py", line 184, in mmal_check
    raise PiCameraMMALError(status, prefix)
PiCameraMMALError: Failed to create MMAL component vc.camera_info: I/O error

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.