Skip to content

Commit 3c3da64

Browse files
committed
Add analyze mode to VR 4 node
1 parent 6ecb7ce commit 3c3da64

2 files changed

Lines changed: 88 additions & 4 deletions

File tree

services/visual_recognition/v4-collection-utils.html

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
<div class="form-row">
3838
<label for="node-input-image-feature"><i class="fa fa-book"></i> Mode: </label>
3939
<select type="text" id="node-input-image-feature" style="display: inline-block; width: 70%;">
40+
<option value="analyze">Analyze Images</option>
41+
<option disabled>______________</option>
4042
<option value="createCollection">Create a collection</option>
4143
<option value="listCollections">List collections</option>
4244
<option value="getCollection">Get collection details</option>
@@ -49,6 +51,10 @@
4951
<option value="getImageDetails">Get details of image in collection</option>
5052
<option value="deleteImage">Delete image from collection</option>
5153
<option value="getJpegImage">Get JPEG image from collection</option>
54+
<option disabled>______________</option>
55+
<option value="train">Train a collection</option>
56+
<option value="addImageTrainingData">Add training data to image</option>
57+
<option value="getTrainingUsage">Get training usage</option>
5258
</select>
5359
</div>
5460
</script>
@@ -115,6 +121,11 @@
115121
your needs.</p>
116122
<p>The following features are available :</p>
117123
<ul>
124+
<li>Analysis
125+
<ul>
126+
<li><b>Analyze Images</b></li>
127+
</ul>
128+
</li>
118129
<li>Collections
119130
<ul>
120131
<li><b>Create a collection</b></li>
@@ -134,10 +145,30 @@
134145
<li><b>Get JPEG image from collection</b></li>
135146
</ul>
136147
</li>
148+
<li>Training
149+
<ul>
150+
<li><b>Train a collection</b></li>
151+
<li><b>Add training data to an image</b></li>
152+
<li><b>Get training usage</b></li>
153+
</ul>
154+
</li>
137155
</ul>
138156

139157
<p>All Results will made available at <code>msg.payload</code></p>
140158

159+
<p><b>Analyze Images</b></p>
160+
<p>this feature should be provided in input : </p>
161+
<ul>
162+
<li><code>msg.payload</code> : Either a zip of image files or an array of URLs of image files (Required)</li>
163+
<li><code>msg.params["collectionId"]</code> : The identifier of the collection (Required)</li>
164+
<li><code>msg.params["threshold"]</code> : Minimum score for matches (Optional)</li>
165+
</ul>
166+
<p>More information on this
167+
<a href="https://cloud.ibm.com/apidocs/visual-recognition/visual-recognition-v4?code=node#add-images">
168+
API documentation</a>.</p>
169+
170+
171+
141172
<p><b>Create a collection</b></p>
142173
<p>this feature should be provided in input : </p>
143174
<ul>
@@ -234,8 +265,30 @@
234265
<li><code>msg.params["size"]</code> : Set to either 'full' or 'thumbnail' (Optional)</li>
235266
</ul>
236267
<p>More information on this
237-
<a href="https://cloud.ibm.com/apidocs/visual-recognition/visual-recognition-v4?code=node#delete-an-image">
268+
<a href="https://cloud.ibm.com/apidocs/visual-recognition/visual-recognition-v4?code=node#get-a-jpeg-file-of-an-image">
269+
API documentation</a>.</p>
270+
271+
<p><b>Train a collection</b></p>
272+
<p>this feature should be provided in input : </p>
273+
<ul>
274+
<li><code>msg.params["collectionId"]</code> : The identifier of the collection (Required)</li>
275+
</ul>
276+
<p>More information on this
277+
<a href="https://cloud.ibm.com/apidocs/visual-recognition/visual-recognition-v4?code=node#train-a-collection">
238278
API documentation</a>.</p>
239279

280+
<p><b>Add training data to an image</b></p>
281+
<p>this feature should be provided in input : </p>
282+
<ul>
283+
<li><code>msg.params["collectionId"]</code> : The identifier of the collection (Required)</li>
284+
<li><code>msg.params["imageId"]</code> : The identifier of the image (Required)</li>
285+
<li><code>msg.params["objects"]</code> : An array of training data (Required)</li>
286+
</ul>
287+
<p>More information on this
288+
<a href="https://cloud.ibm.com/apidocs/visual-recognition/visual-recognition-v4?code=node#add-training-data-to-an-image">
289+
API documentation</a>.</p>
290+
291+
<p><b>Get training usage</b></p>
292+
<p>No input parameters are required : </p>
240293

241294
</script>

services/visual_recognition/v4-collection-utils.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module.exports = function(RED) {
2020
{ IamAuthenticator } = require('ibm-watson/auth'),
2121
FEATURE = 'image-feature',
2222
REQUIRED_PARAMS = {
23+
'analyze': ['collectionIds'],
2324
'createCollection': ['name', 'description'],
2425
'getCollection': ['collectionId'],
2526
'updateCollection': ['collectionId'],
@@ -28,7 +29,9 @@ module.exports = function(RED) {
2829
'listImages': ['collectionId'],
2930
'getImageDetails': ['collectionId', 'imageId'],
3031
'deleteImage': ['collectionId', 'imageId'],
31-
'getJpegImage': ['collectionId', 'imageId']
32+
'getJpegImage': ['collectionId', 'imageId'],
33+
'train': ['collectionId'],
34+
'addImageTrainingData' : ['collectionId', 'imageId', 'objects']
3235
};
3336

3437
var pkg = require('../../package.json'),
@@ -114,7 +117,7 @@ module.exports = function(RED) {
114117
reject(err);
115118
})
116119
}
117-
});
120+
});
118121
}
119122

120123
function extractIDs(body) {
@@ -268,6 +271,7 @@ module.exports = function(RED) {
268271

269272
switch (feature) {
270273

274+
case 'analyze':
271275
case 'createCollection':
272276
case 'getCollection':
273277
case 'updateCollection':
@@ -277,6 +281,8 @@ module.exports = function(RED) {
277281
case 'getImageDetails':
278282
case 'deleteImage':
279283
case 'getJpegImage':
284+
case 'train':
285+
case 'addImageTrainingData':
280286
theMissing = paramCheckFor(REQUIRED_PARAMS[feature], msg);
281287
if (theMissing.length === 0) {
282288
return Promise.resolve();
@@ -286,6 +292,7 @@ module.exports = function(RED) {
286292

287293
case 'listCollections':
288294
case 'deleteAllCollections':
295+
case 'getTrainingUsage':
289296
if (! msg.params) {
290297
msg.params = {};
291298
}
@@ -296,6 +303,13 @@ module.exports = function(RED) {
296303
}
297304
}
298305

306+
function augmentParams(node, msg) {
307+
if ('analyze' === node.config[FEATURE]) {
308+
msg.params.features = ['objects'];
309+
}
310+
return Promise.resolve();
311+
}
312+
299313
function bufferCheck(data) {
300314
return data instanceof Buffer;
301315
}
@@ -335,9 +349,19 @@ module.exports = function(RED) {
335349
});
336350
}
337351

352+
function imagesExpected(feature) {
353+
switch(feature) {
354+
case 'addImages':
355+
case 'analyze':
356+
return true;
357+
default:
358+
return false;
359+
}
360+
}
361+
338362
function processPayload(node, msg) {
339363
return new Promise(function resolver(resolve, reject) {
340-
if ('addImages' !== node.config[FEATURE]) {
364+
if (!imagesExpected(node.config[FEATURE])) {
341365
resolve();
342366
} else if (Array.isArray(msg.payload)) {
343367
// Payload can be either an array of urls for images
@@ -374,8 +398,12 @@ module.exports = function(RED) {
374398
case 'getImageDetails':
375399
case 'deleteImage':
376400
case 'getJpegImage':
401+
case 'train':
402+
case 'addImageTrainingData':
403+
case 'getTrainingUsage':
377404
return Promise.resolve();
378405
case 'addImages':
406+
case 'analyze':
379407
if (!msg.payload) {
380408
return Promise.reject('Missing property: msg.payload');
381409
}
@@ -408,6 +436,9 @@ module.exports = function(RED) {
408436
.then(() => {
409437
return verifyParams(node, msg);
410438
})
439+
.then(() => {
440+
return augmentParams(node, msg);
441+
})
411442
.then(() => {
412443
return processPayload(node, msg);
413444
})

0 commit comments

Comments
 (0)