Intrusion Detection using in-built Webcam

Intrusion Detection using in-built Webcam
[This article was originally published in Dev Community.]

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

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

Open the configuration file for editing.

$ nano ~/.motion/motion.conf

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

daemon: off

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

target_dir  /home/user/survellience

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

log_file /home/user/survellience/log/motion/motion.log

width and height specifies the width and height of images.

width: 640
height: 480

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

threshold: 500

picture_output specifies whether to save pictures of the event.

picture_output: on

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

movie_output: on

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

webcontrol_port 8080

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

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

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

$ touch ~/.motion/event_start_alarm.sh

Then add the following to the contents of the file.

#!/bin/bash
play /home/user/.motion/alarm.mp3

Then make the file executable using chmod.

$ chmod u+x ~/.motion/event_start_alarm.sh

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
on_event_start /home/user/.motion/event_start_alarm.sh

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 

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 

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

Happy Hacking!