diff --git a/cams1.js b/cams1.js new file mode 100644 index 0000000000000000000000000000000000000000..1bab4bce1db13c4e4c3982809d438a051f0078a7 --- /dev/null +++ b/cams1.js @@ -0,0 +1,131 @@ +// +// cams.js +// shopcam activity monitor +// Neil Gershenfeld +// 12/11/17 +// +// settings +// +var image_width = 1600 +var image_height = 1200 +var thumbnail_size = 200 +var compression = 50 +var difference_threshold = .05 +var millisecond_image_delay = 15000 +var days_to_save = 150 +var flip = "--flip h,v" +var settings= '\ +--set "Focus, Auto"=False \ +--set "Focus (absolute)"=0% \ +--set "White Balance Temperature, Auto"=False \ +--set "White Balance Temperature"=50% \ +--set "Exposure, Auto"="Manual Mode" \ +--set "Exposure, Auto Priority"=False \ +--set "Exposure (Absolute)"=15% \ +--set Brightness=50% \ +--set Contrast=50% \ +--set Saturation=50% \ +--set Gain=0% \ +--set Sharpness=100%' +// +// requires +// +const fs = require('fs') +const exec = require('child_process').exec +const execSync = require('child_process').execSync +// +// image update function +// +function update() { + // + // name image with time + // + var date = new Date() + var year = date.getFullYear() + var month = ('0'+(1+parseInt(date.getMonth()))).slice(-2) + var day = ('0'+date.getDate()).slice(-2) + var hour = ('0'+date.getHours()).slice(-2) + var minute = ('0'+date.getMinutes()).slice(-2) + var second = ('0'+date.getSeconds()).slice(-2) + var name = year+'-'+month+'-'+day+'-'+hour+'-'+minute+'-'+second+'.jpg' + // + // take new image + // + console.log('\ncapture '+name) + while (true) { + execSync('avconv -y -f v4l2 -s 1280x720 -i /dev/video0 -vframes 1 image.jpg') + // + // check that taking image was successful + // + if (fs.existsSync('image.jpg')) + break + } + // + // remove prior images + // + date.setDate(date.getDate()-days_to_save) + var oldyear = date.getFullYear() + var oldmonth = ('0'+(1+parseInt(date.getMonth()))).slice(-2) + var oldday = ('0'+date.getDate()).slice(-2) + var oldhour = ('0'+date.getHours()).slice(-2) + var oldminute = ('0'+date.getMinutes()).slice(-2) + var prefix = oldyear+'-'+oldmonth+'-'+oldday+'-'+oldhour+'-'+oldminute+'*' + console.log('remove '+prefix) + execSync('rm -f images/'+prefix+'*') + execSync('rm -f thumbnails/'+prefix+'*') + // + // check if image has changed beyond threshold + // + try { + execSync('compare -metric RMSE image.jpg oldimage.jpg null: 2>&1') + } + catch(err) { + var ret = String(err.stdout).split(' ') + var ptrl = ret[1].indexOf('(') + var ptrr = ret[1].indexOf(')') + var diff = parseFloat(ret[1].slice(ptrl+1,ptrr)) + 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('cp image.jpg images/'+name) + // + // rebuild day's index + // + console.log('reindex day') + execSync("echo '' > index/newindex.html") + execSync("echo '' > index/newindex.html") + execSync('ls images | awk \'/^'+year+'-'+month+'-'+day+'/ { print "" }\' >> index/newindex.html') + execSync("mv index/newindex.html index/"+month+"-"+day+".html") + // + // delay before next image + // + console.log('delay before next image') + execSync('mv image.jpg oldimage.jpg') + setTimeout(update,millisecond_image_delay) + } + else { + // + // size hasn't changed + // + console.log('no activity detected, don\'t save image') + // + // no delay before next image + // + execSync('mv image.jpg oldimage.jpg') + setTimeout(update,0) + } + } +// +// take initial image +// +execSync('avconv -y -f v4l2 -s 1280x720 -i /dev/video0 -vframes 1 oldimage.jpg') +// +// start update +// +update() +