# ImgSum.pm package ImgSum; use strict; use warnings; use Image::Magick; sub summary { my ($imgf, $res) = @_; my $img = new Image::Magick; $img->Read($imgf); my ($w, $h) = $img->Get(qw/rows columns/); my @sum; my $i = 0; for (my $y = 0; $y < $h; $y += $res) { for (my $x = 0; $x < $w; $x += $res) { my $px = $img->Get("pixel[$x,$y]"); my $rgb = [split/,/, $px]; $sum[$i++] = $rgb; } } return \@sum; } 1; #### #!/usr/bin/perl # sum.pl use strict; use warnings; use ImgSum; use Data::Dumper; use Storable; my ($img, $res, $out) = @ARGV; # $res is the sampling resolution my $sum = ImgSum::summary($img, $res); store($sum, $out); #### # SumDif.pm package SumDiff; use strict; use warnings; use Data::Dumper; sub mean { my $tt = 0; my $s = 0; while (defined($_ = shift)) { $tt += $_; $s++; } return $tt/$s; } sub diffmap { my ($s1, $s2) = @_; my @diff; foreach my $i(0..$#$s1) { my $c1 = $s1->[$i]; my $c2 = $s2->[$i]; my @d = map { abs($c2->[$_] - $c1->[$_]) } (0..$#$c1); $diff[$i] = mean(@d); } return \@diff; } sub diffavg { return mean(@{diffmap(@_)}); } 1; #### #!/usr/bin/perl # watch.pl use strict; use warnings; use ImgSum; use LWP::Simple; use SumDiff; use Storable; my @refs = map {retrieve($_)} ('sum-r0.dat', 'sum-r1.dat'); sub min { my $mni = 0; for my $i(0..$#_) { if ($_[$i] < $_[$mni]) { $mni = $i } } return $mni; } sub getclass { my $test = shift; my @diffs; for my $i(0..$#refs) { my $diff = SumDiff::diffavg($refs[$i], $test); $diffs[$i] = $diff; print "Difference to $i: $diff\n"; } return min(@diffs); } my $lc = 1; while (sleep 2) { my $stat = getstore('http://localhost:45005/webcam.jpg', 'current.jpg'); print "HTTP status: $stat\n"; my $sum = ImgSum::summary('current.jpg', 4); my $cl = getclass($sum); print "Likely class: $cl\n"; if ($lc != $cl) { system('xscreensaver-command ' . ($cl?'-deactivate':'-activate')); $lc = $cl; } } #### ( take a picture of the empty room, save it as ref0.jpg ) $ ./sum.pl ref0.jpg 4 sum-r0.dat ( take a picture of yourself, call it ref1.jpg ) $ ./sum.pl ref1.jpg 4 sum-r1.dat $ ./watch.pl