My wife and kids enjoy bird watching. In our dining room, we attached a bird feeder to a window in the room. My wife asked if I could hack together a way to snap pictures of birds that visit the bird feeder. After doing some good searches, I realized that SimpleCV has some easy capabilities enabling you to create a motion detection system with Python. SimpleCV has become one of my favorite open source computer vision tools that you program using python. In general, computer vision is the branch of computer science that deals with understanding images and video. In this post, I’ll try to outline the major ideas from this script by the folks from SimpleCV. I made a few edits to the script to save video frames with motion to disk. To learn more about getting started with python programming, check out this blog post.
In the world of computers, a computer image exists as a grid of numbers. Each number represents a color. A pixel is a cell in this grid of numbers at a particular (x,y) position. SimpleCV enables you to capture an image from your web camera using the following code.
from SimpleCV import *
cam = Camera()
current = cam.getImage()
Let’s say we capture two images taken within a 1/2 second of each other.
previous = cam.getImage() #grab a frame
time.sleep(0.5) #wait for half a second
current = cam.getImage() #grab another frame
diff = current - previous
SimpleCV defines an image subtraction operation so that you can find the differences between two images. If the current and previous images are exactly the same, SimpleCV will compute a black image. (i.e. a grid of zeros) If current and previous images have substantial differences, some of the cells in the diff image will have positive values.
At this point, we compute a ‘mean’ factor using all the pixel values from the diff image. If the mean value is higher than a particular threshold, we know that a motion event occurred. We capture an image and store the image to a file.
You can review the complete code solution below.
The following web page from SimpleCV outlines other applications of image math.
http://tutorial.simplecv.org/en/latest/examples/image-math.html?highlight=motion
I think the image motion blur and green screen tutorials look fun too.
To install SimpleCV on a Raspberry Pi, check out the following link:
http://simplecv.readthedocs.io/en/latest/HOWTO-Install%20on%20RaspberryPi.html
from SimpleCV import *
cam = Camera()
threshold = 5.0 # if mean exceeds this amount do something
i = 0
disp = SimpleCV.Display((1024, 768))
while True:
previous = cam.getImage() #grab a frame
time.sleep(0.5) #wait for half a second
current = cam.getImage() #grab another frame
diff = current - previous
matrix = diff.getNumpy()
mean = matrix.mean()
current.save(disp)
if mean >= threshold:
print "Motion Detected " + str(i)
# capture the image. Display it. Save the image as a JPEG.
img = cam.getImage()
img.save('%.06d.jpg' % i)
# change the filename counter variable.
i += 1
Interested in learning more about SimpleCV? Check out the following PyCon conference video
Abstract: Katherine Scott: This talk is a brief summary of Computer Vision tutorial we proposed for PyCon. In this talk we will discuss what computer vision is, why it’s useful, what tools exist in the Python ecosystem, and how to apply it to your project.