1717'use strict' ;
1818
1919var extend = require ( 'extend' ) ;
20- var requestFactory = require ( '../../lib/requestwrapper' ) ;
2120var pick = require ( 'object.pick' ) ;
21+ var omit = require ( 'object.omit' ) ;
2222var 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
2440function 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 */
3962VisualRecognition . 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 */
84105VisualRecognition . 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 */
114135VisualRecognition . 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 */
135160VisualRecognition . 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 } ;
0 commit comments