diff --git a/README.md b/README.md index 9b70e143c32bd40c6674779f7250faad29a119bc..0c0f1c3e665dd6337822d9725c2d7450e2553af3 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ to reinitialize: ./init to run: node cams.js -uses fswebcam for screen capture +uses ffmpeg for screen capture uses convert to make thumbnails @@ -12,8 +12,6 @@ uses compare to measure activity can use uvcdynctrl -f to list resolutions -can use fswebcam --list-controls to list controls - can use sudo guvcview to adjust camera can use http-server-with-auth to serve: @@ -23,7 +21,7 @@ index is at http://this_address:this_port last image is at http://this_address:this_port/oldimage.jpg can use Ubuntu MATE on Raspberry Pi 3: -sudo apt-get install nodejs-legacy npm fswebcam imagemagick guvcview +sudo apt-get install nodejs-legacy npm ffmpeg imagemagick guvcview sudo npm install -g http-server-with-auth System -> Administration -> Time and Date -> Keep synchronized with internet servers diff --git a/cams.js b/cams.js index 3cfa18cf6ddece654883669986b06798d5bed22a..062ec8ed25c3b97d3a4435f5b7a2edbf3e6a09ed 100644 --- a/cams.js +++ b/cams.js @@ -9,24 +9,10 @@ var image_width = 1600 var image_height = 1200 var thumbnail_size = 200 -var compression = 50 +var video_filter = 'vflip,hflip' 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 // @@ -52,14 +38,16 @@ function update() { // take new image // console.log('\ncapture '+name) - while (true) { - execSync('fswebcam --jpeg '+compression+' --resolution '+image_width+'x'+image_height+' '+flip+' --save image.jpg') - // - // check that taking image was successful - // - if (fs.existsSync('image.jpg')) - break + var success = true + do { + try { + execSync('ffmpeg -y -f v4l2 -s '+image_width+'x'+image_height+' -i /dev/video0 -vframes 1 -vf '+video_filter+' image.jpg') + } + catch(err) { + success = false + } } + while (success != true) // // remove prior images // @@ -123,7 +111,16 @@ function update() { // // take initial image // -execSync('fswebcam --jpeg '+compression+' --resolution '+image_width+'x'+image_height+' '+flip+' '+settings+' --save oldimage.jpg') +var success = true +do { + try { + execSync('ffmpeg -y -f v4l2 -s '+image_width+'x'+image_height+' -i /dev/video0 -vframes 1 -vf '+video_filter+' oldimage.jpg') + } + catch(err) { + success = false + } + } +while (success != true) // // start update // diff --git a/cams1.js b/cams1.js deleted file mode 100644 index 062ec8ed25c3b97d3a4435f5b7a2edbf3e6a09ed..0000000000000000000000000000000000000000 --- a/cams1.js +++ /dev/null @@ -1,128 +0,0 @@ -// -// cams.js -// shopcam activity monitor -// Neil Gershenfeld -// 12/11/17 -// -// settings -// -var image_width = 1600 -var image_height = 1200 -var thumbnail_size = 200 -var video_filter = 'vflip,hflip' -var difference_threshold = .05 -var millisecond_image_delay = 15000 -var days_to_save = 150 -// -// 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) - var success = true - do { - try { - execSync('ffmpeg -y -f v4l2 -s '+image_width+'x'+image_height+' -i /dev/video0 -vframes 1 -vf '+video_filter+' image.jpg') - } - catch(err) { - success = false - } - } - while (success != true) - // - // 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 -// -var success = true -do { - try { - execSync('ffmpeg -y -f v4l2 -s '+image_width+'x'+image_height+' -i /dev/video0 -vframes 1 -vf '+video_filter+' oldimage.jpg') - } - catch(err) { - success = false - } - } -while (success != true) -// -// start update -// -update() -