Skip to content

Commit bf2e89d

Browse files
Add visual Insights support to the wrapper
1 parent 3ee382b commit bf2e89d

File tree

5 files changed

+129
-1
lines changed

5 files changed

+129
-1
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ APIs and SDKs that use cognitive computing to solve complex problems.
3737
* [Speech to Text](#speech-to-text)
3838
* [Text to Speech](#text-to-speech)
3939
* [Tradeoff Analytics](#tradeoff-analytics)
40+
* [Visual Insights](#visual-insights)
4041
* [Visual Recognition](#visual-recognition)
4142
* [Running in Bluemix](#running-in-bluemix)
4243
* [Debug](#debug)
@@ -542,6 +543,31 @@ tradeoff_analytics.dilemmas(params, function(err, res) {
542543
});
543544
```
544545

546+
### Visual Insights
547+
Use the [Visual Insights][visual_insights] to get insight into the themes present in a collection of images based on their visual appearance/content.
548+
549+
```js
550+
var watson = require('watson-developer-cloud');
551+
var fs = require('fs');
552+
553+
var visual_insights = watson.visual_insights({
554+
username: '<username>',
555+
password: '<password>',
556+
version: 'v1'
557+
});
558+
559+
var params = {
560+
images_file: fs.createReadStream('./resources/images.zip')
561+
};
562+
563+
visual_insights.summary(params, function(err, res) {
564+
if (err)
565+
console.log(err);
566+
else
567+
console.log(JSON.stringify(res, null, 2));
568+
});
569+
```
570+
545571
### Visual Recognition
546572
Use the [Visual Recognition][visual_recognition] service to recognize the
547573
following picture.
@@ -641,6 +667,8 @@ See [CONTRIBUTING](https://github.com/watson-developer-cloud/nodejs-wrapper/blob
641667
[concept_expansion]: http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/doc/glimpseapi/
642668
[relationship_extraction]: http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/doc/sireapi/
643669
[visual_recognition]: http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/doc/visual-recognition/
670+
[visual_insights]: http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/doc/visual-insights/
671+
644672
[text_to_speech]: http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/doc/text-to-speech/
645673
[speech_to_text]: http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/doc/speech-to-text/
646674
[concept_insights]: http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/doc/concept-insights/

examples/resources/images.zip

102 KB
Binary file not shown.

examples/visual_insights.v1.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
var watson = require('watson-developer-cloud');
4+
var fs = require('fs');
5+
6+
var visual_insights = watson.visual_insights({
7+
username: 'INSERT YOUR USERNAME FOR THE SERVICE HERE',
8+
password: 'INSERT YOUR PASSWORD FOR THE SERVICE HERE',
9+
version: 'v1'
10+
});
11+
12+
var params = {
13+
images_file: fs.createReadStream('./resources/images.zip')
14+
};
15+
16+
visual_insights.summary(params, function(err, res) {
17+
if (err)
18+
console.log(err);
19+
else
20+
console.log(JSON.stringify(res, null, 2));
21+
});

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ var watson = {};
115115
'dialog',
116116
'retrieve_and_rank',
117117
'document_conversion',
118-
118+
'visual_insights',
119119
// deprecated
120120
'search',
121121
'language_identification',

services/visual_insights/v1.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* Copyright 2014 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
var extend = require('extend');
20+
var requestFactory = require('../../lib/requestwrapper');
21+
var isStream = require('isstream');
22+
23+
function VisualInsights(options) {
24+
// Default URL
25+
var serviceDefaults = {
26+
url: 'https://gateway.watsonplatform.net/visual-insights-experimental/api'
27+
};
28+
29+
// Replace default options with user provided
30+
this._options = extend(serviceDefaults, options);
31+
}
32+
33+
/**
34+
* Returns a list of the classifiers that are used in the summary call
35+
*/
36+
VisualInsights.prototype.classifiers = function(params, callback) {
37+
var parameters = {
38+
options: {
39+
method: 'GET',
40+
url: '/v1/classifiers',
41+
qs: params,
42+
json: true
43+
},
44+
defaultOptions: this._options
45+
};
46+
return requestFactory(parameters, callback);
47+
};
48+
49+
/**
50+
* Classifies @param images_file using all available classifiers.
51+
*
52+
* @param {ReadStream} images_file The zip of images to analyze.
53+
*/
54+
VisualInsights.prototype.summary = function(params, callback) {
55+
params = params || {};
56+
57+
if (!params.images_file) {
58+
callback(new Error('Missing required parameters: images_file'));
59+
return;
60+
}
61+
62+
if (!isStream(params.images_file)) {
63+
callback(new Error('images_file is not a standard Node.js Stream'));
64+
return;
65+
}
66+
67+
var parameters = {
68+
options: {
69+
method: 'POST',
70+
url: '/v1/summary',
71+
formData: params,
72+
json: true
73+
},
74+
defaultOptions: this._options
75+
};
76+
return requestFactory(parameters, callback);
77+
};
78+
79+
module.exports = VisualInsights;

0 commit comments

Comments
 (0)