CCTV Camera – Motion Detection

RaspberryPi

Getting Ready…

For this challenge you will need:

  • A Raspberry Pi with Python 2 installed,
  • A PiCamera

Follow the steps described on the following pages to make sure you have plugged the camera in your raspberry pi and that you have enabled it. ALso make sure you have installed the picamera library.

You will also need to setup the Python Imaging Library (PIL) which you can download from http://www.pythonware.com/products/pil/.

Alternatively you can type the following code in a terminal window:
sudo apt-get install python-imaging-tk

How It Works


Before we start


Most burglar alarm systems using motion sensors would use Passive Infrared Sensors. It is possible to buy one for the Raspberry Pi. However in this tutorial we will use an alternative approach without using an infrared sensor.

Note that if you need a tutorial based on using a Passive Infrared Sensor use this tutorial instead.

We are going to use a script that:

  1. Takes a picture and saves it in a buffer,
  2. Takes another picture,
  3. Compares the current picture and the previous picture (buffer) by counting the number of pixels which are different,
  4. If the number of pixels which are different between both pictures is greater than a given threshold, we will assume that the camera has detected some movements.
    • In this case, add the date and time to the current picture and store the picture on file.
  5. Replaces the picture stored in the buffer with the current picture
  6. Repeats all the steps above (from step 2).

To do so we will use the following script, from Claude Pageau: Source: https://github.com/pageauc/pi-motion-orig

To stop this script while it’s running press CTR + “C”. This will terminate the program.

Understanding the code


Answer the following questions to gain a better understanding of how this code actually works:

At which line (line number) does the main motion loop start?
What is the purpose of the variable called pixdiff?
What is the purpose of the variable called threshold?
What is the purpose of the variable called takePicture?
What is the purpose of the variables called forceCapture and forceCaptureTime?
Share Button
Posted in Computer Science, Python - Advanced, Python Challenges Tagged with:

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Blue Captcha Image
Refresh

*

Our Latest Book