{"id":2317,"date":"2018-03-03T12:59:25","date_gmt":"2018-03-03T12:59:25","guid":{"rendered":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=2317"},"modified":"2018-03-03T17:28:34","modified_gmt":"2018-03-03T17:28:34","slug":"detecting-motion-using-python-simplecv-and-a-raspberry-pi","status":"publish","type":"post","link":"https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/detecting-motion-using-python-simplecv-and-a-raspberry-pi\/","title":{"rendered":"Detecting Motion using Python, SimpleCV and a Raspberry Pi"},"content":{"rendered":"\n<!-- Facebook Like Button v1.9.6 BEGIN [http:\/\/blog.bottomlessinc.com] -->\n<iframe src=\"http:\/\/www.facebook.com\/plugins\/like.php?href=https%3A%2F%2Finspiredtoeducate.net%2Finspiredtoeducate%2Fdetecting-motion-using-python-simplecv-and-a-raspberry-pi%2F&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=like&amp;colorscheme=light\" scrolling=\"no\" frameborder=\"0\" allowTransparency=\"true\" style=\"border:none; overflow:hidden; width:450px; height: 30px; align: left; margin: 2px 0px 2px 0px\"><\/iframe>\n<!-- Facebook Like Button END -->\n<p><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2016\/08\/simpleCV.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-2096\" src=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2016\/08\/simpleCV.png\" alt=\"Simple CV\" width=\"743\" height=\"484\" srcset=\"https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2016\/08\/simpleCV.png 743w, https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2016\/08\/simpleCV-300x195.png 300w\" sizes=\"(max-width: 743px) 100vw, 743px\" \/><\/a><\/p>\n<p>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&#8217;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 <a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/5-fun-ways-to-learn-python\/\">this blog post<\/a>.<\/p>\n<p>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.<\/p>\n<p><code><br \/>\nfrom SimpleCV import *<br \/>\ncam = Camera()<br \/>\ncurrent = cam.getImage()<br \/>\n<\/code><\/p>\n<p>Let&#8217;s say we capture two images taken within a 1\/2 second of each other.<\/p>\n<p><code><br \/>\nprevious = cam.getImage() #grab a frame<br \/>\ntime.sleep(0.5) #wait for half a second<br \/>\ncurrent = cam.getImage() #grab another frame<br \/>\ndiff = current - previous<br \/>\n<\/code><\/p>\n<p>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.<\/p>\n<p>At this point, we compute a &#8216;mean&#8217; 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.<\/p>\n<p>You can review the complete code solution below.<\/p>\n<p>The following web page from SimpleCV outlines other applications of image math.<\/p>\n<p><a href=\"http:\/\/tutorial.simplecv.org\/en\/latest\/examples\/image-math.html?highlight=motion\" rel=\"noopener\" target=\"_blank\">http:\/\/tutorial.simplecv.org\/en\/latest\/examples\/image-math.html?highlight=motion<\/a><\/p>\n<p>I think the image motion blur and green screen tutorials look fun too.<\/p>\n<p>To install SimpleCV on a Raspberry Pi, check out the following link:<br \/>\n<a href=\"http:\/\/simplecv.readthedocs.io\/en\/latest\/HOWTO-Install%20on%20RaspberryPi.html\" rel=\"noopener\" target=\"_blank\">http:\/\/simplecv.readthedocs.io\/en\/latest\/HOWTO-Install%20on%20RaspberryPi.html<\/a><\/p>\n<p><code><br \/>\nfrom SimpleCV import *<\/p>\n<p>cam = Camera()<br \/>\nthreshold = 5.0 # if mean exceeds this amount do something<br \/>\ni = 0<br \/>\ndisp = SimpleCV.Display((1024, 768))<\/p>\n<p>while True:<br \/>\n\tprevious = cam.getImage() #grab a frame<br \/>\n\ttime.sleep(0.5) #wait for half a second<br \/>\n\tcurrent = cam.getImage() #grab another frame<br \/>\n\tdiff = current - previous<br \/>\n\tmatrix = diff.getNumpy()<br \/>\n\tmean = matrix.mean()<\/p>\n<p>\tcurrent.save(disp)<\/p>\n<p>\tif mean >= threshold:<br \/>\n\t\tprint \"Motion Detected \" + str(i)<\/p>\n<p>\t\t# capture the image. Display it. Save the image as a JPEG.<br \/>\n\t\timg = cam.getImage()<br \/>\n\t\timg.save('%.06d.jpg' % i)<\/p>\n<p>\t\t# change the filename counter variable.<br \/>\n\t\ti += 1<br \/>\n<\/code><\/p>\n<p>Interested in learning more about SimpleCV?  Check out the following PyCon conference video<br \/>\n<iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/UZSm7Q2bZoc\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen><\/iframe><\/p>\n<p>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&#8217;s useful, what tools exist in the Python ecosystem, and how to apply it to your project.<\/p>\n\n<!-- Facebook Like Button v1.9.6 BEGIN [http:\/\/blog.bottomlessinc.com] -->\n<iframe src=\"http:\/\/www.facebook.com\/plugins\/like.php?href=https%3A%2F%2Finspiredtoeducate.net%2Finspiredtoeducate%2Fdetecting-motion-using-python-simplecv-and-a-raspberry-pi%2F&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=like&amp;colorscheme=light\" scrolling=\"no\" frameborder=\"0\" allowTransparency=\"true\" style=\"border:none; overflow:hidden; width:450px; height: 30px; align: left; margin: 2px 0px 2px 0px\"><\/iframe>\n<!-- Facebook Like Button END -->\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16,23,11],"tags":[],"_links":{"self":[{"href":"https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/posts\/2317"}],"collection":[{"href":"https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/comments?post=2317"}],"version-history":[{"count":5,"href":"https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/posts\/2317\/revisions"}],"predecessor-version":[{"id":2322,"href":"https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/posts\/2317\/revisions\/2322"}],"wp:attachment":[{"href":"https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/media?parent=2317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/categories?post=2317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/tags?post=2317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}