{"id":248,"date":"2018-04-03T20:36:01","date_gmt":"2018-04-04T04:36:01","guid":{"rendered":"http:\/\/courses.haigarmen.com\/intd320\/?p=248"},"modified":"2018-04-03T20:45:24","modified_gmt":"2018-04-04T04:45:24","slug":"motion-detection-with-picam","status":"publish","type":"post","link":"https:\/\/courses.haigarmen.com\/intd320\/motion-detection-with-picam\/","title":{"rendered":"Motion Detection with PiCam"},"content":{"rendered":"<p data-section-id=\"0\">The Raspberry Pi camera module can take picture when a motion occurs, so you can use it for surveillance purposes. Here is how this can be done:<\/p>\n<p>1. First, make sure that your Raspberry Pi is running the latest firmware by running the command:<\/p>\n<pre><strong>sudo rpi-update<\/strong><\/pre>\n<p>2. If not already installed, install the\u00a0<strong>picamera<\/strong>\u00a0python module by using this command:<\/p>\n<pre><strong>sudo apt-get install python-picamera<\/strong><\/pre>\n<p>3. You will also need to install Python Imaging Library (PIL) to add image processing capabilities to your Python interpreter. This can be done by running this command:<\/p>\n<pre><strong>sudo apt-get install python-imaging-tk<\/strong><\/pre>\n<p>4. Now that the software required is installed you&#8217;ll need to create your Python script, copy the following code into a file called &#8220;motion.py&#8221;, either by using Nano or a text editor in the GUI:<\/p>\n<pre>import io\r\nimport os\r\nimport picamera\r\nimport time\r\nfrom datetime import datetime\r\nfrom PIL import Image\r\n\r\ncamera = picamera.PiCamera()\r\n\r\ndifference = 20\r\npixels = 100\r\n\r\nwidth = 1280\r\nheight = 960\r\n\r\ndef compare():\r\n\tcamera.resolution = (100, 75)\r\n\tstream = io.BytesIO()\r\n\tcamera.capture(stream, format = 'bmp')\r\n\tstream.seek(0)\r\n\tim = Image.open(stream)\r\n\tbuffer = im.load()\r\n\tstream.close()\r\n\treturn im, buffer\r\ndef newimage(width, height):\r\n\ttime = datetime.now()\r\n\tfilename = 'motion-%04d%02d%02d-%02d%02d%02d.jpg' % (time.year, time.month,time.day, time.hour,time.minute, time.second)\r\n\tcamera.resolution = (width, height)\r\n\tcamera.capture(filename)\r\n\tprint 'Captured %s' % filename\r\nimage1, buffer1 = compare()\r\n\r\ntimestamp = time.time()\r\n\r\nwhile (True):\r\n\timage2, buffer2 = compare()\r\n\r\n\tchangedpixels = 0\r\n\tfor x in xrange(0, 100):\r\n\t\tfor y in xrange(0, 75):\r\n\t\t\tpixdiff = abs(buffer1[x,y][1]- buffer2[x,y][1])\r\n\t\t\tif pixdiff &gt; difference:\t\t\r\n\t\t\t\tchangedpixels += 1\r\n\tif changedpixels &gt; pixels:\r\n\t\ttimestamp = time.time()\r\n\t\tnewimage(width, height)\r\n\timage1 = image2\r\n\tbuffer1 = buffer2\r\n<\/pre>\n<p>Be sure to get the right indentation of the code.<\/p>\n<p>5. The Python script will need to be executable, that&#8217;s done by the following command:<\/p>\n<pre><strong>chmod +x motion.py<\/strong><\/pre>\n<p>6. And finally, run the script with the command:<\/p>\n<pre><strong>python motion.py<\/strong><\/pre>\n<p>Your Raspberry Pi should now detect motion and take a picture every time it happens.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Raspberry Pi camera module can take picture when a motion occurs, so you can use it for surveillance purposes. Here is how this can be done: 1. First, make sure that your Raspberry Pi is running the latest firmware by running the command: sudo rpi-update 2. If not already installed, install the\u00a0picamera\u00a0python module by [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-248","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/courses.haigarmen.com\/intd320\/wp-json\/wp\/v2\/posts\/248","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/courses.haigarmen.com\/intd320\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/courses.haigarmen.com\/intd320\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/courses.haigarmen.com\/intd320\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/courses.haigarmen.com\/intd320\/wp-json\/wp\/v2\/comments?post=248"}],"version-history":[{"count":2,"href":"https:\/\/courses.haigarmen.com\/intd320\/wp-json\/wp\/v2\/posts\/248\/revisions"}],"predecessor-version":[{"id":250,"href":"https:\/\/courses.haigarmen.com\/intd320\/wp-json\/wp\/v2\/posts\/248\/revisions\/250"}],"wp:attachment":[{"href":"https:\/\/courses.haigarmen.com\/intd320\/wp-json\/wp\/v2\/media?parent=248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courses.haigarmen.com\/intd320\/wp-json\/wp\/v2\/categories?post=248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courses.haigarmen.com\/intd320\/wp-json\/wp\/v2\/tags?post=248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}