Sat Aug 8 15:28:31 PDT 2015

A Video Monitor for an Office from a MacBook

I was interested in video monitoring a room recently, and decided to use the built-in camera in a relatively elderly MacBook for this purpose. The procedure was extremely simple:

1. Display a video image from the camera on the screen using a suitable program

2. Run the script below, which saves a complete screen capture, compares it with the previous capture, and saves the image if there are significant differences

3. Turn off the display with F1

For additional impact, I also arranged for the MacBook to mail me updates - so if there is motion in the monitored area I receive an email notification. Because the entire screen is captured, each image is date stamped. (The email messages are also given the current date and time as a subject line). Here is the script:

#!/bin/sh
THRESHOLD=30
DELAY=5
count=0
while true
do
 /usr/sbin/screencapture -m -tjpg -x /Users/username/Desktop/pictures/new.jpg
 if [ ! -f old.jpg ]
 then
   cp new.jpg old.jpg
 fi
 /usr/local/bin/djpeg -ppm new.jpg > new.ppm
 /usr/local/bin/djpeg -ppm old.jpg > old.ppm
 ~/bin/pnmpsnr new.ppm old.ppm > yup 2>&1
 Y=`awk '/Y  color/{print int($5)}' yup`
 echo "Y is $Y"
 if [ $Y -lt $THRESHOLD ]
 then
   count=`expr $count + 1`
   name=`echo $count | awk '{printf "%06d.jpg", $1}'`
   echo "Saving: " $name
   cp new.jpg $name
   uuencode $name $name > t
   SUBJECT=`date`
   EMAIL="xyz@abchlk.com"
   /usr/bin/mail -s "$SUBJECT" "$EMAIL" < t
 else
   echo "No significant change in the images"
 fi
 cp new.jpg old.jpg
 sleep $DELAY
done

I drew inspiration for a script from David Bowler's page. Thank you David! There are a few things that you need to do to get this work correctly. Firstly, you need to be able compile djpeg and netpbm. Apple make building open source software a massive pain (I wonder why, largest company in the world?!). Anyway, these things can be done fairly easily - once you've worked out why you need to download several gigabytes of the Xcode environment. Anyway, Xcode is needed, just for the gcc compiler. How Apple get away with making an open source compiler their own, and making it so difficult to install I don't know. But fortunately, they have paid off the right politicians, so no major anti-trust cases for Apple, just yet. djpeg is needed to turn the jpg screen capture into a ppm file. Just grab the source from here, and configure and make it using your newly acquired gcc compiler.

netpbms is needed for the utility which compares two ppm files, called pnmpsnr. I grabbed the source, and built it using make and gcc as usual. The build was not entirely error free (!) but pnmpsnr was built just fine. This is just a program that reads and processes ascii files, so it isn't exactly the most demanding software engineering activity.


Posted by ZFS | Permanent link | File under: bash