Sunday, June 19, 2016

An introduction to basic motion detection on Linux

Setting up a motion detection system on Linux is fairly easy and simple. All that we need is a webcam (or laptop), the “motion” package, and a few minutes to set everything up. The purpose for doing this may be private space surveillance, enhancement of personal security, or simply a fun project. Whatever the case, this quick guide is not intended to promote illegal activities such as unauthorized video recording of people and their activities. That said, please use the knowledge offered here with ethical conduct.

Setting Up Motion

The first thing that we need to do is to install the “motion” package. Given that you're using Ubuntu, this is done by opening a terminal and typing:
sudo apt-get install motion
After that, we can launch motion by opening a terminal and typing:
sudo motion
This will initiate motion detection with the default settings, and your webcam will start taking pictures and storing them in the designated location.
To set up motion, you will have to locate and edit motion's configuration file. This can be done by opening a terminal and typing “sudo nano /etc/motion/motion.conf ” or by opening a file manager session as the administrator (“sudo nautilus”), navigating to /etc/motion and opening motion.conf with a text editor. For example, you may change the size of the captured images as the default size is quite small (320x240), or change the trigger threshold. As I found the latter to be very sensitive, I figured out that I should change it by commending out the corresponding rows and changing the numerical value accordingly (raise it). If you want motion to be more sensitive to pixel changes, you may lower this value as needed.
Motion detection with - motion.
Now if the capturing generates too many images, you can set the framerate (located below the width/height settings) at a lower setting. This will tell motion how many times per second it is allowed to capture an image. Alternatively, you may use the minimum frame time to set a minimum time period (in seconds) between captures. The following screenshot shows the problem, as one single movement of my hand in front of the camera generated about 45 images.
Image capturing with motion.
If you don't need that many pictures generated by motion, you can set the utility to either capture only the first image that matches the trigger threshold, the one that has the biggest motion change, or the one where the action happens in the center of the capturing frame. You may enable any of these by navigating to the “Image File Output” section in the configuration file and replacing the word “on” after output_normal with the words “first”, “best”, or “center”.
Configuring motion.
From the same option, you can change to video mode by adding the “off” parameter after “output_normal”. The prerequisite for this to work is that you have “ffmpeg” installed in your system. If you have the popular tool install, you can even set up a camera capture a timelapse video, or even broadcast video live thanks to motion's in-built webserver (set options for this from the “Live Webcam Server” section).
Change video mode in motion - step 1.
Change video mode in motion - step 2.
Now, let's suppose that we want to make motion start with our system by default. This is easily done by opening a file manager session as the admin user, navigating to /etc/default and opening the file named “motion”. There, change the daemon setting by replacing “no” with “yes” and save the file.
Saved files.


  1. If motion is not set up carefully and it captures too many images/videos at a high quality, it can quickly flood your storage devices or even servers in the case that it is set up to send the captured data online.
  2. If you want to use motion for security purposes, be sure to utilize something better than a webcam, as the low quality of webcams are bound to cause many false triggers.
  3. Some options offered by motion like the brightness auto-adjustment can be helpful when you're using a poorly featured camera.
  4. There's a fine line between frame rate and video/image quality and then bandwidth (if you care for it). Consider it before setting up motion, and take into account your camera's relevant capabilities as well.
  5. Multiple cameras must be assigned multiple dedicated configuration files, in addition to the standard motion.conf file which will only be limited to setting up the daemon and storage file paths. The default configuration file is only adequate when using a single image/video capturing device.


Motion may be lightweight and simple to get started with, but if you browse the configurations file carefully, you'll see that there are tons of different options that you may fiddle with. Motion can be added to the start up list so that it starts capturing with the computer power-up, it can be set to add the captures on an online database, work with multiple cameras, beep on activation, or even send notifications to your phone in the form of SMS or email as Motion can also execute custom external commands. If you want to do something specific with it, you may have to change the options multiple times before you end up having the results that you desire, but Motion is certainly worth your time and attention as it is very capable of doing almost everything if set up correctly.

No comments:

Post a Comment