#!/usr/bin/perl
#-----------------------------------------------------------------------------
# ___ _ _ ___ _ _ _
# ___ / __|__ _ _ __ (_) | |__ _ _ ___/ __| __ _ _(_)_ __| |_(_)_ _ __ _
# / -_) (__/ _` | ' \ _ | '_ \ || | / -_)__ \/ _| '_| | '_ \ _| | ' \/ _` |
# \___|\___\__,_|_|_|_(_) |_.__/\_, | \___|___/\__|_| |_| .__/\__|_|_||_\__, |
# |__/ |_| |___/
#-----------------------------------------------------------------------------
# $Id$
#
# eCam - a smarter webcam
# Copyright (C) 2000 Eric Richardson
#
# 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.
#
# For information, contact eScripting:
# info@escripting.com
# http://escripting.com
#
#-----------------------------------------------------------------------------
use DBI;
use vars qw(%db %cfg);
#----------
%cfg = (
db_host => "",
db_type => "mysql",
db => "eCam",
tbl => "cams",
user => "",
passwd => "",
host => 'mycomputer',
score => '15',
tmp_dir => "/tmp",
);
#----------
$db{h} = DBI->connect(
"DBI:$cfg{db_type}:$cfg{db}:$cfg{db_host}",$cfg{user},$cfg{passwd}
);
$db{insert} = $db{h}->prepare("
insert into $cfg{tbl}(id,host,timestamp,image) values(0,?,?,?)
");
#----------
while (1) {
#-- capture image --#
`streamer -d /dev/video -q -j 60 -f jpeg -i 1 -n ntsc -o /tmp/eCam-client.jpg`;
my $img;
open (IMG, "/tmp/eCam-client.jpg");
undef $/;
$img =
;
close IMG;
#-- compare and make upload decision --#
open (DJPEG, "| djpeg -ppm > $cfg{tmp_dir}/eCam.tmp.ppm");
print DJPEG $img;
close DJPEG;
if (-e"$cfg{tmp_dir}/eCam-client.ppm") {
# compare the image to determine if it has changed
open (ICMP, qq(/home/eric/scripts/eCam/icmp-new -i1 $cfg{tmp_dir}/eCam-client.ppm -i2 $cfg{tmp_dir}/eCam.tmp.ppm -f "%m"|));
my $score = ;
close ICMP;
if ($score >= $cfg{score}) {
warn "uploading with score of $score.\n" if ($cfg{debug});
&upload_image($img);
} else {
warn "too similar to upload\n" if ($cfg{debug});
}
} else {
&upload_image($img);
}
sleep 20;
}
#----------
sub upload_image {
my ($img) = @_;
if (-e"/tmp/eCam.tmp.ppm") {
`cp /tmp/eCam.tmp.ppm /tmp/eCam-client.ppm`;
}
# get a low priority write lock... If this bails we're going to jetison
# and forget the image
unless ($db{h}->do("LOCK TABLE cams LOW_PRIORITY WRITE")) {
warn "NNOOOOOOO!!!!! ".$db{h}->errstr."\n";
return;
}
# insert the image
$db{insert}->execute($cfg{host},time,$img);
$db{h}->do("UNLOCK TABLE");
}