How to set up a motion detection webcam in Ubuntu
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
Back in the Motion configuration, scroll down to the 'Image Processing' category, then find both the width and height variables.
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.
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.
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.
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.
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.
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'.
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.
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'.
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
You should read it
- What's New in Ubuntu 21.10?
- Why is Ubuntu LTS preferred over regular distros?
- What's new in Ubuntu 18.04 LTS just released?
- How to prolong the life of Ubuntu 18.04 installation with Ubuntu Pro
- What is the difference between Ubuntu Desktop and Ubuntu Server?
- Ubuntu 21.04 users need to update the system ASAP
- 5 things to do after upgrading to Ubuntu 22.04 LTS
- 5 Reasons Ubuntu Cinnamon Is Better Than Other Ubuntu Versions
- 7 reasons to upgrade to Ubuntu 23.04
- Ubuntu 19.10: Roadmap for release & planned features
- Tips and tricks after installing Ubuntu
- What is Ubuntu? Why should you use Ubuntu to replace Windows?
Maybe you are interested
iPhone ringtones are attractive because they create 'indescribable' emotions
Apple reveals 'Vehicle Motion Cues' feature to help prevent motion sickness
Apple is about to launch a feature to help users reduce motion sickness
Why do you get motion sickness and how to get rid of motion sickness with medicine, raw ginger, and mint
This new NVIDIA technology can help reduce motion blur in games, but you must have the right monitor
How to develop React apps that analyze emotions using OpenAI API