Skip to content

Commit 7e4b4f3

Browse files
Merge branch 'dev'
2 parents 04d1775 + 3521beb commit 7e4b4f3

File tree

6 files changed

+293
-71
lines changed

6 files changed

+293
-71
lines changed

examples/visual_recognition.v2-beta.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ var visual_recognition = watson.visual_recognition({
77
//url: 'https://gateway-d.watsonplatform.net/visual-recognition-beta/api',
88
username: 'INSERT YOUR USERNAME FOR THE SERVICE HERE',
99
password: 'INSERT YOUR PASSWORD FOR THE SERVICE HERE',
10-
version: 'v2-beta'
10+
version: 'v2-beta',
11+
version_date: '2015-11-24'
1112
});
1213

1314
var params = {
1415
// must be a .zip file containing images
15-
images_file: fs.createReadStream('./resources/images.zip')
16+
images_file: fs.createReadStream('./resources/car.png')
1617
};
1718

1819
visual_recognition.classify(params, function(err, res) {

lib/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ function createServiceAPI(serviceName) {
102102
if (serviceName === 'message_resonance') {
103103
throw new Error('The message_resonance service is no longer available');
104104
}
105+
if (serviceName === 'question_and_answer') {
106+
console.log('The question_and_answer service is deprecated');
107+
}
108+
105109
if (serviceName === 'tone_analyzer' && path.basename(version) === 'v1') {
106110
throw new Error('tone_analyzer v1 has been replaced by v2-experimental');
107111
}

lib/requestwrapper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ function formatErrorIfExists(cb) {
7777
*/
7878
function createRequest(parameters, callback) {
7979
var missingParams = null,
80-
options = extend({}, parameters.defaultOptions, parameters.options),
80+
options = extend(true, {}, parameters.defaultOptions, parameters.options),
8181
path = options.path,
8282
body = options.body, // application/json or text/plain
8383
form = options.form, // application/x-www-form-urlencoded

services/visual_recognition/v2-beta.js

Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,55 @@
1717
'use strict';
1818

1919
var extend = require('extend');
20-
var requestFactory = require('../../lib/requestwrapper');
2120
var pick = require('object.pick');
21+
var omit = require('object.omit');
2222
var isStream = require('isstream');
23+
var requestFactory = require('../../lib/requestwrapper');
24+
25+
/**
26+
* Verifies that the variable is a valid stream
27+
* @param {Object} value Variable value
28+
* @param {String} name Variable name
29+
*/
30+
function verifyStream(value, name) {
31+
if (!value) {
32+
throw new Error('Missing required parameters: ' + name);
33+
}
34+
35+
if (!isStream(value)) {
36+
throw new Error(name + ' is not a standard Node.js Stream');
37+
}
38+
}
2339

2440
function VisualRecognition(options) {
41+
// Check if 'version_date' was provided
42+
if (typeof options.version_date === 'undefined') {
43+
throw new Error('Argument error: version_date was not specified, use 2015-11-24');
44+
}
45+
2546
// Default URL
2647
var serviceDefaults = {
27-
url: 'https://gateway.watsonplatform.net/visual-recognition-beta/api'
48+
url: 'https://gateway.watsonplatform.net/visual-recognition-beta/api',
49+
qs: {
50+
version: options.version_date
51+
}
2852
};
2953

3054
// Replace default options with user provided
31-
this._options = extend(serviceDefaults, options);
55+
this._options = extend(serviceDefaults, omit(options, ['version_date']));
3256
}
3357

3458
/**
35-
* Returns a classifier
59+
* Retrieves information about a specific classifier.
3660
* @param classifier_id The classifier id
37-
*
3861
*/
3962
VisualRecognition.prototype.getClassifier = function(params, callback) {
4063
var parameters = {
4164
options: {
4265
method: 'GET',
4366
url: '/v2/classifiers/{classifier_id}',
4467
path: params,
45-
json: true,
68+
json: true
4669
},
4770
requiredParams: ['classifier_id'],
4871
defaultOptions: this._options
@@ -51,7 +74,7 @@ VisualRecognition.prototype.getClassifier = function(params, callback) {
5174
};
5275

5376
/**
54-
* Deletes a classifier
77+
* Deletes a custom classifier with the specified classifier id.
5578
* @param classifier_id The classifier id
5679
*
5780
*/
@@ -70,27 +93,23 @@ VisualRecognition.prototype.deleteClassifier = function(params, callback) {
7093
};
7194

7295
/**
73-
* Creates a classifiers
74-
* Train a new classifier on the uploaded image data.
75-
* The upload should be a.zip file with a folder named 'train' and
76-
* another named 'test'.Each of those should have a folder named
77-
* after the name of the desired new classifier, for example 'tiger'.
78-
* train/tiger and test/tiger should contain images(.jpg, .png, .gif files)
79-
* showing tigers.Other folders under train and test will be assumed to
80-
* contain negative examples(leopards, dogs, horses, etc).
96+
* Train a new classifier from example images which are uploaded.
8197
* @param name The desired short name of the new classifier.
82-
* @param images_file A compressed (.zip) file of images.
98+
* @param positive_examples A compressed (.zip) file of images which prominently
99+
* depict the visual subject for a new classifier.
100+
* @param negative_examples A compressed (.zip) file of images which di not
101+
* prominently depict the visual subject for a new
102+
* classifier.
103+
* @param name The desired name of the new classifier.
83104
*/
84105
VisualRecognition.prototype.createClassifier = function(params, callback) {
85106
params = params || {};
86107

87-
if (!params.images_file) {
88-
callback(new Error('Missing required parameters: images_file'));
89-
return;
90-
}
91-
92-
if (!isStream(params.images_file)) {
93-
callback(new Error('images_file is not a standard Node.js Stream'));
108+
try {
109+
verifyStream(params.positive_examples, 'positive_examples');
110+
verifyStream(params.negative_examples, 'negative_examples');
111+
} catch (e) {
112+
callback(e);
94113
return;
95114
}
96115

@@ -99,7 +118,7 @@ VisualRecognition.prototype.createClassifier = function(params, callback) {
99118
url: '/v2/classifiers',
100119
method: 'POST',
101120
json: true,
102-
formData: pick(params, ['name', 'images_file'])
121+
formData: pick(params, ['name', 'positive_examples', 'negative_examples'])
103122
},
104123
requiredParams: ['name'],
105124
defaultOptions: this._options
@@ -108,15 +127,17 @@ VisualRecognition.prototype.createClassifier = function(params, callback) {
108127
};
109128

110129
/**
111-
* Returns the classifiers
112-
*
130+
* Retrieve a list of all classifiers, including built-in and
131+
* user-created classifiers.
132+
* @param verbose If verbose is present and not equal to "0",
133+
* return detailed results for each classifier.
113134
*/
114135
VisualRecognition.prototype.listClassifiers = function(params, callback) {
115136
var parameters = {
116137
options: {
117138
method: 'GET',
118139
url: '/v2/classifiers',
119-
qs: pick(params, ['name']),
140+
qs: pick(params, ['verbose']),
120141
json: true,
121142
},
122143
defaultOptions: this._options
@@ -131,17 +152,18 @@ VisualRecognition.prototype.listClassifiers = function(params, callback) {
131152
* of relevant classifier scores for each image.
132153
*
133154
* @param {ReadStream} images_file The image/s to analyze.
155+
* @param {ReadStream} classifier_ids The ids of the classifier
156+
* to check images against.
157+
* Omit this parameter to use
158+
* all classifiers.
134159
*/
135160
VisualRecognition.prototype.classify = function(params, callback) {
136161
params = params || {};
137162

138-
if (!params.images_file) {
139-
callback(new Error('Missing required parameters: images_file'));
140-
return;
141-
}
142-
143-
if (!isStream(params.images_file)) {
144-
callback(new Error('images_file is not a standard Node.js Stream'));
163+
try {
164+
verifyStream(params.images_file, 'images_file');
165+
} catch (e) {
166+
callback(e);
145167
return;
146168
}
147169

@@ -150,7 +172,7 @@ VisualRecognition.prototype.classify = function(params, callback) {
150172
url: '/v2/classify',
151173
method: 'POST',
152174
json: true,
153-
formData: pick(params, ['images_file'])
175+
formData: pick(params, ['images_file', 'classifier_ids'])
154176
},
155177
defaultOptions: this._options
156178
};

test/test.integration-all-services.js

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -48,29 +48,6 @@ describe('integration-all-services', function() {
4848
});
4949
});
5050

51-
describe('functional_question_and_answer', function() {
52-
this.timeout(FIVE_SECONDS);
53-
var question_and_answer = watson.question_and_answer(auth.question_and_answer);
54-
55-
it('ask()', function(done) {
56-
var params = {
57-
dataset: 'healthcare',
58-
question: {
59-
questionText: 'Why should I take aspirin?',
60-
items: 2, // number of answers
61-
evidenceRequest: {
62-
items: 5 // number of evidences
63-
}
64-
}
65-
};
66-
question_and_answer.ask(params, failIfError.bind(failIfError, done));
67-
});
68-
69-
it('datasets()', function(done) {
70-
question_and_answer.datasets(null, failIfError.bind(failIfError, done));
71-
});
72-
});
73-
7451
describe('functional_tone_analyzer', function() {
7552
this.timeout(TEN_SECONDS);
7653
var tone_analyzer = watson.tone_analyzer(auth.tone_analyzer);
@@ -105,17 +82,38 @@ describe('integration-all-services', function() {
10582
});
10683

10784
describe('functional_visual_recognition', function() {
108-
this.timeout(TWENTY_SECONDS);
109-
var visual_recognition = watson.visual_recognition(auth.visual_recognition);
85+
describe('v1-beta', function() {
86+
this.timeout(TWENTY_SECONDS);
87+
var visual_recognition = watson.visual_recognition(auth.visual_recognition.v1);
88+
89+
it('recognize()', function(done) {
90+
var params = {
91+
image_file: fs.createReadStream(__dirname + '/resources/car.png'),
92+
labels_to_check: JSON.stringify({
93+
label_groups: ['Vehicle']
94+
})
95+
};
96+
visual_recognition.recognize(params, failIfError.bind(failIfError, done));
97+
});
98+
});
99+
describe('v2-beta', function() {
100+
this.timeout(TWENTY_SECONDS);
101+
var visual_recognition = watson.visual_recognition(auth.visual_recognition.v2);
110102

111-
it('recognize()', function(done) {
112-
var params = {
113-
image_file: fs.createReadStream(__dirname + '/resources/car.png'),
114-
labels_to_check: JSON.stringify({
115-
label_groups: ['Vehicle']
116-
})
117-
};
118-
visual_recognition.recognize(params, failIfError.bind(failIfError, done));
103+
it('getClassifier()', function(done) {
104+
visual_recognition.getClassifier({classifier_id: 'Black'}, failIfError.bind(failIfError, done));
105+
});
106+
107+
it('listClassifiers()', function(done) {
108+
visual_recognition.listClassifiers({}, failIfError.bind(failIfError, done));
109+
});
110+
111+
it('classify()', function(done) {
112+
var params = {
113+
images_file: fs.createReadStream(__dirname + '/resources/car.png'),
114+
};
115+
visual_recognition.listClassifiers(params, failIfError.bind(failIfError, done));
116+
});
119117
});
120118
});
121119

0 commit comments

Comments
 (0)