Oliver Hagen

Making

Timelapse Cam

Using Python and a webcam

Introduction

In the summer before I went to university, I heard that some student accommodation nearby to where I would be staying had been burgled. My solution was to create a surveillance camera for my room, to be set every time I left my accommodation. I hacked a basic version of this together over the summer, and quite quickly became much more interested in the movement of clouds than in the presence of potential thieves. Given this change of purpose, I eventually modified the programme to focus on timelapse photography.

Features

Two helper functions show you all available video sources and let you set your preference, and a further function called 'view' acts as a viewfinder so you don't have to fire up a separate programme to position your webcam. A mostly useless function called 'split' exists to split an existing video into its frames.

The two main functions are called 'Capture' and 'Render', and do what you'd expect. 'Capture' gives you the option of taking pictures for a certain amount of time, or taking a certain number of pictures- both options prompt for a delay time of x seconds between pictures. 'Render' asks you for a photo folder to render, an output name, a framerate, and whether you want timestamp data added or not.

Implementation

The programme uses the cv2 and numpy libraries to get stuff done. I won't go through the whole code body because it's really scruffy code that makes me sad to look at (refactoring this is somewhere on the priority list, right near the bottom), but I will describe one particular problem and solution:

time.sleep(x) is very likely to go over the time x, especially when the programme's run on a low-powered device like a Raspberry Pi. It was being used in the programme to control the delay between taking pictures. For this it was insufficient- the timings would drift off considerably, due both to the lag from running on a slower machine, and also the fact that taking pictures isn't an instantaneous operation, and adds even more lag to the process (as above, increasing on low-powered systems). The solution was to instead measure time by taking the exact time (time.time()) straight after a picture is taken, and then waiting for the difference between that time and the current time - continually checked - to be greater than or equal to the specified delay. This factors in any lag as part of the delay time, and keeps the programme running exactly to schedule.

Where to find

The latest version can be downloaded here.


Last updated 2024-11-07