Skip to content

Commit eaa4a5e

Browse files
authored
Merge pull request #171 from ylecleach/master
Visual Recognition V3 : added support of Accept-Language for Classify…
2 parents a81761c + 7ca5339 commit eaa4a5e

File tree

6 files changed

+66
-17
lines changed

6 files changed

+66
-17
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ Node-RED Watson Nodes for IBM Bluemix
77

88
<a href="https://cla-assistant.io/watson-developer-cloud/node-red-node-watson"><img src="https://cla-assistant.io/readme/badge/watson-developer-cloud/node-red-node-watson" alt="CLA assistant" /></a>
99

10+
1011
### New in version 0.4.9
1112
- Added in German and Japanese support to Natural Language Classifier node
13+
- Visual Recognition V3 : added support of Accept-Language for Classify feature, new icon with pink background, icon label renamed (removing v3 from node name)
1214

1315
### New in version 0.4.8
1416
- Fixed document conversion node when filetype is not recognized
File renamed without changes.
-1.58 KB
Binary file not shown.
8.6 KB
Loading

services/visual_recognition/v3.html

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@
3636
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
3737
<input type="text" id="node-input-name" placeholder="Name">
3838
</div>
39+
<div class="form-row" id="lang-group">
40+
<label for="node-input-lang-label"><i class="fa fa-language"></i> Language</label>
41+
<select type="text" id="node-input-lang" style="display: inline-block; width: 70%;" >
42+
<option value="en">English</option>
43+
<option value="es">Spanish</option>
44+
<option value="ar">Arabic</option>
45+
<option value="ja">Japanese</option>
46+
</select>
47+
</div>
3948
</script>
4049

4150
<script type="text/x-red" data-template-name="visual-recognition-util-v3">
@@ -84,6 +93,7 @@
8493
<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>
8594
<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>
8695
<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>
96+
<li><code>msg.params["accept_language"]</code> : Specifies the language of the output class names. Can be 'en' (English as default), 'es' (Spanish), 'ar' (Arabic) or 'ja' (Japanese). Classes for which no translation is available are omitted. If specified, it overrides the language specified in the node configuration (Optional)</li>
8797
</ul>
8898
<p>More information on this <a href="http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/visual-recognition/api/v3/?curl#classify_an_image">API documentation</a>.</p>
8999

@@ -163,29 +173,46 @@
163173

164174

165175
<script type="text/javascript">
176+
177+
// Need to simulate a namespace, as some of the variables had started to leak across nodes
178+
function VISUALRECOGNITIONV3 () {
179+
var language_selected = '';
180+
}
181+
182+
// This is the namespace for visualrecognitionv3. Currently only contains models, but more vars and functions may need to be
183+
// moved in if there is a clash with other nodes.
184+
var visualrecognitionv3 = new VISUALRECOGNITIONV3();
185+
186+
// Save the values in the dynamic lists to the hidden fields.
187+
function oneditsave(){
188+
visualrecognitionv3.language_selected = $('#node-input-lang').val();
189+
}
190+
166191
(function() {
167192
RED.nodes.registerType('visual-recognition-v3', {
168193
category: 'IBM Watson',
169194
defaults: {
170195
name: {value: ""},
171196
apikey: {value: ""},
172-
"image-feature": {value: ""}
197+
"image-feature": {value: ""},
198+
"lang": {value: ""},
173199
},
174200
credentials: {
175201
apikey: {type:"password"}
176202
},
177-
color: 'rgb(72, 232, 211)',
203+
color: 'rgb(228, 189, 255)',
178204
inputs: 1,
179205
outputs: 1,
180-
icon: "VR-v3-25x25.png",
181-
paletteLabel: "visual recognition v3",
206+
icon: "VR-v3-pink.png",
207+
paletteLabel: "visual recognition",
182208
label: function() {
183-
return this.name || "visual recognition v3";
209+
return this.name || "visual recognition";
184210
},
185211
labelStyle: function() {
186212
return this.name ? "node_label_italic" : "";
187213
},
188214
oneditprepare: function() {
215+
var node = this;
189216
$.getJSON('watson-visual-recognition/vcap/')
190217
.done(function (service) {
191218
$('.credentials').toggle(!service);
@@ -195,10 +222,21 @@
195222
})
196223
.always(function () {
197224
$('#credentials-check').hide();
198-
})
199-
}
200-
});
225+
});
201226

227+
// update list of ruleset for the selected service when the service changes
228+
$('#node-input-image-feature').change(function() {
229+
var selectedFeature = $('#node-input-image-feature');
230+
if (selectedFeature.val() == 'classifyImage') {
231+
$('#lang-group').show();
232+
}
233+
else {
234+
$('#lang-group').hide();
235+
}
236+
});
237+
},
238+
oneditsave: oneditsave
239+
});
202240

203241

204242
RED.nodes.registerType('visual-recognition-util-v3', {
@@ -211,13 +249,13 @@
211249
credentials: {
212250
apikey: {type:"password"}
213251
},
214-
color: 'rgb(72, 232, 211)',
252+
color: 'rgb(228, 189, 255)',
215253
inputs: 1,
216254
outputs: 1,
217-
icon: "VR-v3-25x25.png",
218-
paletteLabel: "visual recognition util v3",
255+
icon: "VR-v3-pink.png",
256+
paletteLabel: "visual recognition util",
219257
label: function() {
220-
return this.name || "visual recognition util v3";
258+
return this.name || "visual recognition util";
221259
},
222260
labelStyle: function() {
223261
return this.name ? "node_label_italic" : "";
@@ -235,7 +273,5 @@
235273
})
236274
}
237275
});
238-
239-
240276
})();
241277
</script>

services/visual_recognition/v3.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@ module.exports = function (RED) {
8585
node.apikey = sAPIKey || node.credentials.apikey;
8686
if (!node.apikey) {
8787
node.status({fill:'red', shape:'ring', text:'missing credentials'});
88-
var message ='Missing Watson Visual Recognition API service credentials';
89-
90-
node.error(message, msg);
88+
node.error('Missing Watson Visual Recognition API service credentials', msg);
9189
return false;
9290
}
9391
node.service = watson.visual_recognition({
@@ -154,6 +152,12 @@ module.exports = function (RED) {
154152
if (msg.params != null && msg.params.threshold != null) {
155153
params['threshold'] = msg.params['threshold'];
156154
}
155+
if (node.config != null && node.config.lang != null) {
156+
params['Accept-Language'] = node.config.lang;
157+
}
158+
if (msg.params != null && msg.params.accept_language != null) {
159+
params['Accept-Language'] = msg.params['accept_language'];
160+
}
157161
cb();
158162
});
159163
});
@@ -168,6 +172,12 @@ module.exports = function (RED) {
168172
if (msg.params != null && msg.params.threshold != null) {
169173
params['threshold'] = msg.params['threshold'];
170174
}
175+
if (node.config != null && node.config.lang != null) {
176+
params['Accept-Language'] = node.config.lang;
177+
}
178+
if (msg.params != null && msg.params.accept_language != null) {
179+
params['Accept-Language'] = msg.params['accept_language'];
180+
}
171181
return cb();
172182
} else {
173183
node.status({fill:'red', shape:'ring', text:'payload is invalid'});
@@ -331,6 +341,7 @@ module.exports = function (RED) {
331341
function WatsonVisualRecognitionV3Node (config) {
332342
var node = this, b = false, feature = config['image-feature'];
333343
RED.nodes.createNode(this, config);
344+
node.config = config;
334345

335346
node.on('input', function (msg) {
336347
var params = {};

0 commit comments

Comments
 (0)