Skip to content

Instantly share code, notes, and snippets.

@ricardobeat
Created July 22, 2012 12:34
Show Gist options
  • Save ricardobeat/3159523 to your computer and use it in GitHub Desktop.
Save ricardobeat/3159523 to your computer and use it in GitHub Desktop.
Client for Lambda Labs face-detection API
var https = require('https')
, crypto = require('crypto')
function Face(publicKey, privateKey){
this.publicKey = publicKey
this.privateKey = privateKey
}
Face.prototype.detect = function(images, callback){
if (!/Array|String/.test(Object.prototype.toString.call(images))){
callback(new Error('images must be String or Array'))
return
}
var hash = crypto.createHmac('sha1', this.privateKey)
.update(this.publicKey)
.digest('hex')
var authorization = new Buffer(this.publicKey+':'+hash).toString('base64')
var options = {
host: 'lambda-face-detection-and-recognition.p.mashape.com'
, path: '/detect?images=' + images
, method: 'GET'
, headers: { 'X-Mashape-Authorization': authorization }
}
https.get(options, function(res){
var data = ''
res.on('data', function(d){
data += d
})
res.on('end', function(){
try {
data = JSON.parse(data)
} catch (err) {
return callback(err)
}
callback(null, data)
})
}).on('error', callback)
}
module.exports = Face
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment