Surveillance or timelapse video on Raspberry Pi


Recently, I had a need to build some semblance of video surveillance. The requirements were pretty simple:
  • the ability to watch online
  • real-time mode is not necessary, it’s enough in the evening to view the main events of the day (say, find out if someone is sitting at your favorite computer while you are at work)
  • no need to spend hours reviewing the results
  • highest possible picture quality
  • minimum cost

Based on the criteria, it would be possible to dwell on ready-made solutions in the form of IP cameras. However, a quick analysis of the market showed that in devices costing up to $ 100, the image size rarely exceeds 640x480 pixels, and at the same time, they cannot boast of good opportunities for setting up software. That is, it is quite possible that having bought such a camera, you will have to put up with the curvature of the factory firmware and the inability to fully realize your plans.

In contrast to ready-made solutions, home-made work on the Raspberry Pi is quite inexpensive, much more exciting, and much more flexible, thanks to a full-fledged OS on board and terabytes of ready-made software for every taste.

To solve the described problems, the following algorithm of work was invented:

At the beginning of the day (on a schedule), a process starts in the system that takes photos at a certain interval (say, once a minute), saves them locally to a memory card and immediately uploads it to some cloud disk using the WebDAV protocol. This provides some semblance of “live” observation, which, although not required by the conditions, is a pleasant bonus. At the end of the day (also on schedule), the photography process is interrupted and the assembly of the video file from the photos starts. At the end of the assembly, the video is uploaded to the same cloud disk, which makes it possible to quickly remotely view a whole avi-flip, without the need to switch between photo files (10 hours of work, per frame per minute, will give a total video timing - only about a minute, when 10 fps).

In this approach, of course, video surveillance is not implemented in its usual form, but all the tasks are solved. In addition, there is no need to buy a dedicated IP from the provider to connect to the camera directly, and there is no need to start a separate video surveillance server, because its storage and access to data functions are transferred to a free service (for example Yandex.Disk ).

So, iron:

  • Raspberry Pi ($ 50- $ 60 in our area): I used model B, the one with Ethernet, two USB and 256 MB of RAM
  • Logitech C270 ($ 20): a relatively cheap and popular HD (1280x720) webcam that meets the UVC specification (as a result, it works without problems on Linux systems)
  • Charging from iPhone ($ 10): in fact, an iPhone, of course, has nothing to do with it, any power supply with an output current of 1A and a micro-USB connector will do. You can find Chinese pulse charging no more than $ 2 apiece, only you need to check them carefully.
  • 4 GB SD card ($ 6): four gigs - the minimum card size for comfortable work.
  • Case for raspberries ($ 1): made using a dremel from a more or less suitable box, purchased at a construction hypermarket.

The assembly looks something like this:



Since this is my first experience with Raspberry, I will dwell a little on things that may already be known to more advanced users.

The first nuance is the power of peripheral devices. The fact is that when you turn on Raspberry from a single-amp source, USB ports get very little current. As a result, I could not connect both a webcam and a USB Wi-Fi dongle, or two webcams: one of the devices worked stably, and the other at first seemed to start up in the system, but after a minute it simply refused to work.

The second nuance is the number of USB ports. There are only two of them in the raspberry, which means connecting the camera, keyboard and wi-fi at the same time will not work. However, there are a couple of solutions.

Firstly, you can connect a powering USB hub to the Raspberry, and plug all the necessary peripherals into it. For example, everything worked for me with this :



It will increase the number of available ports and give them all enough current, although it will require another 220V socket.

Secondly, it makes sense to manage raspberries over SSH - then you do not need to connect a keyboard, mouse or monitor. And if you really want to see the graphical shell, then you can install the VNC server and get remote access to the desktop. This is done like this:

installation:

sudo apt-get install tightvncserver


starting the server on the first port:

vncserver :1 -geometry 1200x700 -depth 16


Now you can connect to the "raspberry" VNC client from under the working OS (for Windows, for example, UltraVNC works fine )

Selection and installation of software:

As the OS, I used Raspbian as the most suitable for non-advanced users. You need to download the system image and install it on the memory card using a special utility (Win32DiskImager for Windows). All this is described in detail on the download page of the official Raspberry website: www.raspberrypi.org/downloads

After the first start of the system, it makes sense to configure the time zone so that the time of the creation of images is displayed correctly.



Of course, this can be done later, as well.

sudo dpkg-reconfigure tzdata


To capture photos from a webcam, I highly recommend using mjpg-streamer. This utility - the only one tested on this configuration of iron, was able to issue a photo in the "correct" resolution. The rest of the tools that I happened to experience (motion, ffmpeg, streamer) did not give an HD picture in any way, offering at best only 640x480, or even refused to start at all. Unfortunately, mjpg-streamer is not common as executable files, however, compiling it is no more difficult than installing a ready-made package. What we will do (it is assumed that all operations are performed on behalf of the user pi).

First, update the local index of packages available for installation:

sudo apt-get update


You can also start the upgrade of packages already installed in the system, if necessary, although in practice everything works without it:

sudo apt-get upgrade


To display the date and time of their creation on photographs, we need the imagemagick package, which includes the convert utility. It has truly unlimited possibilities for changing images, so writing in the corner a dozen numbers with its help is completely uncomplicated. The same package will be needed to compile mjpg-streamer, so we put it without hesitation:

sudo apt-get install imagemagick


The aviconv program is involved in gluing the video from the set of photos, which we will not install on our own, but with the libav-tools package, without which the mjpg-streamer compilation will not be successful. We put:

sudo apt-get install libav-tools


You will also need the libjpeg8-dev package, which also contains the necessary libraries for mjpg-streamer:

sudo apt-get install libjpeg8-dev


To download the source from sourceforge you will need subversion:

sudo apt-get install subversion


And downloading and compiling mjpg-streamer itself is done like this:

sudo svn co https://svn.code.sf.net/p/mjpg-streamer/code/mjpg-streamer/ mjpg-streamer
cd mjpg-streamer
make


The last step is to install the package for working with the remote file system via WebDAV and, in fact, mount it:

sudo apt-get install davfs2
sudo mkdir /mnt/dav
sudo mount -t davfs https://webdav.yandex.ru /mnt/dav -o uid=pi,gid=pi

In the process of mounting, you will need to enter a username and password.

Workflow step by step:

Now we have everything necessary for further work. I will briefly describe the main commands, not particularly leaning on bash, so as not to overload the article. I think that the description of automated scripts to run individual operations may be the reason for a separate essay.

So, run mjpg-streamer:

cd mjpg-streamer 
./mjpg_streamer -i "./input_uvc.so -r 1280x720 -f 1" -o "./output_file.so -f ./ -d 60000"

In this mode, it will once a minute (-d 60000) in the current directory (-f ./) create a file with a beautiful name, like this: 2013_10_04_12_11_30_picture_000000000.jpg.

To draw a timestamp on the image, we use a script that looks something like this:

filename="2013_10_04_12_11_30_picture_000000000.jpg"
timestamp=`stat -c %y $filename`
convert $filename -fill black -draw "rectangle 1130,695 1270,715" -fill white -pointsize 15 -draw  "text 1135,710 '${timestamp:0:19}'" ./out.jpg.

He will put a mark in the lower right corner of the image and write the result in out.jpg. It will turn out something like this:



To write to Yandex.Disk, just copy the desired file to / mnt / dav:

cp out.jpg /mnt/dav


It is worth noting that copying to a remote file system itself is carried out in 2 stages: first, the file is copied to the local cache somewhere in / var / cache / davfs2, and then davfs is already engaged in actually transferring the file over the network. This means that the copy command will complete before the file is physically available in Yandex.Disk. Therefore, do not be discouraged if you do not find the file you just copied on Yandex - most likely you need to wait a bit.

Creating a video from a set of jpegs needs to be prepared: file names should be a sequence of increasing numbers that obey a specific pattern. Here is a script that renames all jpg files in the current directory in ascending time of their modification, and then starts the conversion to avi:

i=0
for f in `ls -tr *.jpg 2>/dev/null`
do
  newf=`printf %06d $i`.jpg
  echo $f "-->" $newf
  mv $f $newf
  i=$((i+1))
done
avconv -r 10 -i %06d.jpg -r 10 -vcodec mjpeg -qscale 1  out.avi


I must say that avconv is also a very powerful utility that can encode almost anything into anything with a good hundred parameters. However, it is worth considering the rather modest capabilities in software encoding-decoding video at Raspberry, so as not to place too many hopes on it. For example, converting a set of approximately 600 files into a movie compressed in H.264 took me about two hours. Therefore, in the example above, I use coding in MJPEG, which essentially does not compress anything, but only glues jpg to each other. Such conversion is performed in a few minutes and does not lose the original quality of the pictures.

Further, with some dexterity and skills in developing bash scripts, you can set up a fully automated system that will turn on and off according to a schedule, remove garbage, monitor the availability of free space on a remote disk, etc. Also, through the disk, you can organize remote control of Raspberry, for example, laying out scripts in a specific directory that Malinka itself will pick up and execute.

In conclusion, I offer an example of a home-made timelapse video. Please note that the time in the lower right corner is incorrect. This is due to the fact that Raspberry does not have a real-time clock, and if it is turned on without an Internet connection, then the last working time will be used, excluding the time zone.


Also popular now: