Skip to content
This repository was archived by the owner on Oct 30, 2018. It is now read-only.

Added Face recognition functionality #9

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions alchemyapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,9 @@ AlchemyAPI.ENDPOINTS['image']['url'] = '/url/URLGetImage';
AlchemyAPI.ENDPOINTS['image_keywords'] = {};
AlchemyAPI.ENDPOINTS['image_keywords']['url'] = '/url/URLGetRankedImageKeywords';
AlchemyAPI.ENDPOINTS['image_keywords']['image'] = '/image/ImageGetRankedImageKeywords';
AlchemyAPI.ENDPOINTS['image_face_tag'] = {};
AlchemyAPI.ENDPOINTS['image_face_tag']['url'] = '/url/URLGetRankedImageFaceTags';
AlchemyAPI.ENDPOINTS['image_face_tag']['image'] = '/image/ImageGetRankedImageFaceTags';



Expand Down Expand Up @@ -829,3 +832,33 @@ AlchemyAPI.prototype.image_keywords =
}
};

/**
* Face detection and Recognition
*
* INPUT:
* flavor -> which version of the call (currently, only 'url' or 'image' is supported)
* data -> the URL to the data to analyze.
* options -> various parameters that can be used to adjust how the API works, see below for more info on the available options.
* callback -> the callback function for this async function
*
* Available Options:
* imagePostMode -> not-raw: pass an unencoded image file with "image=URI_ENCODED_DATA"; raw: pass an unencoded image file using POST ('image' flavor only).
* knowledgeGraph -> provide extra metadata for detected celebrities
*
* OUTPUT:
* The response, already converted from JSON to a Javascript object.
*/
AlchemyAPI.prototype.image_face_tag =
function(flavor, data, options, callback) {
options = options || {}

//Add the data to the options and analyze
if (flavor === "image") { // if it's an image, we'll pass the image to upload
this.analyze(AlchemyAPI.ENDPOINTS['image_face_tag'][flavor],
options, data, callback);
} else {
options[flavor] = data;
this.analyze(AlchemyAPI.ENDPOINTS['image_face_tag'][flavor],
options, callback);
}
};
36 changes: 36 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,21 @@ server.listen(port, function(){
var demo_text = 'Yesterday dumb Bob destroyed my fancy iPhone in beautiful Denver, Colorado. I guess I will have to head over to the Apple Store and buy a new one.';
var demo_url = 'http://www.npr.org/2013/11/26/247336038/dont-stuff-the-turkey-and-other-tips-from-americas-test-kitchen';
var demo_html = '<html><head><title>Node.js Demo | AlchemyAPI</title></head><body><h1>Did you know that AlchemyAPI works on HTML?</h1><p>Well, you do now.</p></body></html>';
var post_image = 'emaxfpo.jpg';
var url_face_image = 'http://demo1.alchemyapi.com/images/vision/mother-daughter.jpg';
var post_face_image = 'politicians.jpg';
var fs = require('fs');

// function to encode file data to base64 encoded string
function base64_encode(file) {
// read binary data
var bitmap = fs.readFileSync(file);
// convert binary data to base64 encoded string
return new Buffer(bitmap).toString('base64');
}

var base64str = base64_encode(post_image);
var face_base64str = base64_encode(post_face_image);

function example(req, res) {
var output = {};
Expand Down Expand Up @@ -178,6 +192,27 @@ function combined(req, res, output) {
function image(req, res, output) {
alchemyapi.image('url', demo_url, {}, function(response) {
output['image'] = { url:demo_url, response:JSON.stringify(response,null,4), results:response };
post_local_image(req, res, output);
});
}

function post_local_image(req, res, output) {
alchemyapi.image_keywords('image', post_image, {}, function(response) {
output['post_local_image'] = { url:post_image, base:base64str, response:JSON.stringify(response,null,4), results:response };
face_image(req, res, output);
});
}

function face_image(req, res, output) {
alchemyapi.image_face_tag('url', url_face_image, {}, function(response) {
output['face_image'] = { url:url_face_image, response:JSON.stringify(response,null,4), results:response };
face_local_image(req, res, output);
});
}

function face_local_image(req, res, output) {
alchemyapi.image_face_tag('image', post_face_image, {}, function(response) {
output['face_local_image'] = { url:post_face_image, base:face_base64str, response:JSON.stringify(response,null,4), results:response };
image_keywords(req, res, output);
});
}
Expand All @@ -189,3 +224,4 @@ function image_keywords(req, res, output) {
});
}


Binary file added politicians.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 22 additions & 1 deletion tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ var test_text = 'Bob broke my heart, and then made up this silly sentence to tes
var test_html = '<html><head><title>The best SDK Test | AlchemyAPI</title></head><body><h1>Hello World!</h1><p>My favorite language is Javascript</p></body></html>';
var test_url = 'http://www.nytimes.com/2013/07/13/us/politics/a-day-of-friction-notable-even-for-a-fractious-congress.html?_r=0';
var test_image = './emaxfpo.jpg';
var test_face_url = 'http://demo1.alchemyapi.com/images/vision/mother-daughter.jpg';
var test_face_image = 'politicians.jpg';



Expand Down Expand Up @@ -380,10 +382,29 @@ function image_keywords() {
alchemyapi.image_keywords('image', test_image, null, function(response) {
assert.equal(response['status'],'OK');
console.log('Image keywords tests complete!\n');
combined();
url_face_tag();
});
}

//Face detection with URL
function url_face_tag() {
console.log('Checking url image Face . . . ');
alchemyapi.image_face_tag('url', test_face_url, null, function(response) {
assert.equal(response['status'],'OK');
console.log('Face Detection tests complete!\n');
image_face_tag();
});
}

//Face detection with POST
function image_face_tag() {
console.log('Checking image Face . . . ');
alchemyapi.image_face_tag('image', test_face_image, null, function(response) {
assert.equal(response['status'],'OK');
console.log('Face Detection tests complete!\n');
combined();
});
}

//Combined
function combined() {
Expand Down
37 changes: 37 additions & 0 deletions views/example.dust
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,20 @@

<p><strong>Image:</strong> {image.results.image}<br/>


<hr>

<h1>Image Local Upload</h1>
<p>
For the docs: <a href='http://www.alchemyapi.com/api/' target='_blank'>http://www.alchemyapi.com/api/</a></p>

<h3>Processed Local Image</h3>
<p><img src="data:image/jpg;base64,{post_local_image.base}"></p>

<h3>Response Object</h3>
<textarea rows='10' class='form-control'>{post_local_image.response}</textarea>


<hr>

<h1>Image Keywords Tagging</h1>
Expand Down Expand Up @@ -323,6 +337,29 @@

<hr>

<h1>Face Recognition</h1>
<p>
For the docs: <a href='http://www.alchemyapi.com/api/' target='_blank'>http://www.alchemyapi.com/api/</a></p>

<h3>Processed URL</h3>
<p><a href='{face_image.url}'>{face_image.url}</a></p>

<h3>Response Object</h3>
<textarea rows='10' class='form-control'>{face_image.response}</textarea>

<hr>

<h1>Face Recognition Local Image</h1>
<p>
For the docs: <a href='http://www.alchemyapi.com/api/' target='_blank'>http://www.alchemyapi.com/api/</a></p>

<h3>Local Image</h3>
<p><img src="data:image/jpg;base64,{face_local_image.base}"></p>

<h3>Response Object</h3>
<textarea rows='10' class='form-control'>{face_local_image.response}</textarea>

<hr>

</div>
{/content}