Last active
July 12, 2021 03:53
-
-
Save manuquentin/6042779 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
socket.on('status', function(status){ | |
// ... | |
if(status.intrusion){ | |
takeSnapShotAndCompareTo('me@WorkmateProtection'); | |
} | |
}); | |
function takeSnapShotAndCompareTo(tag, done){ | |
var file; | |
var fileName; | |
async.waterfall([ | |
// Take a picture with the webcam | |
function(callback){ | |
takeSnapShot(callback); | |
}, | |
// Resize snapshot | |
function(snapShotRslt, stderr, callback){ | |
file = snapShotRslt.trim().split('...').pop(); | |
fileName = file.split('/').pop(); | |
// Resize file | |
imagemagick.resize({ | |
srcPath: file, | |
dstPath: file, | |
width: 512 | |
}, callback); | |
}, | |
// Upload image | |
function(stdin, stdout, callback){ | |
upload(file, callback); | |
}, | |
// Recognize it | |
function(stdout, stderr, callback){ | |
getMatchValue('http://my-server/'+fileName, tag, callback) | |
}, | |
// Handle result | |
function(result, callback){ | |
if(result < 50){ | |
sendSMS(done); | |
} | |
if(done){ | |
done() | |
} | |
} | |
], function(err, rslt){ | |
if(err){ | |
console.log('Err : '+err); | |
return; | |
} | |
}); | |
} | |
// Take a snapshow via imagesnap | |
function takeSnapShot(cb){ | |
var now = new Date(); | |
exec('imagesnap ~/Desktop/photo-'+now.getTime()+'.jpg', cb); | |
} | |
// Upload a picture to the public dir of the server | |
function upload(file, cb){ | |
exec('scp -i ~/.ssh/myPem.pem '+file+' user@my-server:/var/app/public', cb); | |
} | |
function getMatchValue(url, tag, cb){ | |
var apiURL = 'http://api.skybiometry.com/fc/faces/recognize.json?api_key='+config.skybiometry.key+'&api_secret='+config.skybiometry.secret+'&uids='+tag+'&urls='+url+'&attributes=all'; | |
http.get(apiURL, function(res){ | |
content = ''; | |
res.on('data', function(chunk){ | |
content += chunk; | |
}); | |
res.on('end', function(){ | |
content = JSON.parse(content); | |
// Nobody found : return 0 | |
if(content.photos[0].tags == undefined || content.photos[0].tags.length == 0 || content.photos[0].tags[0].uids.length == 0){ | |
return cb(null, 0); | |
} | |
// Return the ratio of similarity | |
cb(null, content.photos[0].tags[0].uids[0].confidence); | |
}) | |
}) | |
.on('error', cb); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment