Skip to content

Commit d166af8

Browse files
authored
Merge pull request #147 from watson-developer-cloud/master
Rebasing
2 parents 2f0bd8f + a7dcc81 commit d166af8

File tree

3 files changed

+43
-57
lines changed

3 files changed

+43
-57
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ Node-RED Watson Nodes for IBM Cloud
1111
- Fix to defaulting name for NLU Node.
1212
- Allow pre-check of audio format to be disabled in Speech to Text node.
1313
- Migrate from deprecated getModels and getCustomizations methods in Speech to Text nodes.
14+
- Implement update classifier in Visual Recognition Util node.
15+
- In Visual Recognition Util node append 'positive_' to zip name if neither 'positive' not 'negative' not already there.
16+
- Removed duplicated code in Visual Recognition Util node.
1417

1518
### New in version 0.6.10
1619
- Needed to stringify json before addDocument in Discovery node.

services/visual_recognition/v3.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<option value="createClassifier">Create a classifier</option>
6868
<option value="retrieveClassifiersList">Retrieve a list of classifiers</option>
6969
<option value="retrieveClassifierDetails">Retrieve Classifier Details</option>
70+
<option value="updateClassifier">Update a classifier</option>
7071
<option value="deleteClassifier">Delete a classifier</option>
7172
<option value="deleteAllClassifiers">Delete all classifiers</option>
7273
</select>
@@ -94,7 +95,7 @@
9495
<p>This feature should be provided in input : </p>
9596
<ul>
9697
<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>
97-
<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>
98+
<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>
9899
<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>
99100
<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>
100101
<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>

services/visual_recognition/v3.js

Lines changed: 38 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,19 @@
1515
**/
1616

1717
module.exports = function(RED) {
18-
const SERVICE_IDENTIFIER = 'visual-recognition';
19-
const VisualRecognitionV3 = require('watson-developer-cloud/visual-recognition/v3');
18+
const SERVICE_IDENTIFIER = 'visual-recognition',
19+
VisualRecognitionV3 = require('watson-developer-cloud/visual-recognition/v3'),
20+
METHODS = {
21+
CREATECLASSIFER : 'createClassifier',
22+
LISTCLASSIFIERS : 'listClassifiers',
23+
UPDATECLASSIFIER : 'updateClassifier',
24+
GETCLASSIFIER : 'getClassifier',
25+
DELETECLASSIFIER : 'deleteClassifier',
26+
retrieveClassifiersList : 'listClassifiers',
27+
retrieveClassifierDetails : 'getClassifier',
28+
deleteClassifier : 'deleteClassifier'
29+
};
30+
2031
var pkg = require('../../package.json'),
2132
serviceutils = require('../../utilities/service-utils'),
2233
payloadutils = require('../../utilities/payload-utils'),
@@ -254,8 +265,12 @@ module.exports = function(RED) {
254265
return callback('open error on ' + k);
255266
}
256267
stream_buffer(info.path, msg.params[k], function() {
257-
listParams[k] = fs.createReadStream(info.path);
258-
callback(null, k);
268+
let example_name = k;
269+
if (! (k.includes('positive') || k.includes('negative'))) {
270+
example_name = k.replace('examples', 'positive_examples');
271+
}
272+
listParams[example_name] = fs.createReadStream(info.path);
273+
callback(null, example_name);
259274
});
260275
});
261276
});
@@ -269,7 +284,7 @@ module.exports = function(RED) {
269284
// msg.params["negative_examples"] : a Node.js binary Buffer of the ZIP
270285
// that contains a minimum of 10 images.(Optional)
271286

272-
function prepareParamsCreateClassifier(params, node, msg) {
287+
function prepareParamsClassifierFiles(params, node, msg) {
273288
var p = new Promise(function resolver(resolve, reject) {
274289
var listParams = {},
275290
asyncTasks = [],
@@ -378,9 +393,9 @@ module.exports = function(RED) {
378393
return p;
379394
}
380395

381-
function invokeCreateClassifier(node, params) {
396+
function invokeClassifierMethod(node, params, method) {
382397
var p = new Promise(function resolver(resolve, reject) {
383-
node.service.createClassifier(params, function(err, body) {
398+
node.service[method](params, function(err, body) {
384399
if (err) {
385400
reject(err);
386401
} else {
@@ -392,52 +407,21 @@ module.exports = function(RED) {
392407
return p;
393408
}
394409

395-
function invokeListClassifiers(node, params) {
396-
var p = new Promise(function resolver(resolve, reject) {
397-
node.service.listClassifiers(params, function(err, body) {
398-
if (err) {
399-
reject(err);
400-
} else {
401-
resolve(body);
402-
}
403-
});
404-
});
405-
return p;
406-
}
407-
408-
function invokeGetClassifier(node, params, msg) {
409-
var p = new Promise(function resolver(resolve, reject) {
410-
params['classifier_id'] = msg.params['classifier_id'];
411-
node.service.getClassifier(params, function(err, body) {
412-
if (err) {
413-
reject(err);
414-
} else {
415-
resolve(body);
416-
}
410+
function executeCreateClassifier(params, node, msg) {
411+
var p = prepareParamsClassifierFiles(params, node, msg)
412+
.then(function() {
413+
return invokeClassifierMethod(node, params, METHODS.CREATECLASSIFER);
417414
});
418-
});
419-
return p;
420-
}
421415

422-
function invokeDeleteClassifier(node, params, msg) {
423-
var p = new Promise(function resolver(resolve, reject) {
424-
params['classifier_id'] = msg.params['classifier_id'];
425-
node.service.deleteClassifier(params, function(err, body) {
426-
if (err) {
427-
reject(err);
428-
} else {
429-
resolve(body);
430-
}
431-
});
432-
});
433416
return p;
434417
}
435418

436-
437-
function executeCreateClassifier(params, node, msg) {
438-
var p = prepareParamsCreateClassifier(params, node, msg)
419+
function executeUpdateClassifier(params, node, msg) {
420+
// This is not an error, the params for create & update
421+
// are essentially the same.
422+
var p = prepareParamsClassifierFiles(params, node, msg)
439423
.then(function() {
440-
return invokeCreateClassifier(node, params);
424+
return invokeClassifierMethod(node, params, METHODS.UPDATECLASSIFIER);
441425
});
442426

443427
return p;
@@ -453,22 +437,17 @@ module.exports = function(RED) {
453437
});
454438
break;
455439

456-
case 'retrieveClassifiersList':
457-
p = invokeListClassifiers(node, params)
440+
case 'updateClassifier':
441+
p = executeUpdateClassifier(params, node, msg)
458442
.then(function(body) {
459443
return processTheResponse(body, feature, node, msg);
460444
});
461445
break;
462446

447+
case 'retrieveClassifiersList':
463448
case 'retrieveClassifierDetails':
464-
p = invokeGetClassifier(node, params, msg)
465-
.then(function(body) {
466-
return processTheResponse(body, feature, node, msg);
467-
});
468-
break;
469-
470449
case 'deleteClassifier':
471-
p = invokeDeleteClassifier(node, params, msg)
450+
p = invokeClassifierMethod(node, params, METHODS[feature])
472451
.then(function(body) {
473452
return processTheResponse(body, feature, node, msg);
474453
});
@@ -494,6 +473,9 @@ module.exports = function(RED) {
494473
return executeService(feature, params, node, msg);
495474
//return Promise.resolve();
496475
} else {
476+
if (msg.params && msg.params['classifier_id']) {
477+
params['classifier_id'] = msg.params['classifier_id'];
478+
}
497479
return executeUtilService(feature, params, node, msg);
498480
// return Promise.resolve();
499481
}

0 commit comments

Comments
 (0)