DEV Community

Cover image for Intrusion Detection using in-built Webcam
Peeyush Man Singh
Peeyush Man Singh

Posted on

Intrusion Detection using in-built Webcam

Introduction

This post is about building an intrusion detection system on Ubuntu using the laptop's in-built camera. It can be used to detect intrusion while you are out of your room or when someone tries to access your computer while it is locked. Also, no additional hardware is required for this setup.

We will be using Motion for motion detection.

Installation

To install Motion on Debian based systems, use the following command.

$ sudo apt-get update
$ sudo apt-get install motion
Enter fullscreen mode Exit fullscreen mode

Optionally, it can also be downloaded via Ubuntu Software Center, and it can be installed on other distros or operating systems by following the installation guide.

Configuring Motion

Copy the root configuration file to your home folder.

$ mkdir ~/.motion
$ sudo cp /etc/motion/motion.conf ~/.motion/motion.conf
Enter fullscreen mode Exit fullscreen mode

Open the configuration file for editing.

$ nano ~/.motion/motion.conf
Enter fullscreen mode Exit fullscreen mode

The daemon option can be changed to on if you want the process to be run in the background.

daemon: off
Enter fullscreen mode Exit fullscreen mode

target_dir specifies the directory location where images and/or videos will be saved to.

target_dir  /home/user/survellience
Enter fullscreen mode Exit fullscreen mode

log_file specifies the file location where the log file will be stored.

log_file /home/user/survellience/log/motion/motion.log
Enter fullscreen mode Exit fullscreen mode

width and height specifies the width and height of images.

width: 640
height: 480
Enter fullscreen mode Exit fullscreen mode

threshold specifies the minimum number of pixels that needs to be changed between frames to trigger an event.

threshold: 500
Enter fullscreen mode Exit fullscreen mode

picture_output specifies whether to save pictures of the event.

picture_output: on
Enter fullscreen mode Exit fullscreen mode

movie_output specifies whether to save a movie clip of the event.

movie_output: on
Enter fullscreen mode Exit fullscreen mode

webcontrol_port specifies the localhost port number where the web portal will be shown.

webcontrol_port 8080
Enter fullscreen mode Exit fullscreen mode

Running motion detection

To run the program, use the following command. The -c option defines the path to the configuration file.

$ motion -c ~/.motion/motion.conf
Enter fullscreen mode Exit fullscreen mode

You can view the web portal on http://localhost:8080/. Also notice image and/or movie files being created in the target_folder when it detects motion.

Sounding the Alarm

To sound an alarm, you will need a mp3 file. Any mp3 file should work. Here is a free alarm mp3 file to download.

To play a mp3 file from the terminal, install the Sox command line utility. libsox-fmt-all is suggested to be installed manually after installing Sox.

$ sudo apt-get update
$ sudo apt-get install sox
$ sudo apt-get install libsox-fmt-all
Enter fullscreen mode Exit fullscreen mode

Create a bash script file in path ~/.motion.

$ touch ~/.motion/event_start_alarm.sh
Enter fullscreen mode Exit fullscreen mode

Then add the following to the contents of the file.

#!/bin/bash
play /home/user/.motion/alarm.mp3
Enter fullscreen mode Exit fullscreen mode

Then make the file executable using chmod.

$ chmod u+x ~/.motion/event_start_alarm.sh
Enter fullscreen mode Exit fullscreen mode

Now open the Motion config file and add the path to this script to the on_event_start option. Note that this option is by default disabled using comment. Make sure to uncomment the line and add the path to the script file.

$ nano ~/.motion/motion.conf
Enter fullscreen mode Exit fullscreen mode
on_event_start /home/user/.motion/event_start_alarm.sh
Enter fullscreen mode Exit fullscreen mode

Now start the program as before and when it detects a motion, it should sound the alarm, take pictures and videos and save them to the target_folder.

$ motion -c ~/.motion/motion.conf 
Enter fullscreen mode Exit fullscreen mode

Optionally, the target_folder could also be a network file location, so that you can view what motion the program detected when you are out of home.

You can use the following command if you want Motion to set off after a fixed period of time. For example, if you run the command and go away, it will sound immediately as it detected the motion as you moved away. To avoid this, you can let it start once you leave your workstation, say after 5 minutes of running the command.

$ sleep 5m; motion -c ~/.motion/motion.conf 
Enter fullscreen mode Exit fullscreen mode

Motion also continues to work even while your computer is locked, so make sure to lock your computer before leaving.

Happy Hacking!

Top comments (0)