Motion detecting web cam

9 November 2012 in Blog

Not an every day task but I recently needed to set up a motion detecting camera that would play a random sound file when motion was detected.  Having checked online for a few ideas I decided to try an Ubuntu (v12.04) system with Kenneth Lavrsen’s Motion.  I had a very old Creative NX Pro webcam that I thought would work and it took no time at all to get Ubuntu and Motion running with the camera.

My first problem was that webcams aren’t designed for outdoor use as the aperture is too wide.  I tried to work out if it would be possible to reduce the size of the aperture but in the end I went for a pair of shades.  Not just any shades … some Oakleys.  So a pair of Oakley lenses were taken from an old pair of Eye Jackets and blue-tacked together in front of the webcam lense to reduce the amount of light.

The next step was to get the sound files working, so I installed mpg123 and configured Motion to run a shell script at the appropriate time:

# Command to be executed when a motion frame is detected (default: none)
on_motion_detected /home/myuser/bin/play.sh

The shell script play.sh then used a simple loop and checked to see if it was the only copy executing at the time (to avoid duplicate sounds playing) and then played a random file from a specified folder:

#!/bin/bash
if [ `ps -ef | grep mpg123| grep -v grep| wc -l` = “0” ]; then

cd /home/myuser/Music/winston
set — *
length=$#
#   echo $# >> /home/myuser/bin/play.log

random_num=$(( $RANDOM % ($length + 1) ))
#    echo ${!random_num} >> /home/myuser/bin/play.log
mpg123 ${!random_num}

fi

Motion is a very nice little program as it not only takes timed snapshot images from the camera, but also creates images of each motion event and creates an avi movie of that event.

 

9 November 2012 Blog