{"id":2130,"date":"2015-02-13T23:41:29","date_gmt":"2015-02-13T23:41:29","guid":{"rendered":"http:\/\/www.richardmudhar.com\/?p=2130"},"modified":"2018-01-06T19:18:53","modified_gmt":"2018-01-06T19:18:53","slug":"raspberry-pi-camera-and-motion-out-of-the-box-sparrowcam","status":"publish","type":"post","link":"https:\/\/www.richardmudhar.com\/blog\/2015\/02\/raspberry-pi-camera-and-motion-out-of-the-box-sparrowcam\/","title":{"rendered":"Raspberry Pi Camera and Motion out of the box &#8211; Sparrowcam"},"content":{"rendered":"<p>The idea is simple enough &#8211; a bird feeder camera on the network, using the Pi and associated camera. Using motion detection software I can pick out the birds. Of course I will also get the feeders swinging in the wind \ud83d\ude09<\/p>\n<p>Although this is about running motion I can use videolan instead to stream the video as a netcam and use motion on a second machine. Videolan streaming<\/p>\n<pre>cvlc v4l2:\/\/\/dev\/video0 --v4l2-width 640 --v4l2-height 480 v4l2-chroma h264 --sout\u00a0 '#standard{access=http,mux=ts,dst=0.0.0.0:8082}'<\/pre>\n<p>is nice on the Pi, because it seems the camera can do the h264 in some sort of hardware\/accelerated mode in the V4l driver. I can then watch the birds with realtime update rates on my LAN. That&#8217;s for another day&#8230;<\/p>\n<figure id=\"attachment_2131\" aria-describedby=\"caption-attachment-2131\" style=\"width: 480px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.richardmudhar.com\/blog\/wp-content\/uploads\/2015\/02\/1502_crop_1296_s.jpg\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"wp-image-2131 size-full\" src=\"https:\/\/i0.wp.com\/www.richardmudhar.com\/blog\/wp-content\/uploads\/2015\/02\/1502_crop_1296_s.jpg?resize=480%2C272\" alt=\"width 1296 looks okay\" width=\"480\" height=\"272\" \/><\/a><figcaption id=\"caption-attachment-2131\" class=\"wp-caption-text\">Spadgers<\/figcaption><\/figure>\n<p>Up to about mid 2014 it used to be a load of hurt to run <a href=\"http:\/\/www.lavrsen.dk\/foswiki\/bin\/view\/Motion\/WebHome\">Motion <\/a>and the Raspberry Pi camera because there were no videoforlinux drivers for the camera. That way you don&#8217;t get a \/dev\/video0 for the Pi Camera and needed <a href=\"http:\/\/www.raspberrypi.org\/forums\/viewtopic.php?t=44966\">workarounds for Motion<\/a>.<\/p>\n<p>Now there is a driver which you&#8217;ll already have on a Raspbian install, and it&#8217;s easy to use. right out of the box.<!--more--><\/p>\n<p>Install the camera hardware, then<\/p>\n<p><strong>sudo modprobe bcm2835-v4l2<\/strong><\/p>\n<p>if it works you should get a<\/p>\n<p><strong>\/dev\/video0<\/strong><\/p>\n<p>that you didn&#8217;t have before. Then add the line to<\/p>\n<p><strong> \/etc\/modules<\/strong><\/p>\n<pre># \/etc\/modules: kernel modules to load at boot time.\n#\n# This file contains the names of kernel modules that should be loaded\n# at boot time, one per line. Lines beginning with \"#\" are ignored.\n# Parameters can be specified after the module name.\n\nsnd-bcm2835\n\n<strong># camera with v4l2 driver<\/strong>\n<strong>bcm2835-v4l2<\/strong><\/pre>\n<p>on reboot should come good with <strong>\/dev\/video0<\/strong> appearing<\/p>\n<p>now install motion &#8211; (2018 update &#8211; if you&#8217;re on Stretch or later <a href=\"http:\/\/www.richardmudhar.com\/blog\/2015\/02\/raspberry-pi-camera-and-motion-out-of-the-box-sparrowcam\/#comment-4237\">read this comment first<\/a>)<\/p>\n<p><strong>sudo apt-get install motion<\/strong><\/p>\n<p>copy the <strong>\/etc\/motion\/motion.conf<\/strong> to <strong>\/etc\/motion\/motion.conf.ori<\/strong><\/p>\n<p>then edit <strong>\/etc\/motion\/motion.conf<\/strong><br \/>\nI changed<\/p>\n<pre># Image width (pixels). Valid range: Camera dependent, default: 352\n<strong>width 1024<\/strong>\n\n# Image height (pixels). Valid range: Camera dependent, default: 288\n<strong>height 768<\/strong>\n\n# Maximum number of frames to be captured per second.\n# Valid range: 2-100. Default: 100 (almost no limit).\n<strong>framerate 25<\/strong><\/pre>\n<p>[&#8230;]<\/p>\n<pre># Maximum framerate for webcam streams (default: 1)\n<strong>webcam_maxrate 10<\/strong>\n\n# Restrict webcam connections to localhost only (default: on)\n<strong>webcam_localhost off<\/strong>\n\n# Limits the number of images per connection (default: 0 = unlimited)\n# Number can be defined by multiplying actual webcam rate by desired number of $\n# Actual webcam rate is the smallest of the numbers framerate and webcam_maxrate\nwebcam_limit 0\n\n############################################################\n# HTTP Based Control\n############################################################\n\n# TCP\/IP port for the http server to listen on (default: 0 = disabled)\ncontrol_port 8080\n\n# Restrict control connections to localhost only (default: on)\n<strong>control_localhost off<\/strong><\/pre>\n<p>and left everything else alone. Worked okay as an initial test.<\/p>\n<figure id=\"attachment_2136\" aria-describedby=\"caption-attachment-2136\" style=\"width: 480px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.richardmudhar.com\/blog\/wp-content\/uploads\/2015\/02\/1502_1024_crop_s1.jpg\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"wp-image-2136 size-full\" src=\"https:\/\/i0.wp.com\/www.richardmudhar.com\/blog\/wp-content\/uploads\/2015\/02\/1502_1024_crop_s1.jpg?resize=480%2C272\" alt=\"\" width=\"480\" height=\"272\" \/><\/a><figcaption id=\"caption-attachment-2136\" class=\"wp-caption-text\">crop from 1024&#215;768 image &#8211; the problem every bird photographer has &#8211; the birds are too far away. It loses a bit of sharpness compared to the 1296 width version below<\/figcaption><\/figure>\n<p>test with<\/p>\n<p><strong>sudo motion -n -c \/etc\/motion\/motion.conf<\/strong><\/p>\n<p>seems to work on<\/p>\n<p>http:\/\/pi.ip.address:8081\/<\/p>\n<p>then edit motion.conf to okay the daemon<\/p>\n<p>sudo nano \/etc\/default\/motion<\/p>\n<p>set start_demon to on<\/p>\n<p>reboot<\/p>\n<p>should find the camera autostarted<\/p>\n<p>Motion is a resource hog with these settings &#8211; this was on an old-skool Raspberry Pi Model A Rev 2<\/p>\n<pre>top - 09:25:36 up 4 min, 1 user, load average: 1.04, 0.66, 0.29\n Tasks: 68 total, 2 running, 66 sleeping, 0 stopped, 0 zombie\n %Cpu(s): 91.9 us, 5.9 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 2.3 si, 0.0 st\n KiB Mem: 380716 total, 87088 used, 293628 free, 9068 buffers\n KiB Swap: 102396 total, 0 used, 102396 free, 38712 cached\n \n PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND\n 2337 motion 20 0 60340 16m 6024 S <strong>93.8<\/strong> 4.4 3:31.14 motion\n 6 root 20 0 0 0 0 S 2.0 0.0 0:03.28 kworker\/u2:0\n 40 root 20 0 0 0 0 S 1.6 0.0 0:01.86 kworker\/u2:2<\/pre>\n<h4>Improving picture quality<\/h4>\n<p>If you read the <a href=\"http:\/\/www.raspberrypi.org\/documentation\/raspbian\/applications\/camera.md\">camera specs<\/a> resolutions below 1296 let the camera average over two pixels to improve noise levels. OTOH you don&#8217;t want the camera to resample oddball non-integer widths, like 1280 for instance. 1024 isn&#8217;t an integer submultiple of of the native 2592, so howsabout taking 1296, which isn&#8217;t that much bigger than 1280<\/p>\n<figure id=\"attachment_2131\" aria-describedby=\"caption-attachment-2131\" style=\"width: 480px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.richardmudhar.com\/blog\/wp-content\/uploads\/2015\/02\/1502_crop_1296_s.jpg\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2131\" src=\"https:\/\/i0.wp.com\/www.richardmudhar.com\/blog\/wp-content\/uploads\/2015\/02\/1502_crop_1296_s.jpg?resize=480%2C272\" alt=\"width 1296 looks okay\" width=\"480\" height=\"272\" \/><\/a><figcaption id=\"caption-attachment-2131\" class=\"wp-caption-text\">width 1296 looks okay (1:1 screengrab crop from a 1296 x 480 setting)<\/figcaption><\/figure>\n<p>1280 makes the resampling a mess<\/p>\n<figure id=\"attachment_2132\" aria-describedby=\"caption-attachment-2132\" style=\"width: 480px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.richardmudhar.com\/blog\/wp-content\/uploads\/2015\/02\/1502_1280_crop_s.jpg\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2132\" src=\"https:\/\/i0.wp.com\/www.richardmudhar.com\/blog\/wp-content\/uploads\/2015\/02\/1502_1280_crop_s.jpg?resize=480%2C272\" alt=\"1280x480 looks much worse\" width=\"480\" height=\"272\" \/><\/a><figcaption id=\"caption-attachment-2132\" class=\"wp-caption-text\">1280&#215;480 looks much worse<\/figcaption><\/figure>\n<p>Motion has some nice features to act as a regular web camera, saving a picture every x seconds<\/p>\n<h4>Getting big enough to ID the birds<\/h4>\n<p>To get a higher res in motion using YU12 the pixel dimensions seem to want <a title=\"seems to be a motion limitation\" href=\"http:\/\/www.lavrsen.dk\/foswiki\/bin\/view\/Motion\/ConfigOptionHeight\" target=\"_blank\" rel=\"noopener\">to be divisible by 16<\/a>, so HD1920 x 1080 is no good (1080\u00f716=67.5) but using a width\/height of 1920 x 1088 works. Sort of &#8211; you get an absolutely HUGE picture. If you can live with an oddball aspect ratio you can drop the height of the picture\u00a0 &#8211; I used 1296 x 352 satisfactorily enough, but if you want a standard aspect ratio then big pictures will slow down the refresh rate.<\/p>\n<p>Using the distorted aspect ratio trick I can use more of the pixels of the camera to get &#8216;closer&#8217; to the birds, but in the end I will either have to move the camera closer to the birds or use some sort of optical aid. Changing the lens of the Pi camera is no fun at all &#8211; it is tiny, and probably matched to the sensor because <a title=\"a Raspberry Pi camera after a season outdoors in the British weather\" href=\"http:\/\/www.richardmudhar.com\/2014\/10\/a-raspberry-pi-after-season-outdoors-in-the-british-weather\/\">previous experiments<\/a> with a M12 CCTV lens sucked. From that experience I&#8217;d also really prefer not to rig the camera outside, which makes for tedious powering and waterproofing issues.<\/p>\n<p>There&#8217;s a whole cottage industry in Shenzen\u00a0 making things like this<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/www.richardmudhar.com\/blog\/wp-content\/uploads\/2015\/02\/1502_telephonelens.jpg\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2141\" src=\"https:\/\/i0.wp.com\/www.richardmudhar.com\/blog\/wp-content\/uploads\/2015\/02\/1502_telephonelens.jpg?resize=500%2C500\" alt=\"1502_telephonelens\" width=\"500\" height=\"500\" srcset=\"https:\/\/i0.wp.com\/www.richardmudhar.com\/blog\/wp-content\/uploads\/2015\/02\/1502_telephonelens.jpg?w=500&amp;ssl=1 500w, https:\/\/i0.wp.com\/www.richardmudhar.com\/blog\/wp-content\/uploads\/2015\/02\/1502_telephonelens.jpg?resize=150%2C150&amp;ssl=1 150w\" sizes=\"auto, (max-width: 500px) 85vw, 500px\" \/><\/a>on ebay, which is like sticking a pair of massive binoculars in front of the lens. I&#8217;m not so sure about this &#8211; it will make it hard to rig on a windowsill without falling over, and experience has taught me that high-power lenses stuck in front of an existing lens aren&#8217;t always so great.<\/p>\n<p>The Eagle Eye 5x OpticZoom made for digiscoping from years ago made things bigger, but not 5x better. But for \u00a35 a 2x tele mobile phone lens seems worth a go, and if I really need the extra reach I will kludge the OpticZoom to the Pi. The OpticZoom doesn&#8217;t fill the frame on the Pi camera, the image is badly vignetted to about half the sensor, which was always the bugbear with that lens. Some work needed on this.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The idea is simple enough &#8211; a bird feeder camera on the network, using the Pi and associated camera. Using motion detection software I can pick out the birds. Of course I will also get the feeders swinging in the wind \ud83d\ude09 Although this is about running motion I can use videolan instead to stream &hellip; <a href=\"https:\/\/www.richardmudhar.com\/blog\/2015\/02\/raspberry-pi-camera-and-motion-out-of-the-box-sparrowcam\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Raspberry Pi Camera and Motion out of the box &#8211; Sparrowcam&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[296,10],"tags":[],"class_list":["post-2130","post","type-post","status-publish","format-standard","hentry","category-birds","category-raspberry-pi"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5aOO7-ym","jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.richardmudhar.com\/blog\/wp-json\/wp\/v2\/posts\/2130","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.richardmudhar.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.richardmudhar.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.richardmudhar.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.richardmudhar.com\/blog\/wp-json\/wp\/v2\/comments?post=2130"}],"version-history":[{"count":16,"href":"https:\/\/www.richardmudhar.com\/blog\/wp-json\/wp\/v2\/posts\/2130\/revisions"}],"predecessor-version":[{"id":3352,"href":"https:\/\/www.richardmudhar.com\/blog\/wp-json\/wp\/v2\/posts\/2130\/revisions\/3352"}],"wp:attachment":[{"href":"https:\/\/www.richardmudhar.com\/blog\/wp-json\/wp\/v2\/media?parent=2130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.richardmudhar.com\/blog\/wp-json\/wp\/v2\/categories?post=2130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.richardmudhar.com\/blog\/wp-json\/wp\/v2\/tags?post=2130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}