Skip to content
This repository has been archived by the owner on Jun 23, 2019. It is now read-only.

Commit

Permalink
Feature: Face detect, face similar
Browse files Browse the repository at this point in the history
  • Loading branch information
felixrieseberg committed Aug 8, 2015
0 parents commit 5cde551
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .gitignore
@@ -0,0 +1,28 @@
# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git
node_modules
5 changes: 5 additions & 0 deletions jsconfig.json
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"target": "ES6"
}
}
124 changes: 124 additions & 0 deletions lib/face.js
@@ -0,0 +1,124 @@
var request = require('request'),
Promise = require('Bluebird'),
fs = require('fs');

const detectUrl = 'https://api.projectoxford.ai/face/v0/detections';
const similarUrl = 'https://api.projectoxford.ai/face/v0/findsimilars';

var face = function (key) {
/**
* Call the Face Detected API using a local image
* @param {string} image - Path to the image
* @param {object} options - Querystring object
* @return {Promise} - Promise resolving with the resulting JSON
*/
var _detectLocal = function (image, options) {
return new Promise(function (resolve, reject) {
fs.createReadStream(image).pipe(request.post({
uri: detectUrl,
headers: {
'Ocp-Apim-Subscription-Key': key,
'Content-Type': 'application/octet-stream'
},
qs: options
}, function (error, response) {
if (error) {
return reject(error);
}

return resolve(response);
})
)});
};

/**
* Call the Face Detected API using a uri to an online iage
* @param {string} image - Url to the image
* @param {object} options - Querystring object
* @return {Promise} - Promise resolving with the resulting JSON
*/
var _detectOnline = function (image, options) {
return new Promise(function (resolve, reject) {
request.post({
uri: detectUrl,
headers: {
'Ocp-Apim-Subscription-Key': key,
},
json: true,
body: {
'url': image
},
qs: options
}, function (error, response) {
if (error) {
return reject(error);
}

return resolve(response);
})
});
};

/**
* Call the Face Detected API
* @param {object} options - Options object
* @param {boolean} options.analyzesFaceLandmarks - Analyze face landmarks?
* @param {boolean} options.analyzesAge - Analyze age?
* @param {boolean} options.analyzesGender - Analyze gender?
* @param {boolean} options.analyzesHeadPose - Analyze headpose?
* @return {Promise} - Promise resolving with the resulting JSON
*/
var detect = function (options) {
let qs = {
analyzesFaceLandmarks: options.analyzesFaceLandmarks ? true : false,
analyzesAge: options.analyzesAge ? true : false,
analyzesGender: options.analyzesGender ? true : false,
analyzesHeadPose: options.analyzesHeadPose ? true : false
}

if (options.url && options.url !== '') {
return _detectOnline(options.url, qs);
}

if (options.path && options.path !== '') {
return _detectLocal(options.path, qs);
}
};

/**
* Detect similar faces using faceIds (as returned from the detect API)
* @param {string[]} faces - Array of faceIds to detec
* @return {Promise} - Promise resolving with the resulting JSON
*/
var similar = function (faces) {
return new Promise(function (resolve, reject) {
if (faces.length === 1) {
faces = {'faceId': faces[0]};
} else {
faces = {'faceIds': faces};
}

request.post({
uri: similarUrl,
headers: {
'Ocp-Apim-Subscription-Key': key,
},
json: true,
body: faces
}, function (error, response) {
if (error) {
return reject(error);
}

return resolve(response);
})
});
}

return {
detect: detect,
similar: similar
}
}

module.exports = face;
13 changes: 13 additions & 0 deletions lib/oxford.js
@@ -0,0 +1,13 @@
var face = require('./face'),
oxford = {};

oxford.Client = function (key) {
if (!key || key === '') {
return console.error('Tried to initialize Project Oxford client without API key');
}

this._key = key;
this.face = face(key);
}

module.exports = oxford;
16 changes: 16 additions & 0 deletions package.json
@@ -0,0 +1,16 @@
{
"name": "project-oxford",
"version": "0.1.0",
"description": "Vision, Speech, and Face Intelligence from Project Oxford",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Felix Rieseberg",
"license": "MIT",
"dependencies": {
"bluebird": "^2.9.34",
"request": "^2.60.0",
"restling": "^0.9.1"
}
}

0 comments on commit 5cde551

Please sign in to comment.