3232import com .ibm .watson .developer_cloud .visual_recognition .v3 .model .GetCoreMlModelOptions ;
3333import com .ibm .watson .developer_cloud .visual_recognition .v3 .model .ListClassifiersOptions ;
3434import com .ibm .watson .developer_cloud .visual_recognition .v3 .model .UpdateClassifierOptions ;
35+ import java .io .InputStream ;
3536import okhttp3 .MultipartBody ;
3637import okhttp3 .RequestBody ;
3738
38- import java .io .File ;
39- import java .io .InputStream ;
40-
4139/**
4240 * The IBM Watson™ Visual Recognition service uses deep learning algorithms to identify scenes, objects, and faces
4341 * in images you upload to the service. You can create and train a custom classifier to identify subjects that suit
@@ -70,52 +68,6 @@ public VisualRecognition(String versionDate) {
7068 this .versionDate = versionDate ;
7169 }
7270
73- /**
74- * Instantiates a new `VisualRecognition` with API Key.
75- *
76- * @param versionDate The version date (yyyy-MM-dd) of the REST API to use. Specifying this value will keep your API
77- * calls from failing when the service introduces breaking changes.
78- * @param apiKey the API Key
79- * @deprecated This form of authentication is deprecated and will be removed in the next major release. Please
80- * authenticate using IAM credentials, using either the (String, IamOptions) constructor or with the
81- * setIamCredentials() method.
82- */
83- public VisualRecognition (String versionDate , String apiKey ) {
84- this (versionDate );
85- setApiKey (apiKey );
86- }
87-
88- /*
89- * (non-Javadoc)
90- */
91- @ Override
92- protected void setAuthentication (okhttp3 .Request .Builder builder ) {
93- if ((getUsername () != null && getPassword () != null ) || isTokenManagerSet ()) {
94- super .setAuthentication (builder );
95- } else if (getApiKey () != null ) {
96- addApiKeyQueryParameter (builder , getApiKey ());
97- } else {
98- throw new IllegalArgumentException (
99- "Credentials need to be specified. Use setApiKey(), setIamCredentials(), or setUsernameAndPassword()." );
100- }
101- }
102-
103- /**
104- * Adds the API key as a query parameter to the request URL.
105- *
106- * @param builder builder for the current request
107- * @param apiKey API key to be added
108- */
109- private void addApiKeyQueryParameter (okhttp3 .Request .Builder builder , String apiKey ) {
110- final okhttp3 .HttpUrl url = okhttp3 .HttpUrl .parse (builder .build ().url ().toString ());
111-
112- if ((url .query () == null ) || url .query ().isEmpty ()) {
113- builder .url (builder .build ().url () + "?api_key=" + apiKey );
114- } else {
115- builder .url (builder .build ().url () + "&api_key=" + apiKey );
116- }
117- }
118-
11971 /**
12072 * Instantiates a new `VisualRecognition` with IAM. Note that if the access token is specified in the
12173 * iamOptions, you accept responsibility for managing the access token yourself. You must set a new access token
@@ -142,13 +94,9 @@ public VisualRecognition(String versionDate, IamOptions iamOptions) {
14294 */
14395 public ServiceCall <ClassifiedImages > classify (ClassifyOptions classifyOptions ) {
14496 Validator .notNull (classifyOptions , "classifyOptions cannot be null" );
145- Validator .isTrue ((classifyOptions .imagesFile () != null )
146- || (classifyOptions .url () != null )
147- || (classifyOptions .threshold () != null )
148- || (classifyOptions .owners () != null )
149- || (classifyOptions .classifierIds () != null )
150- || (classifyOptions .parameters () != null ),
151- "At least one of imagesFile, url, threshold, owners, classifierIds, or parameters must be supplied." );
97+ Validator .isTrue ((classifyOptions .imagesFile () != null ) || (classifyOptions .url () != null ) || (classifyOptions
98+ .threshold () != null ) || (classifyOptions .owners () != null ) || (classifyOptions .classifierIds () != null ),
99+ "At least one of imagesFile, url, threshold, owners, or classifierIds must be supplied." );
152100 String [] pathSegments = { "v3/classify" };
153101 RequestBuilder builder = RequestBuilder .post (RequestBuilder .constructHttpUrl (getEndPoint (), pathSegments ));
154102 builder .query (VERSION , versionDate );
@@ -162,9 +110,6 @@ public ServiceCall<ClassifiedImages> classify(ClassifyOptions classifyOptions) {
162110 .imagesFileContentType ());
163111 multipartBuilder .addFormDataPart ("images_file" , classifyOptions .imagesFilename (), imagesFileBody );
164112 }
165- if (classifyOptions .parameters () != null ) {
166- multipartBuilder .addFormDataPart ("parameters" , classifyOptions .parameters ());
167- }
168113 if (classifyOptions .url () != null ) {
169114 multipartBuilder .addFormDataPart ("url" , classifyOptions .url ());
170115 }
@@ -212,10 +157,8 @@ public ServiceCall<ClassifiedImages> classify() {
212157 */
213158 public ServiceCall <DetectedFaces > detectFaces (DetectFacesOptions detectFacesOptions ) {
214159 Validator .notNull (detectFacesOptions , "detectFacesOptions cannot be null" );
215- Validator .isTrue ((detectFacesOptions .imagesFile () != null )
216- || (detectFacesOptions .url () != null )
217- || (detectFacesOptions .parameters () != null ),
218- "At least one of imagesFile, url, or parameters must be supplied." );
160+ Validator .isTrue ((detectFacesOptions .imagesFile () != null ) || (detectFacesOptions .url () != null ),
161+ "At least one of imagesFile or url must be supplied." );
219162 String [] pathSegments = { "v3/detect_faces" };
220163 RequestBuilder builder = RequestBuilder .post (RequestBuilder .constructHttpUrl (getEndPoint (), pathSegments ));
221164 builder .query (VERSION , versionDate );
@@ -226,9 +169,6 @@ public ServiceCall<DetectedFaces> detectFaces(DetectFacesOptions detectFacesOpti
226169 .imagesFileContentType ());
227170 multipartBuilder .addFormDataPart ("images_file" , detectFacesOptions .imagesFilename (), imagesFileBody );
228171 }
229- if (detectFacesOptions .parameters () != null ) {
230- multipartBuilder .addFormDataPart ("parameters" , detectFacesOptions .parameters ());
231- }
232172 if (detectFacesOptions .url () != null ) {
233173 multipartBuilder .addFormDataPart ("url" , detectFacesOptions .url ());
234174 }
@@ -278,12 +218,12 @@ public ServiceCall<Classifier> createClassifier(CreateClassifierOptions createCl
278218 MultipartBody .Builder multipartBuilder = new MultipartBody .Builder ();
279219 multipartBuilder .setType (MultipartBody .FORM );
280220 multipartBuilder .addFormDataPart ("name" , createClassifierOptions .name ());
281- // Classes
282- for ( String className : createClassifierOptions . classNames ()) {
283- String dataName = className + "_positive_examples" ;
284- File positiveExamples = createClassifierOptions . positiveExamplesByClassName ( className ) ;
285- RequestBody body = RequestUtils .fileBody ( positiveExamples , "application/octet-stream" );
286- multipartBuilder .addFormDataPart (dataName , positiveExamples . getName (), body );
221+ for ( Map . Entry < String , InputStream > entry : createClassifierOptions . positiveExamples (). entrySet ()) {
222+ String partName = String . format ( "%s_positive_examples" , entry . getKey ());
223+ String fileName = createClassifierOptions . positiveExamplesFilename () != null ? createClassifierOptions
224+ . positiveExamplesFilename (). get ( entry . getKey ()) : null ;
225+ RequestBody part = RequestUtils .inputStreamBody ( entry . getValue () , "application/octet-stream" );
226+ multipartBuilder .addFormDataPart (partName , fileName , part );
287227 }
288228 if (createClassifierOptions .negativeExamples () != null ) {
289229 RequestBody negativeExamplesBody = RequestUtils .inputStreamBody (createClassifierOptions .negativeExamples (),
@@ -362,8 +302,7 @@ public ServiceCall<Classifiers> listClassifiers() {
362302 * Update a custom classifier by adding new positive or negative classes (examples) or by adding new images to
363303 * existing classes. You must supply at least one set of positive or negative examples. For details, see [Updating
364304 * custom
365- * classifiers]
366- * (https://console.bluemix.net/docs/services/visual-recognition/customizing.html#updating-custom-classifiers).
305+ * classifiers](https://console.bluemix.net/docs/services/visual-recognition/customizing.html#updating-custom-classifiers).
367306 *
368307 * Encode all names in UTF-8 if they contain non-ASCII characters (.zip and image file names, and classifier and class
369308 * names). The service assumes UTF-8 encoding if it encounters non-ASCII characters.
@@ -377,22 +316,23 @@ public ServiceCall<Classifiers> listClassifiers() {
377316 */
378317 public ServiceCall <Classifier > updateClassifier (UpdateClassifierOptions updateClassifierOptions ) {
379318 Validator .notNull (updateClassifierOptions , "updateClassifierOptions cannot be null" );
380- Validator .isTrue ((updateClassifierOptions .classNames ().size () > 0 ) || (updateClassifierOptions
381- .negativeExamples () != null ),
382- "At least one of classnamePositiveExamples or negativeExamples must be supplied." );
319+ Validator .isTrue ((updateClassifierOptions .positiveExamples () != null ) || (updateClassifierOptions
320+ .negativeExamples () != null ), "At least one of positiveExamples or negativeExamples must be supplied." );
383321 String [] pathSegments = { "v3/classifiers" };
384322 String [] pathParameters = { updateClassifierOptions .classifierId () };
385323 RequestBuilder builder = RequestBuilder .post (RequestBuilder .constructHttpUrl (getEndPoint (), pathSegments ,
386324 pathParameters ));
387325 builder .query (VERSION , versionDate );
388326 MultipartBody .Builder multipartBuilder = new MultipartBody .Builder ();
389327 multipartBuilder .setType (MultipartBody .FORM );
390- // Classes
391- for (String className : updateClassifierOptions .classNames ()) {
392- String dataName = className + "_positive_examples" ;
393- File positiveExamples = updateClassifierOptions .positiveExamplesByClassName (className );
394- RequestBody body = RequestUtils .fileBody (positiveExamples , "application/octet-stream" );
395- multipartBuilder .addFormDataPart (dataName , positiveExamples .getName (), body );
328+ if (updateClassifierOptions .positiveExamples () != null ) {
329+ for (Map .Entry <String , InputStream > entry : updateClassifierOptions .positiveExamples ().entrySet ()) {
330+ String partName = String .format ("%s_positive_examples" , entry .getKey ());
331+ String fileName = updateClassifierOptions .positiveExamplesFilename () != null ? updateClassifierOptions
332+ .positiveExamplesFilename ().get (entry .getKey ()) : null ;
333+ RequestBody part = RequestUtils .inputStreamBody (entry .getValue (), "application/octet-stream" );
334+ multipartBuilder .addFormDataPart (partName , fileName , part );
335+ }
396336 }
397337 if (updateClassifierOptions .negativeExamples () != null ) {
398338 RequestBody negativeExamplesBody = RequestUtils .inputStreamBody (updateClassifierOptions .negativeExamples (),
@@ -420,7 +360,7 @@ public ServiceCall<InputStream> getCoreMlModel(GetCoreMlModelOptions getCoreMlMo
420360 RequestBuilder builder = RequestBuilder .get (RequestBuilder .constructHttpUrl (getEndPoint (), pathSegments ,
421361 pathParameters ));
422362 builder .query (VERSION , versionDate );
423- return createServiceCall (builder .build (), ResponseConverterUtils .getInputStream ( ));
363+ return createServiceCall (builder .build (), ResponseConverterUtils .getObject ( InputStream . class ));
424364 }
425365
426366 /**
0 commit comments