How to Set Up Motion Detection Webcam in Ubuntu

Spread the love

Motion is a useful tool that can turn your regular webcam to a surveillance camera. It does this by monitoring the signal from your webcam and taking screenshots and videos when it detects movement. Here, we show you how to install Motion and set up a motion detection webcam in Ubuntu Linux.

Content

Installing the Motion Server

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

sudo apt install motion v4l-utils

Note: While we are dealing with Ubuntu in this article, Motion will work for most Linux distros as well.

Confirm that your Motion server is working properly by opening the program’s help prompt:

motion -h

Configuring Motion for Your System

Before we start configuring Motion, we need to copy the configuration file to our Home folder so that we have a clean backup of the original. Open a terminal and copy the config file to your Home folder:

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

Open the original config using your favorite text editor:

sudo nano /etc/motion/motion.conf

One of the easiest tweaks that you can do for your Motion server is changing the quality of its image and video output. By default, the server sets its resolution at 640×480 which can be too small for modern webcams.

To change this, run the following command on a new terminal session to check the available resolutions for your camera:

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

Go back to your Motion config, scroll down to the “Image Processing” category, then look for both the width and height variables.

Provide the appropriate values for the resolution that you want. In my case, I want to run Motion in 1280×720, so I type “1280” as my width and “720” as my height.

Set the framerate value to the framerate that you want to capture your videos in. By default, this is set to “15” but you can lower it to save up on disk space.

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 picture and video from your webcam.

Changing the Default Save Location

Another tweak that you can do for your Motion server is changing where it saves its data. This can be useful if you want to export your captured videos and images to either a mounted remote directory or a cloud storage service like Dropbox.

To start, either navigate to or create the directory that you want to save your data on:

sudo mkdir /motion/data

Set the ownership of your directory to “motion:adm”:

sudo chown -R motion:adm /motion/data

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

sudo chmod -R 755 /motion/data

Go back to your Motion config, look for the target_dir variable, then set its value to the full path of your directory.

Tweaking the Detection Settings in Motion

Motion works by looking at the difference between the current video feed and the one just moments before it. If the program finds enough difference between the two, it classifies that as “movement” and records a video of the event.

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

To tone this down, go to the “Motion Detection” category, then raise the value of the threshold variable. I find that 5500 pixels is enough to prevent Motion from recording unnecessary small movements from the webcam feed.

You can also add the threshold_maximum variable to set a cap on the difference that Motion considers as “movement.” This is useful if your webcam looks at a busy area and you only want it to run when there’s less activity.

If you’re unsure at the threshold and threshold_maximum values for your webcam, you can let Motion adjust it for you. For that, add the variable threshold_tune and set it to “on.”

Lastly, you can configure Motion to also record the moments before and after it detects movement. To do that, go to the pre_capture and post_capture variables and set their values to the amount of additional frames that you want to save.

Enabling the Motion Web Interface

The Motion server comes with a web interface that allows you to monitor your webcam remotely. To enable this, go to the “Webcontrol” section of the config, 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.”

Note: Motion doesn’t have any authentication checks for its web interface. As such, anyone who knows your Motion server’s address can look at your webcam and control the service.

Managing the Saved Captures Remotely

As I mentioned earlier, the best way is to save the images into your Dropbox folder so you can access it anywhere you want. However, if you prefer to have the images uploaded to your own file server (via FTP), you can use the command wput to upload the images:

sudo apt install wput

Open your Motion server’s config, 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

Starting and Testing the Motion Server

Make sure to save your Motion configuration file, then run the following command to start the server:

sudo systemctl enable --now motion.service

Confirm that the server is working properly by opening a web browser and navigating to your machine’s IP address followed by “:8080.”

This will bring up a webpage showing the current video feed from your webcam along with a menu containing additional actions for your server. For instance, you can click Action -> Snapshot to capture a still image from your webcam.

Test whether Motion detects and records movement from your webcam by passing an object in front of your camera and checking the contents of your target_dir directory.

There’s also a live stream that you can open from either your media player or web browser. To access this, navigate to your machine’s IP address followed by “:8081.”

Turning your regular webcam to a motion detection system is just one of the things that you can do with cameras in Linux. Learn more about some of the best webcam apps for Linux that you can install and try out today.

Image credit: Bermix Studio via Unsplash. All alterations and screenshots by Ramces Red.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Sign up for all newsletters.
By signing up, you agree to our Privacy Policy and European users agree to the data transfer policy. We will not share your data and you can unsubscribe at any time. Subscribe


Ramces Red
Staff Writer

Ramces is a technology writer that lived with computers all his life. A prolific reader and a student of Anthropology, he is an eccentric character that writes articles about Linux and anything *nix.

Leave a comment