Python Motion Detection with OpenCV (Simple)

Whats goin’ on guys?

I am back with another Python video, this time we’re playing with computer vision:

Computer vision is an interdisciplinary field that deals with how computers can be made for gaining high-level understanding from digital images or videos. From the perspective of engineering, it seeks to automate tasks that the human visual system can do.” (Wikipedia)

Although it sounds complicated, we’re just getting started and we will keep it simple today! 😉

Environment Configuration

In order to follow along, you will need the following:

  1. A computer with Python installed (currently version 2.7.13);
  2. Install Numpy (required to install Python OpenCV library);
  3. Install Python OpenCV library for your operating system;
  4. A text-editor of your liking – I recommend: Sublime Text;
  5. Finally you will also need a webcam to test out the code;


Motion Detection

Now that we’re all set up, let’s think about this. What is motion detection?

“Motion detection is the process of detecting a change in the position of an object relative to its surroundings or a change in the surroundings relative to an object.” (Wikipedia)

Needless to say, for this project, we will be basing movement off of a steady camera, since capturing motion in a moving camera would be more of a advanced tutorial. So, we have a steady camera, whether it’s pointing outside of a window or being used for video chatting, we will write a program to save images from this camera only when there is motion in place.

This is particularly useful for many reasons. From computer security to keeping an eye on your pets, you could find hundreds of uses for this.

In computer security, for example, you could run this script stealthily on a compromised computer to record images of the person using this computer for further information gathering. Then again, you could also leave this script running in a camera in front of your house to see how many cars pass by your street…

The best thing about this is that for whatever project you might be doing, you will be saving time by having the camera record only moments in which there is motion – so that you don’t have to review many ‘blank’ pictures that were taken by collateral later.

Ok, back to motion detection. Basically what we’re doing is taking different “pictures” and comparing the difference between them. Before comparing this difference however, we also convert these pictures to grayscale (black and white) to make this easier. This way, after comparing these pictures, we will have a black background where all pixels are the same and the difference will be the white pixels. So let’s say we have about 10,000 white pixels on the screen, we can set a ‘threshold’ to classify that as motion. We have to account for trees and clouds moving on the screen so this ‘threshold’ value can vary from 10,000 to maybe 100,000. It depends on many factors like daytime or nighttime, resolution, how close the motion to detect is and so on…

This is the basic premise for how this script is going to work…

I actually do not specialize in this field so a lot of my knowledge came from the resources I found while researching this subject. You can find all the resources at the end of this blog post.


Video Capture

Now that we have a basic understanding of how this script works, we just have to find the right timing and record the pictures when we detect motion.

This is by far the easier part – given that I wrote this part of the code myself hahaha 😛

Ideally we only want to take one picture per second, otherwise we would end up with a ridiculous number of images, which we do not want.

So that is exactly what we will do. Note that you could also make this different based on your needs…


Video + Download:

[ Download ] Click here to download the python script.



As to make the process of finding a ‘threshold’ easier, I have made an update to the script which shows that value on-screen. By glancing at the camera feed for a few seconds it will give you an idea of what a proper value for the ‘threshold’ should be. This will likely require Numpy, so make sure to download and install it before running it!

[ Download ] Click here to download the updated python script.



Credits go out to these guys, they wrote basically 90% of the code – I just refined it for my own needs:

[1] “Motion detection using a webcam, Python, OpenCV and Differential Images

[2] “Getting Webcam Images with Python and OpenCV 2


Leave a Reply

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