diff --git a/alchemyapi.js b/alchemyapi.js index b83cd3a..492791a 100644 --- a/alchemyapi.js +++ b/alchemyapi.js @@ -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'; @@ -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); + } +}; diff --git a/app.js b/app.js index 4836a51..82d7a73 100644 --- a/app.js +++ b/app.js @@ -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 = 'Node.js Demo | AlchemyAPI

Did you know that AlchemyAPI works on HTML?

Well, you do now.

'; +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 = {}; @@ -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); }); } @@ -189,3 +224,4 @@ function image_keywords(req, res, output) { }); } + diff --git a/politicians.jpg b/politicians.jpg new file mode 100644 index 0000000..bb362e2 Binary files /dev/null and b/politicians.jpg differ diff --git a/tests.js b/tests.js index ba9dee8..edc1710 100644 --- a/tests.js +++ b/tests.js @@ -24,6 +24,8 @@ var test_text = 'Bob broke my heart, and then made up this silly sentence to tes var test_html = 'The best SDK Test | AlchemyAPI

Hello World!

My favorite language is Javascript

'; 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'; @@ -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() { diff --git a/views/example.dust b/views/example.dust index e50f780..831c8ea 100644 --- a/views/example.dust +++ b/views/example.dust @@ -263,6 +263,20 @@

Image: {image.results.image}
+ +


+ +

Image Local Upload

+

+ For the docs: http://www.alchemyapi.com/api/

+ +

Processed Local Image

+

+ +

Response Object

+ + +

Image Keywords Tagging

@@ -323,6 +337,29 @@
+

Face Recognition

+

+ For the docs: http://www.alchemyapi.com/api/

+ +

Processed URL

+

{face_image.url}

+ +

Response Object

+ + +
+ +

Face Recognition Local Image

+

+ For the docs: http://www.alchemyapi.com/api/

+ +

Local Image

+

+ +

Response Object

+ + +
{/content}