Intrusion Detection using in-built Webcam
Tags: webcam, motion, intrusion, ubuntu
Published: 2023-01-06 18:13:40

## 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](https://motion-project.github.io) for motion detection. 

## Installation

To install Motion on Debian based systems, use the following command.
```bash
$ 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](https://motion-project.github.io/motion_build.html).

## Configuring Motion

Copy the root configuration file to your home folder.
```bash
$ mkdir ~/.motion
$ sudo cp /etc/motion/motion.conf ~/.motion/motion.conf
```

Open the configuration file for editing.
```bash
$ nano ~/.motion/motion.conf
```

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

`target_dir` specifies the directory location where images and/or videos will be saved to.
```bash
target_dir  /home/user/survellience
```

`log_file` specifies the file location where the log file will be stored.
```bash
log_file /home/user/survellience/log/motion/motion.log
```

`width` and `height` specifies the width and height of images.
```bash
width: 640
height: 480
```

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

`picture_output` specifies whether to save pictures of the event.
```bash
picture_output: on
```

`movie_output` specifies whether to save a movie clip of the event.
```bash
movie_output: on
```

`webcontrol_port` specifies the localhost port number where the web portal will be shown.
```bash
webcontrol_port 8080
```
## Running motion detection
To run the program, use the following command. The `-c` option defines the path to the configuration file.
```bash
$ motion -c ~/.motion/motion.conf
```

You can view the web portal on [http://localhost:8080/](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](https://orangefreesounds.com/burglar-alarm-sound/) 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.

```bash
$ sudo apt-get update
$ sudo apt-get install sox
$ sudo apt-get install libsox-fmt-all
```

Create a bash script file in path `~/.motion`.
```bash
$ touch ~/.motion/event_start_alarm.sh
```

Then add the following to the contents of the file.
```bash
#!/bin/bash
play /home/user/.motion/alarm.mp3
```

Then make the file executable using `chmod`.
```bash
$ 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.
```bash
$ nano ~/.motion/motion.conf
```
```bash
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`.
```bash
$ 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.
```bash
$ 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!