Commit eb50c38a authored by Neil Gershenfeld's avatar Neil Gershenfeld

switch to compare

parent e02bfa5f
......@@ -8,6 +8,7 @@
// to run: node cams.js
// uses fswebcam for screen capture
// uses convert to make thumbnails
// uses compare to measure activity
// can use Ubuntu MATE on Raspberry Pi 3
// can use uvcdynctrl -f to list resolutions
// can use guvcview to adjust webcam
......@@ -19,7 +20,7 @@ var image_width = 1920
var image_height = 1080
var thumbnail_size = 100
var compression = 50
var activity_threshold = 250
var difference_threshold = .1
var millisecond_image_delay = 15000
var days_to_save = 150
//
......@@ -29,10 +30,6 @@ const fs = require('fs')
const exec = require('child_process').exec
const execSync = require('child_process').execSync
//
// initialization
//
var oldsize = 0
//
// image update function
//
function update() {
......@@ -66,32 +63,36 @@ function update() {
execSync('rm -f images/'+prefix+'*')
execSync('rm -f thumbnails/'+prefix+'*')
//
// check if image size has changed beyond threshold
// check if image has changed beyond threshold
//
var stats = fs.statSync('image.jpg')
var newsize = stats.size
console.log('old image size: '+oldsize+' new image size: '+newsize)
if (Math.abs(newsize-oldsize) > activity_threshold) {
try {
execSync('compare -metric RMSE image.jpg oldimage.jpg null: 2>&1')
}
catch(err) {
var ret = String(err.stdout).split(' ')
var diff = eval(ret[1])
console.log('image difference: '+diff)
}
if (diff > difference_threshold) {
//
// size has changed, keep image
//
console.log('activity detected, save image')
execSync('convert image.jpg -resize '+thumbnail_size+'x'+thumbnail_size+' thumbnails/'+name)
execSync('mv image.jpg images/'+name)
execSync('cp image.jpg images/'+name)
//
// rebuild day's index
//
console.log('reindex day')
execSync("echo '<html><body>' > index/newindex.html")
execSync("echo '<script src=../image.js></script>' > index/newindex.html")
//execSync('ls images | awk \'/^'+year+'-'+month+'-'+day+'/ { print "<img src=../thumbnails/" $1 "id=\"" $1 "\" width=\"10%\"" "onmousedown=\"image(\'" $1 "\')\">" }\' >> index/newindex.html')
execSync('ls images | awk \'/^'+year+'-'+month+'-'+day+'/ { print "<img src=\\"../thumbnails/" $1 "\\" id=\\"" $1 "\\" style=\\"width:10%;height:auto;cursor:pointer;\\" onmousedown=image(\\"" $1 "\\")>" }\' >> index/newindex.html')
execSync('ls images | awk \'/^'+year+'-'+month+'-'+day+'/ { print "<img src=\\"../thumbnails/" $1 "\\" id=\\"" $1 "\\" style=\\"width:10%;height:auto;cursor:pointer;\\" onmousedown=display(\\"" $1 "\\")>" }\' >> index/newindex.html')
execSync("mv index/newindex.html index/"+month+"-"+day+".html")
//
// delay before next image
//
console.log('delay before next image')
oldsize = newsize
execSync('mv image.jpg oldimage.jpg')
setTimeout(update,millisecond_image_delay)
}
else {
......@@ -102,12 +103,16 @@ function update() {
//
// no delay before next image
//
oldsize = newsize
execSync('mv image.jpg oldimage.jpg')
setTimeout(update,0)
}
}
//
// set update interval
// take initial image
//
execSync('fswebcam --jpeg '+compression+' --resolution '+image_width+'x'+image_height+' --save oldimage.jpg')
//
// start update
//
update()
var lastid
function image(id) {
function display(id) {
var img = document.getElementById(lastid)
if (img != null) {
img.src = "../thumbnails/"+id
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment