Skip to content

Instantly share code, notes, and snippets.

@dzeitman
Last active November 3, 2017 18:40
Show Gist options
  • Save dzeitman/a029fe8890bafd05a0320e4305369217 to your computer and use it in GitHub Desktop.
Save dzeitman/a029fe8890bafd05a0320e4305369217 to your computer and use it in GitHub Desktop.
/*
required param
accessKey (String)
public_id (String)
optional param
save_json
Retrieves an image from cloudinary and runs Watson AI visual-recognition service.
auth:
watson_api_key : context.secrets.watson_api_key
"cloud_name": context.secrets.cloud_name,
"api_key": context.secrets.api_key,
"api_secret": context.secrets.api_secret
*/
var watson = require('watson-developer-cloud');
var cloudinary = require('cloudinary');
function save(data, jsonfilename, context, cb){
var objJsonStr = JSON.stringify(data,null,3);
var objJsonB64 = new Buffer(objJsonStr).toString("base64");
// format to B64 data:
var jsonData = 'data:image/png;base64,' + objJsonB64;
var options = { public_id: jsonfilename, resource_type: 'raw', tags:context.data.tags};
console.log('calling upload');
cloudinary.v2.uploader['upload'](jsonData,
options,
function(error, result) {
if(error){
console.log(error);
return cb(null, error);
}
if(result){
var payload = { static_json: result, data: data };
console.log(payload);
return cb(null, payload);
}
});
}
function sortClass(a,b){
return a.score - b.score;
}
function getTags(data){
var tags = [data].map(function(item,index,arr) {
return item.images[index].classifiers[0].classes.sort(sortClass)
.map(function (item,index){
if(item.score > 0.5){
return item.class;
}
});
});
console.log(tags.join(', '));
return tags;
}
function updateCloudinary(data, context,cb){
cloudinary.config({
"cloud_name": context.secrets.cloud_name,
"api_key": context.secrets.api_key,
"api_secret": context.secrets.api_secret
});
var public_id = context.data.public_id;
var tags = getTags(data);
var options = { faces:true, colors:true, image_metadata: true, resource_type: 'image', tags:tags, type:'upload', phash:true};
cloudinary.v2.uploader.explicit(public_id, options,
function(error, result) {
if(error){
console.log(error);
return cb(null, error);
}
if(result){
var jsonfilename = context.data.public_id + '.json';
var payload = {ibm: data, cloudinary:result};
save(payload, jsonfilename, context, cb);
}
});
}
function classify(context,cb){
var visual_recognition = watson.visual_recognition({
api_key: context.secrets.watson_api_key ,
version: 'v3',
version_date: '2016-05-20'
});
var params = {
url:'http://res.cloudinary.com/de-demo/image/upload/v1505182273/' + context.data.public_id
};
visual_recognition.classify(params, function(err, res) {
if (err){
console.log(err); cb(null, err);
}
if (res){
console.log(res);
updateCloudinary(res, context,cb);
// cb(null, res);
}
});
}
module.exports = function(context, cb) {
cloudinary.config({
"cloud_name": context.secrets.cloud_name,
"api_key": context.secrets.api_key,
"api_secret": context.secrets.api_secret
});
classify(context,cb)
};

Cloudinary's API's provide some great capibilities such as auto tagging.
Sometimes developers want to expand on that by calling other Third Party APIS such as IBM Watson.

In this example we'll take an asset previously uploaded to cloudinary and use IBM Watson services to classify the image to find out what's in the image and then combine the image metadata into a JSON payload, and save that payload back to Cloudinary as a Static JSON resource.

This simple pattern allows for caching the meta data within a static file.
This scenerio might be used versus calling Watson API's each time you want to retrieve the additional meta data.

We're using Auth0 Webtasks to host this Function as a Service (FAAS) example:

Sign up for IBM Bluemix and Cloudinary. Create an account with Webtask.io

Add your cloudinary and IBM Watson Credentials as secrets key values:

Cloudinary Credentials: cloud_name api_key api_secret

IBM Watson API Key: watson_api_key

Install the webtask cli: https://webtask.io/cli

Create your webtask: (from our URL)

wt create https://gist.githubusercontent.com/dzeitman/a029fe8890bafd05a0320e4305369217/raw/9d919bf9a0de9640e6092c1a56e79ea2b455a073/cloudinary-watson.js --name cloudinary-watson-classify --secret watson_api_key=<your-watson-key> --secret cloud_name=<your-cloudname> --secret api_key=<your-apikey> --secret api_secret=<your-api-secret>

Edit and run your webtask:

wt edit cloudinary-watson-classify

Or from the browser: (Where public_id is a previously uploaded image) https://wt-1f717bba0ab14ce48591a3520bd159b5-0.run.webtask.io/cloudinary-watson-classify?public_id=studio-talk&save_json=true

FAAS Function Workflow: public_id --> Classify --> UpdateCloudinary --> Save

Example Static JSON Output: http://res.cloudinary.com/de-demo/raw/upload/v1509023864/studio-talk.json

More about Webtask: Getting Started with WebTask: https://webtask.io/docs/101

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment