How to Make a Webcam Intruder Alarm with Mathematica
November 10, 2010 — Jon McLoone , Director, Technical Communication & Strategy
First, I am going to make use of an imminent new Mathematica command CurrentImage, which will import a real-time image from a video device. Let’s get some test images using the webcam on my laptop.
And again, but with a couple of seconds to hide from the camera:
ImageDifference gives us a pixel-by-pixel absolute difference between the two images. My camera isn’t very good, so there is a bit of noise. As a quick way to deal with that, I first blur both images slightly.
This image is black where there was no difference between the images. I don’t care how different they are, so I will apply a threshold. I have applied this to the maximum difference out of all the color channels, since we want a change in color to be as important as an overall change in brightness.
Now we clump together these differences by filling in gaps of 10 pixels or less.
This is all we need to detect an intruder. So let’s put those steps together into a function.
We can use this simplified scene-change image in two important ways.
Firstly, we can use it to highlight what has changed in an image. I will use it to darken the rest of the image by 75% and add a white line around the edge of the changed area.
Secondly, we can use it as a measure of the amount that has changed.
In this case there has been one scene change of over 37,000 pixels. Had the coffee cup been removed or another added, we would see a second smaller component listed as well.
Now I can create a function that tests and acts on the result of the scene changes. If the size of the biggest change is more than 1,000 pixels, then we will call that an intruder event, and we will use the SendMail command to send me email to tell me about it together with the highlighted changed image. I ignore the case of over 70,000 pixels, as I don’t want an alarm when someone turns off the office lights and the entire image changes.
Next I want to check the current scene against a time lagged store of what the image should look like. We want to update the store regularly, so that lasting changes such as an item being taken from the scene or placed there, or the sun setting, do not produce repeated alerts.
Finally, we need to call the function repeatedly. I could use an infinite loop, but that would lock my Mathematica completely. I might want to run overnight computations at the same time, or have other events happen periodically, so instead I program a scheduled interrupt (another new capability) that will suspend any running Mathematica program, run this command, and then return to the program.
Now let’s use it.
This gives me 60 seconds to leave the room and then runs the task every 15 seconds, keeping a 4 minute buffer of background images.
Later that night I get the following emails. (OK, not really!) Not bad for a couple dozen lines of code!