Raspberry Pi Camera and Motion out of the box – Sparrowcam

The idea is simple enough – 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 😉

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

cvlc v4l2:///dev/video0 --v4l2-width 640 --v4l2-height 480 v4l2-chroma h264 --sout  '#standard{access=http,mux=ts,dst=0.0.0.0:8082}'

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’s for another day…

width 1296 looks okay
Spadgers

Up to about mid 2014 it used to be a load of hurt to run Motion and the Raspberry Pi camera because there were no videoforlinux drivers for the camera. That way you don’t get a /dev/video0 for the Pi Camera and needed workarounds for Motion.

Now there is a driver which you’ll already have on a Raspbian install, and it’s easy to use. right out of the box.

Install the camera hardware, then

sudo modprobe bcm2835-v4l2

if it works you should get a

/dev/video0

that you didn’t have before. Then add the line to

/etc/modules

# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.
# Parameters can be specified after the module name.

snd-bcm2835

# camera with v4l2 driver
bcm2835-v4l2

on reboot should come good with /dev/video0 appearing

now install motion – (2018 update – if you’re on Stretch or later read this comment first)

sudo apt-get install motion

copy the /etc/motion/motion.conf to /etc/motion/motion.conf.ori

then edit /etc/motion/motion.conf
I changed

# Image width (pixels). Valid range: Camera dependent, default: 352
width 1024

# Image height (pixels). Valid range: Camera dependent, default: 288
height 768

# Maximum number of frames to be captured per second.
# Valid range: 2-100. Default: 100 (almost no limit).
framerate 25

[…]

# Maximum framerate for webcam streams (default: 1)
webcam_maxrate 10

# Restrict webcam connections to localhost only (default: on)
webcam_localhost off

# Limits the number of images per connection (default: 0 = unlimited)
# Number can be defined by multiplying actual webcam rate by desired number of $
# Actual webcam rate is the smallest of the numbers framerate and webcam_maxrate
webcam_limit 0

############################################################
# HTTP Based Control
############################################################

# TCP/IP port for the http server to listen on (default: 0 = disabled)
control_port 8080

# Restrict control connections to localhost only (default: on)
control_localhost off

and left everything else alone. Worked okay as an initial test.

crop from 1024×768 image – the problem every bird photographer has – the birds are too far away. It loses a bit of sharpness compared to the 1296 width version below

test with

sudo motion -n -c /etc/motion/motion.conf

seems to work on

http://pi.ip.address:8081/

then edit motion.conf to okay the daemon

sudo nano /etc/default/motion

set start_demon to on

reboot

should find the camera autostarted

Motion is a resource hog with these settings – this was on an old-skool Raspberry Pi Model A Rev 2

top - 09:25:36 up 4 min, 1 user, load average: 1.04, 0.66, 0.29
 Tasks: 68 total, 2 running, 66 sleeping, 0 stopped, 0 zombie
 %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
 KiB Mem: 380716 total, 87088 used, 293628 free, 9068 buffers
 KiB Swap: 102396 total, 0 used, 102396 free, 38712 cached
 
 PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
 2337 motion 20 0 60340 16m 6024 S 93.8 4.4 3:31.14 motion
 6 root 20 0 0 0 0 S 2.0 0.0 0:03.28 kworker/u2:0
 40 root 20 0 0 0 0 S 1.6 0.0 0:01.86 kworker/u2:2

Improving picture quality

If you read the camera specs resolutions below 1296 let the camera average over two pixels to improve noise levels. OTOH you don’t want the camera to resample oddball non-integer widths, like 1280 for instance. 1024 isn’t an integer submultiple of of the native 2592, so howsabout taking 1296, which isn’t that much bigger than 1280

width 1296 looks okay
width 1296 looks okay (1:1 screengrab crop from a 1296 x 480 setting)

1280 makes the resampling a mess

1280x480 looks much worse
1280×480 looks much worse

Motion has some nice features to act as a regular web camera, saving a picture every x seconds

Getting big enough to ID the birds

To get a higher res in motion using YU12 the pixel dimensions seem to want to be divisible by 16, so HD1920 x 1080 is no good (1080÷16=67.5) but using a width/height of 1920 x 1088 works. Sort of – you get an absolutely HUGE picture. If you can live with an oddball aspect ratio you can drop the height of the picture  – I used 1296 x 352 satisfactorily enough, but if you want a standard aspect ratio then big pictures will slow down the refresh rate.

Using the distorted aspect ratio trick I can use more of the pixels of the camera to get ‘closer’ 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 – it is tiny, and probably matched to the sensor because previous experiments with a M12 CCTV lens sucked. From that experience I’d also really prefer not to rig the camera outside, which makes for tedious powering and waterproofing issues.

There’s a whole cottage industry in Shenzen  making things like this

1502_telephonelenson ebay, which is like sticking a pair of massive binoculars in front of the lens. I’m not so sure about this – 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’t always so great.

The Eagle Eye 5x OpticZoom made for digiscoping from years ago made things bigger, but not 5x better. But for £5 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’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.

7 thoughts on “Raspberry Pi Camera and Motion out of the box – Sparrowcam”

  1. Thanks for the guide to get motion working quickly. I have a Pi 3 with the 5mp camera that I want to use for a security camera. Started with 2592×1944 and 5fps but it was maxing out the CPU 100%. It seemed to be working but I did not want to run the CPU that hard. Tried 1296×976 and 5fps. It worked better and the clarity was still good. (motion would not work with 1296×972…) With this resolution motion consumes 30-40% of CPU.

  2. Thanks for putting this post together – really helped me.

    On Raspian Stretch they changed a few of the parameter names:

    stream_localhost off
    stream_maxrate 2 or 10
    webcontrol_localhost off (and webcontrol_authentication username:password)

  3. The FPV drone folk use very similar cameras and change their lenses all the time. You might find usable lenses that fit at such webshops as Banggood, Wish, Geek, LightInTheBox, HobbyKing, EleFun, AdaFruit, Element14 and so on. Or just ask any drone pilot you might know of.

  4. Richard,
    A couple of drops of J&B 5 minute Weld will mount the lense of a different diameter.
    The big thing is to get the focus distance setup before the glue. I have used toothpicks and carbon rods to give me the spacing for some of the longer lenses. The Hyperfocal distance is very small so usually almost everything beyond 1-2 feet is in focus to infinity.
    tjcooper

    1. The problem I suffered first was axial skew, one side of the image was in focus but the other wasn’t. When I got that sorted using blu-tak to microadjust things I found using a CCTV M12 lens the centre was sharp but the periphery wasn’t or vice versa. I concluded they must be using an aspherical lens or perhaps my CCTV lens was too poor quality, and threw in the towel.

      Other people seem to have experienced a similar level of hurt You can also buy third-party M12 camera modules for the Pi, if I were trying this now I’d go that way.

Leave a Reply to jb Cancel reply

Your email address will not be published. Required fields are marked *