Skip to content

Commit

Permalink
Merge pull request #405 from chughts/vrfiles
Browse files Browse the repository at this point in the history
Update Classifier - Visual Recognition Node
  • Loading branch information
chughts committed May 6, 2018
2 parents f091543 + da6d971 commit a7dcc81
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 57 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Node-RED Watson Nodes for IBM Cloud
- Fix to defaulting name for NLU Node.
- Allow pre-check of audio format to be disabled in Speech to Text node.
- Migrate from deprecated getModels and getCustomizations methods in Speech to Text nodes.
- Implement update classifier in Visual Recognition Util node.
- In Visual Recognition Util node append 'positive_' to zip name if neither 'positive' not 'negative' not already there.
- Removed duplicated code in Visual Recognition Util node.

### New in version 0.6.10
- Needed to stringify json before addDocument in Discovery node.
Expand Down
3 changes: 2 additions & 1 deletion services/visual_recognition/v3.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<option value="createClassifier">Create a classifier</option>
<option value="retrieveClassifiersList">Retrieve a list of classifiers</option>
<option value="retrieveClassifierDetails">Retrieve Classifier Details</option>
<option value="updateClassifier">Update a classifier</option>
<option value="deleteClassifier">Delete a classifier</option>
<option value="deleteAllClassifiers">Delete all classifiers</option>
</select>
Expand Down Expand Up @@ -94,7 +95,7 @@
<p>This feature should be provided in input : </p>
<ul>
<li><code>msg.payload</code> : the URL or the Node.js Buffer of an image. Redirects are followed, so you can use shortened URLs. (Required)</li>
<li><code>msg.params["detect_mode"]</code> : A setting of "classify", "faces" or "text" indicating the visual recognition feature required. "Default" is "classify" (string) (Optional)</li>
<li><code>msg.params["detect_mode"]</code> : A setting of "classify" or "faces" indicating the visual recognition feature required. "Default" is "classify" (string) (Optional)</li>
<li><code>msg.params["classifier_ids"]</code> : A comma-separated list of the classifier IDs used to classify the images. "Default" is the classifier_id of the built-in classifier. (string) (Optional)</li>
<li><code>msg.params["owners"]</code> : A comma-separated list with the value(s) "IBM" and/or "me" to specify which classifiers to run. (string) (Optional)</li>
<li><code>msg.params["threshold"]</code> : A floating value (in string format) that specifies the minimum score a class must have to be displayed in the response (Optional)</li>
Expand Down
94 changes: 38 additions & 56 deletions services/visual_recognition/v3.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,19 @@
**/

module.exports = function(RED) {
const SERVICE_IDENTIFIER = 'visual-recognition';
const VisualRecognitionV3 = require('watson-developer-cloud/visual-recognition/v3');
const SERVICE_IDENTIFIER = 'visual-recognition',
VisualRecognitionV3 = require('watson-developer-cloud/visual-recognition/v3'),
METHODS = {
CREATECLASSIFER : 'createClassifier',
LISTCLASSIFIERS : 'listClassifiers',
UPDATECLASSIFIER : 'updateClassifier',
GETCLASSIFIER : 'getClassifier',
DELETECLASSIFIER : 'deleteClassifier',
retrieveClassifiersList : 'listClassifiers',
retrieveClassifierDetails : 'getClassifier',
deleteClassifier : 'deleteClassifier'
};

var pkg = require('../../package.json'),
serviceutils = require('../../utilities/service-utils'),
payloadutils = require('../../utilities/payload-utils'),
Expand Down Expand Up @@ -254,8 +265,12 @@ module.exports = function(RED) {
return callback('open error on ' + k);
}
stream_buffer(info.path, msg.params[k], function() {
listParams[k] = fs.createReadStream(info.path);
callback(null, k);
let example_name = k;
if (! (k.includes('positive') || k.includes('negative'))) {
example_name = k.replace('examples', 'positive_examples');
}
listParams[example_name] = fs.createReadStream(info.path);
callback(null, example_name);
});
});
});
Expand All @@ -269,7 +284,7 @@ module.exports = function(RED) {
// msg.params["negative_examples"] : a Node.js binary Buffer of the ZIP
// that contains a minimum of 10 images.(Optional)

function prepareParamsCreateClassifier(params, node, msg) {
function prepareParamsClassifierFiles(params, node, msg) {
var p = new Promise(function resolver(resolve, reject) {
var listParams = {},
asyncTasks = [],
Expand Down Expand Up @@ -378,9 +393,9 @@ module.exports = function(RED) {
return p;
}

function invokeCreateClassifier(node, params) {
function invokeClassifierMethod(node, params, method) {
var p = new Promise(function resolver(resolve, reject) {
node.service.createClassifier(params, function(err, body) {
node.service[method](params, function(err, body) {
if (err) {
reject(err);
} else {
Expand All @@ -392,52 +407,21 @@ module.exports = function(RED) {
return p;
}

function invokeListClassifiers(node, params) {
var p = new Promise(function resolver(resolve, reject) {
node.service.listClassifiers(params, function(err, body) {
if (err) {
reject(err);
} else {
resolve(body);
}
});
});
return p;
}

function invokeGetClassifier(node, params, msg) {
var p = new Promise(function resolver(resolve, reject) {
params['classifier_id'] = msg.params['classifier_id'];
node.service.getClassifier(params, function(err, body) {
if (err) {
reject(err);
} else {
resolve(body);
}
function executeCreateClassifier(params, node, msg) {
var p = prepareParamsClassifierFiles(params, node, msg)
.then(function() {
return invokeClassifierMethod(node, params, METHODS.CREATECLASSIFER);
});
});
return p;
}

function invokeDeleteClassifier(node, params, msg) {
var p = new Promise(function resolver(resolve, reject) {
params['classifier_id'] = msg.params['classifier_id'];
node.service.deleteClassifier(params, function(err, body) {
if (err) {
reject(err);
} else {
resolve(body);
}
});
});
return p;
}


function executeCreateClassifier(params, node, msg) {
var p = prepareParamsCreateClassifier(params, node, msg)
function executeUpdateClassifier(params, node, msg) {
// This is not an error, the params for create & update
// are essentially the same.
var p = prepareParamsClassifierFiles(params, node, msg)
.then(function() {
return invokeCreateClassifier(node, params);
return invokeClassifierMethod(node, params, METHODS.UPDATECLASSIFIER);
});

return p;
Expand All @@ -453,22 +437,17 @@ module.exports = function(RED) {
});
break;

case 'retrieveClassifiersList':
p = invokeListClassifiers(node, params)
case 'updateClassifier':
p = executeUpdateClassifier(params, node, msg)
.then(function(body) {
return processTheResponse(body, feature, node, msg);
});
break;

case 'retrieveClassifiersList':
case 'retrieveClassifierDetails':
p = invokeGetClassifier(node, params, msg)
.then(function(body) {
return processTheResponse(body, feature, node, msg);
});
break;

case 'deleteClassifier':
p = invokeDeleteClassifier(node, params, msg)
p = invokeClassifierMethod(node, params, METHODS[feature])
.then(function(body) {
return processTheResponse(body, feature, node, msg);
});
Expand All @@ -494,6 +473,9 @@ module.exports = function(RED) {
return executeService(feature, params, node, msg);
//return Promise.resolve();
} else {
if (msg.params && msg.params['classifier_id']) {
params['classifier_id'] = msg.params['classifier_id'];
}
return executeUtilService(feature, params, node, msg);
// return Promise.resolve();
}
Expand Down

0 comments on commit a7dcc81

Please sign in to comment.