#!/usr/bin/perl # # A motion sensor monitor for mon. Requires "pnmpsnr" from netpbm # distribution. You will need to modify this to obtain the image from # your image sensor. I use a D-Link DCS-900W, which allows me to simply # fetch the current image via HTTP. # # Jim Trocki # # Copyright 2007 Jim Trocki # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # sub getimage; sub debug; $SECS = 0; $TOLERANCE = 3; $COUNT = 2000; $lastY = undef; $Y = undef; $movement = 0; $prev_img = "a.pnm"; $cur_img = "b.pnm"; getimage ($prev_img); sleep $SECS; # # this will run until $COUNT images are sampled or until a change # in the image is identified # for ($i = 0; $i < $COUNT; $i++) { getimage ($cur_img); $output = `pnmpsnr $prev_img $cur_img 2>&1`; die if ($output !~ /Y\s+color\s+component:\s+(\d+(\.\d+)?)/ims); $Y = $1; $diff = abs ($Y - $lastY); debug "y=$Y diff=$diff"; if (defined $lastY && $diff > $TOLERANCE) { $movement++; last; } system ("mv $cur_img $prev_img"); $lastY = $Y; sleep $SECS; } # eventually give the mon server a status update if ($movement) { # mkdir ("motion-$s"); # system ("mv $cur_img $prev_img motion-$s"); my @t = localtime; my $f = sprintf ('motion-%04d%02d%02d-%02d%02d%02d.jpg', $t[5] + 1900, $t[4] + 1, @t[3,2,1,0]); system ("pnmtojpeg $cur_img > $f"); print "motion detected\n"; } else { print "no motion detected\n"; } exit ($movement); sub getimage { my $name = shift; debug "wgetting $name"; system ("wget -O- -q --http-user=root --http-passwd='roots' " . "http://cam:8080/IMAGE.JPG | " . "jpegtopnm > $name 2>/dev/null"); } sub debug { my $d = shift; print STDERR "$d\n"; }