Raspberry Pi, Motion and the Pi Camera
An application that is often used with the Raspberry and its camera, excepted timelapsing, this is the video surveillance. A tool well suited for this use is the software motion.
There had already been on pihomeserver, a post discussing motion but it applied to USB cameras. Here we see how to use the camera dedicated to the Raspberry on the CSI port. I used the normal camera but there is no reason to not be able to use the NoIR version with a suitable infrared projector.
Main installation
- I went for a clean install installed by network thanks to this article. You get the SD card in less than 2 minutes and the installation will be done from the Raspberry.
- Install raspi-config
apt-get install raspi-config
- Configure the system if necessary
- The locale
- The allocation of memory. I put 1 Mb for video memory
- Overclocking
- Install packages needed for the use of Motion
apt-get install -y motion libjpeg62 libjpeg62-dev libavformat53 libavformat-dev libavcodec53 libavcodec-dev libavutil51 libavutil-dev libc6-dev zlib1g-dev libmysqlclient18 libmysqlclient-dev libpq5 libpq-dev
- Retrieve the version supporting the Raspberry IP Camera
wget https://www.dropbox.com/s/xdfcxm5hu71s97d/motion-mmal.tar.gz
- Unpack the archive
tar zxvf motion-mmal.tar.gz
- Install files that were unzipped
mv motion/usr/bin/motion mv motion-mmalcam.conf/etc/motion.conf
This version of motion by default, uses the configuration file located in /etc and not /etc/motion.
You now have an installation of motion who supports the camera of the Raspberry. For a first test, you can run the following command :
motion -n
Now connect over http://<raspberry_pi_@IP>:8081/ to see the result. The files are all saved in the directory defined in the variable target_dir of the configuration file (/home/pi by default).
I advise you to read carefully this page to know all configuration options because they are many and they are really very useful.
Here is a picture to the following examples :
Option 1 : disable the led of the camera
When the camera is activated, its red LED lights. If you want to disable, at the end of the /boot/config.txt file add the line :
disable_camera_led=1
You will need to restart the Raspberry Pi for the change to be effective.
Option 2 : tag generated images
By default, the images have a text inserted at the bottom on the right with the date and time. You can change in the configuration file /etc/motion.conf (from the line 360) the texts that will be embedded. You have two possibilities : at the bottom left or top right.
If you want to increase these opportunities, you can use the following packages :
apt-get install graphicsmagick-imagemagick-compat libimage-exiftool-perl
You have now several tools at your disposal to manipulate the generated images :
- identify : will allow you to retrieve information from each image as its width or height.
For example the following command will return the width of the image :identify -format '%w' 01-20141206121908-00.jpg
- composite : You can insert your own logo from the image above.
For example to add an image at the top right :composite -geometry +50+50 -gravity NorthEast <image_to_insert> "<source_file>" "<destination_file>"
- convert : to resize the image, crop it, add a frame, change the attributes or add text by choosing the location, the font, the size, etc.
For example, to add a red box at the bottom of the image :convert "<image_file>" -fill '#FF000080' -draw 'rectangle 0,<image_height>-<box_height>,<Image_width>,<image_height>'
To add text at the bottom left :convert "<image_file>" -gravity SouthWest -font <font_name> -pointsize <font_size> -fill <font_color> -draw '5,12 "<text_to_insert>"'
exifTool : change the image EXIF attributes.
For example, to change the city :
exifTool -overwrite_original -IPTC:City="<city_name>" "<filename_to_modify>"
You can now create your script and run it when an image is saved thanks to the management of events in motion. For example, in /etc/motion.conf :
on_picture_save <my_script_to_execute> %f
For example, you can insert the weather and your logo to then broadcast on an internet site.
Option 3 : export images to an external server
If you use your Raspberry Pi to monitor your home, It is likely that the thieves will also take you your precious and therefore SD card (or hard drive) with images. To avoid this situation, you can synchronize the directory with the images with a Google Drive for example or send them to a remote server. In all cases, attention anyway because your username and password will be stored on the card. if the thief is in the dark side of the geeks, he will go to erase the files on the server.
To synchronize with an account Google Drive, You can use Grive, as I you have presented here.
For FTP, You can use the classic ftp like this for example command :
#!/bin/sh cd/path/to/my/images ftp-i <<** open <my_ftp_server_nameOrIp> put <my_image_file> bye *
Think to put in the .netrc file of the user running motion, the identifiers to connect to your server in the form :
machine my_ftp_server_nameOrIp login my_login password my_password
The script will be added to motion with the management of events such as on_picture_save for example, in /etc/motion.conf
Alternatively, you can install the package ncftp who has a command ncftpput who sends files and then directly removes the sources.
Option 4 : remote access to your monitoring server
For Internet access to your server, you need to open the port selected in the configuration (8081 by default) and install for example inadyn to update a domain like no-ip name. You will find instructions on this page to install inadyn.
Option 5 : automatically start motion at the boot of the Raspberry Pi
You modify the configuration file /etc/motion.conf so motion starts daemon with the option mode (line 6):
daemon on
The rights of the various files must be updated :
chown root:root/usr/bin/motion chown root:root/etc/motion.conf chmod 755 /usr/bin/motion chmod 644 /etc/motion.conf
Then the system will start every time motion. Edit the file /etc/default/motion :
start_motion_daemon=yes
Then change directories so that motion can write :
logfile /tmp/motion.log
and
target_dir /var/run/motion
Now, you can manually start the motion with the command
service motion start
or restart your Raspberry
Sources and examples
- http://anderson69s.com/2014/06/28/raspberry-PI-picam-camera-de-securite/
- http://www.epingle.info/?p = 3070
- http://www.NcFTP.com/ ncftp/doc/ncftpput.html
- http://2009/05/encadrement-annotation-et-tags-iptc-de-photo-en-ligne-de-commande-avec-convert-imagemagick-et-exiftool/ www.pervasive-network.org/
- http://ImageMagick.org
- http://www.SNO.PHY.queensu.ca/ ~phil/exiftool/