How to set up a motion detection webcam in Ubuntu

Motion is a useful tool that can turn a regular webcam into a surveillance camera. It does this by monitoring the signal from the webcam and taking photos and videos when motion is detected.

 The following article will show you how to install Motion and set up a motion detection webcam in Ubuntu Linux.

Install Motion Server

By default, Motion is included in the official Ubuntu repository. This means you can install this program through your apt package manager:

sudo apt install motion v4l-utils

Note : We mention Ubuntu in this article, but Motion will also work with most Linux distributions.

Confirm that Motion Server is working properly by opening the program's help prompt:

motion -h

Configure Motion for the system

Before starting to configure Motion, it is necessary to copy the configuration file to the Home folder to have a clean backup of the original. Open Terminal and copy the configuration file to the Home folder:

cp /etc/motion/motion.conf ~/motion.conf.backup

Open the root configuration with your favorite editor:

sudo nano /etc/motion/motion.conf

One of the easiest adjustments you can make to your Motion Server is to change the image and video output quality. By default, the server sets the resolution at 640×480, which may be too small for modern webcams.

To change this, run the following command in the new terminal to check the available resolutions for the camera:

sudo v4l2-ctl -d /dev/video0 --list-formats-ext

How to set up a motion detection webcam in Ubuntu Picture 1How to set up a motion detection webcam in Ubuntu Picture 1

Back in the Motion configuration, scroll down to the 'Image Processing' category, then find both the width and height variables.

How to set up a motion detection webcam in Ubuntu Picture 2How to set up a motion detection webcam in Ubuntu Picture 2

Provide appropriate values ​​for the desired resolution. In the example case, I want to run Motion at 1280×720, so the author entered '1280' as the width and '720' as the height.

How to set up a motion detection webcam in Ubuntu Picture 3How to set up a motion detection webcam in Ubuntu Picture 3

Set the framerate value to the frame rate at which you want to record video. By default, this value is set to '15' but you can lower this value to save disk space.

How to set up a motion detection webcam in Ubuntu Picture 4How to set up a motion detection webcam in Ubuntu Picture 4

Scroll down to the 'Picture output' category, then set the value of the picture_output variable to 'on'. This will allow Motion to capture both images and video from the webcam.

How to set up a motion detection webcam in Ubuntu Picture 5How to set up a motion detection webcam in Ubuntu Picture 5

Change default save location

Another adjustment you can make to Motion Server is to change where data is saved. This can be useful if you want to export your captured videos and images to a mounted remote folder or a cloud storage service like Dropbox.

To get started, navigate to or create the folder you want to save your data in:

sudo mkdir /motion/data

Set folder ownership to 'motion:adm':

sudo chown -R motion:adm /motion/data

Make sure that the entire path to the folder is readable by all users:

sudo chmod -R 755 /motion/data

Back in the Motion configuration, find the target_dir variable, then set its value to the full path in the directory.

How to set up a motion detection webcam in Ubuntu Picture 6How to set up a motion detection webcam in Ubuntu Picture 6

Fine-tune detection settings in Motion

Motion works by looking at the difference between the current video feed and the feed immediately before. If the program finds enough difference between the two, it classifies it as 'motion' and records a video of the event.

Currently, Motion assumes that a difference of 1500 pixels is enough to classify as 'motion'. While this makes it easy for most webcams to detect motion, it can be too sensitive for higher resolution cameras.

To alleviate this, go to the 'Motion Detection' category, then increase the value of the threshold variable. 5500 pixels is enough to prevent Motion from recording unnecessary small movements from the webcam feed.

How to set up a motion detection webcam in Ubuntu Picture 7How to set up a motion detection webcam in Ubuntu Picture 7

You can also add a threshold_maximum variable to set a limit on the amount of difference that Motion considers 'motion'. This is useful if the webcam is covering a crowded area and you only want it to run when there is less activity.

If you're not sure about the threshold and threshold_maximum values ​​for your webcam, you can let Motion adjust it for you. To do that, add the variable threshold_tune and set it to 'on'.

How to set up a motion detection webcam in Ubuntu Picture 8How to set up a motion detection webcam in Ubuntu Picture 8

Finally, you can configure Motion to record the moments before and after motion is detected. To do that, go to the pre_capture and post_capture variables and set their values ​​to the number of additional frames you want to save.

How to set up a motion detection webcam in Ubuntu Picture 9How to set up a motion detection webcam in Ubuntu Picture 9

Enable the Motion web interface

Motion server comes with a web interface that allows remote webcam monitoring. To enable this feature, go to the 'Webcontrol' section of the configuration, then set the value of the webcontrol_localhost variable to 'off'.

Scroll down to the 'Live stream' section, then set the value of stream_localhost to 'off'.

How to set up a motion detection webcam in Ubuntu Picture 10How to set up a motion detection webcam in Ubuntu Picture 10

Note : Motion does not have any validation checks for its web interface. Therefore, anyone who knows your Motion Server address can view the webcam and control the service.

Manage saved photos remotely

As mentioned earlier, it is best to save the image to a Dropbox folder so it can be accessed anywhere you want. However, if you want to upload images to your own file server (via FTP), you can use the wput command to upload images:

sudo apt install wput

Open the Motion Server configuration, then insert the following lines of code at the end of the file:

on_picture_save wput ftp://YOUR-USERNAME:YOUR-PASSWORD@YOUR-SERVER-ADDRESS %f on_movie_end wput ftp://YOUR-USERNAME:YOUR-PASSWORD@YOUR-SERVER-ADDRESS %f

How to set up a motion detection webcam in Ubuntu Picture 11How to set up a motion detection webcam in Ubuntu Picture 11

5 ★ | 1 Vote