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.

Considerations

  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.

Conclusion

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.

Share this page:

Suggested articles

13 Comment(s)

Add comment

Comments

By: emb3dd3d

add frontend motioneye and you have a really nice setup..  I included the url to the PI setup because it actually had screenshots to how motioneye looks..

http://www.howtoembed.com/projects/raspberry-pi/95-motioneye-with-raspberry-pi

https://github.com/ccrisan/motioneye/wiki

By: John

Hi, to get more relaible motion detection, with minimum false aterts, is it possibe to add a PIR detector to the system that includes a simple bullet ip camera?  Thanks

By: Alcot

Can I change the size of the captured images as the default size is quite small (320x240), or change the trigger threshold?

If anyone having any problem with Netgear arlo seccurity camera, they can try to netgear arlo support for getting information.

By: officecomsetup

your post is very helpful to thank you for this information.

By: nicholas garrett

Thank you for this amazing writing on the above topic, please keep helping us in the future as well by providing similar content on the internet all the time.

By: Michael Keller

Thanks for sharing this information.

By: Annie Besant

Thanks for posting article..keep posting always

By: shayari

This is a great inspiring article. I am pretty much pleased with your good work. You put very helpful information.

By: Eric

Hi, I have installed Motion. I have a problem accessing it through http : it requires a username and password. I try to insert my usual usernames and password, but it doesn't work. Should I declare a specific user, add one to a specific group, ... ? Thanks

By: azazel

Motion is compleate garbage. Does not work without FFMPEG and takes hours Troubleshooting issues then you get like 3 frames a second. In 2020 this is unacceptable.

By: Andrea

Great article.

However, my motion takes up 50% of the cpu. What should i check?

By: Daniele

Hi,

could I ask you to help me understand in wich way could I get some parameters about Motion detection wih a Raspeberry PiCam and put this parameters about the Motion use of a camera for example, in a database ( SQLite or posgreSQl ), I mean where the parameters regarding the use of Motion are stored in a Debian Raspberry 4, and from where I could get these type of parameters ?

Thank you if you coul help me and suggest me.

Daniele

By: Hal

is it possible to send a notification when motion is detected? email or cell phone would be nice, but I don't see how to do that from reading the manuals. thanks