From 681f9f89c199da7d0db3c216ce273ff785b04637 Mon Sep 17 00:00:00 2001 From: German Attanasio Date: Wed, 27 May 2020 16:22:50 -0400 Subject: [PATCH] chore(visual-recognition): manual changes feat(visual-recognition): Add integration test --- .../speech_to_text/v1/SpeechToText.java | 2 +- .../v4/VisualRecognition.java | 37 ++++ .../v4/model/GetModelFileOptions.java | 159 ++++++++++++++++++ .../v4/model/ObjectTrainingStatus.java | 32 ++++ .../v4/VisualRecognitionIT.java | 17 +- 5 files changed, 244 insertions(+), 3 deletions(-) create mode 100644 visual-recognition/src/main/java/com/ibm/watson/visual_recognition/v4/model/GetModelFileOptions.java diff --git a/speech-to-text/src/main/java/com/ibm/watson/speech_to_text/v1/SpeechToText.java b/speech-to-text/src/main/java/com/ibm/watson/speech_to_text/v1/SpeechToText.java index a7e00e9a351..cec94a03cf3 100644 --- a/speech-to-text/src/main/java/com/ibm/watson/speech_to_text/v1/SpeechToText.java +++ b/speech-to-text/src/main/java/com/ibm/watson/speech_to_text/v1/SpeechToText.java @@ -2431,4 +2431,4 @@ public ServiceCall deleteUserData(DeleteUserDataOptions deleteUserDataOpti ResponseConverter responseConverter = ResponseConverterUtils.getVoid(); return createServiceCall(builder.build(), responseConverter); } -} \ No newline at end of file +} diff --git a/visual-recognition/src/main/java/com/ibm/watson/visual_recognition/v4/VisualRecognition.java b/visual-recognition/src/main/java/com/ibm/watson/visual_recognition/v4/VisualRecognition.java index 7b716122a10..9d79ca7d0cd 100644 --- a/visual-recognition/src/main/java/com/ibm/watson/visual_recognition/v4/VisualRecognition.java +++ b/visual-recognition/src/main/java/com/ibm/watson/visual_recognition/v4/VisualRecognition.java @@ -37,6 +37,7 @@ import com.ibm.watson.visual_recognition.v4.model.GetCollectionOptions; import com.ibm.watson.visual_recognition.v4.model.GetImageDetailsOptions; import com.ibm.watson.visual_recognition.v4.model.GetJpegImageOptions; +import com.ibm.watson.visual_recognition.v4.model.GetModelFileOptions; import com.ibm.watson.visual_recognition.v4.model.GetObjectMetadataOptions; import com.ibm.watson.visual_recognition.v4.model.GetTrainingUsageOptions; import com.ibm.watson.visual_recognition.v4.model.ImageDetails; @@ -384,6 +385,42 @@ public ServiceCall deleteCollection(DeleteCollectionOptions deleteCollecti return createServiceCall(builder.build(), responseConverter); } + /** + * Get a model. + * + *

Download a model that you can deploy to detect objects in images. The collection must + * include a generated model, which is indicated in the response for the collection details as + * `"rscnn_ready": true`. If the value is `false`, train or retrain the collection to generate the + * model. + * + *

Currently, the model format is specific to Android apps. For more information about how to + * deploy the model to your app, see the [Watson Visual Recognition on + * Android](https://github.com/matt-ny/rscnn) project in GitHub. + * + * @param getModelFileOptions the {@link GetModelFileOptions} containing the options for the call + * @return a {@link ServiceCall} with a response type of {@link InputStream} + */ + public ServiceCall getModelFile(GetModelFileOptions getModelFileOptions) { + com.ibm.cloud.sdk.core.util.Validator.notNull( + getModelFileOptions, "getModelFileOptions cannot be null"); + String[] pathSegments = {"v4/collections", "model"}; + String[] pathParameters = {getModelFileOptions.collectionId()}; + RequestBuilder builder = + RequestBuilder.get( + RequestBuilder.constructHttpUrl(getServiceUrl(), pathSegments, pathParameters)); + builder.query("version", versionDate); + Map sdkHeaders = + SdkCommon.getSdkHeaders("watson_vision_combined", "v4", "getModelFile"); + for (Entry header : sdkHeaders.entrySet()) { + builder.header(header.getKey(), header.getValue()); + } + builder.header("Accept", "application/octet-stream"); + builder.query("feature", getModelFileOptions.feature()); + builder.query("model_format", getModelFileOptions.modelFormat()); + ResponseConverter responseConverter = ResponseConverterUtils.getInputStream(); + return createServiceCall(builder.build(), responseConverter); + } + /** * Add images. * diff --git a/visual-recognition/src/main/java/com/ibm/watson/visual_recognition/v4/model/GetModelFileOptions.java b/visual-recognition/src/main/java/com/ibm/watson/visual_recognition/v4/model/GetModelFileOptions.java new file mode 100644 index 00000000000..e968fe85063 --- /dev/null +++ b/visual-recognition/src/main/java/com/ibm/watson/visual_recognition/v4/model/GetModelFileOptions.java @@ -0,0 +1,159 @@ +/* + * (C) Copyright IBM Corp. 2020. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package com.ibm.watson.visual_recognition.v4.model; + +import com.ibm.cloud.sdk.core.service.model.GenericModel; + +/** The getModelFile options. */ +public class GetModelFileOptions extends GenericModel { + + /** The feature for the model. */ + public interface Feature { + /** objects. */ + String OBJECTS = "objects"; + } + + /** The format of the returned model. */ + public interface ModelFormat { + /** rscnn. */ + String RSCNN = "rscnn"; + } + + protected String collectionId; + protected String feature; + protected String modelFormat; + + /** Builder. */ + public static class Builder { + private String collectionId; + private String feature; + private String modelFormat; + + private Builder(GetModelFileOptions getModelFileOptions) { + this.collectionId = getModelFileOptions.collectionId; + this.feature = getModelFileOptions.feature; + this.modelFormat = getModelFileOptions.modelFormat; + } + + /** Instantiates a new builder. */ + public Builder() {} + + /** + * Instantiates a new builder with required properties. + * + * @param collectionId the collectionId + * @param feature the feature + * @param modelFormat the modelFormat + */ + public Builder(String collectionId, String feature, String modelFormat) { + this.collectionId = collectionId; + this.feature = feature; + this.modelFormat = modelFormat; + } + + /** + * Builds a GetModelFileOptions. + * + * @return the getModelFileOptions + */ + public GetModelFileOptions build() { + return new GetModelFileOptions(this); + } + + /** + * Set the collectionId. + * + * @param collectionId the collectionId + * @return the GetModelFileOptions builder + */ + public Builder collectionId(String collectionId) { + this.collectionId = collectionId; + return this; + } + + /** + * Set the feature. + * + * @param feature the feature + * @return the GetModelFileOptions builder + */ + public Builder feature(String feature) { + this.feature = feature; + return this; + } + + /** + * Set the modelFormat. + * + * @param modelFormat the modelFormat + * @return the GetModelFileOptions builder + */ + public Builder modelFormat(String modelFormat) { + this.modelFormat = modelFormat; + return this; + } + } + + protected GetModelFileOptions(Builder builder) { + com.ibm.cloud.sdk.core.util.Validator.notEmpty( + builder.collectionId, "collectionId cannot be empty"); + com.ibm.cloud.sdk.core.util.Validator.notNull(builder.feature, "feature cannot be null"); + com.ibm.cloud.sdk.core.util.Validator.notNull( + builder.modelFormat, "modelFormat cannot be null"); + collectionId = builder.collectionId; + feature = builder.feature; + modelFormat = builder.modelFormat; + } + + /** + * New builder. + * + * @return a GetModelFileOptions builder + */ + public Builder newBuilder() { + return new Builder(this); + } + + /** + * Gets the collectionId. + * + *

The identifier of the collection. + * + * @return the collectionId + */ + public String collectionId() { + return collectionId; + } + + /** + * Gets the feature. + * + *

The feature for the model. + * + * @return the feature + */ + public String feature() { + return feature; + } + + /** + * Gets the modelFormat. + * + *

The format of the returned model. + * + * @return the modelFormat + */ + public String modelFormat() { + return modelFormat; + } +} diff --git a/visual-recognition/src/main/java/com/ibm/watson/visual_recognition/v4/model/ObjectTrainingStatus.java b/visual-recognition/src/main/java/com/ibm/watson/visual_recognition/v4/model/ObjectTrainingStatus.java index 09dde59e566..f11ed205321 100644 --- a/visual-recognition/src/main/java/com/ibm/watson/visual_recognition/v4/model/ObjectTrainingStatus.java +++ b/visual-recognition/src/main/java/com/ibm/watson/visual_recognition/v4/model/ObjectTrainingStatus.java @@ -29,6 +29,9 @@ public class ObjectTrainingStatus extends GenericModel { @SerializedName("latest_failed") protected Boolean latestFailed; + @SerializedName("rscnn_ready") + protected Boolean rscnnReady; + protected String description; /** Builder. */ @@ -37,6 +40,7 @@ public static class Builder { private Boolean inProgress; private Boolean dataChanged; private Boolean latestFailed; + private Boolean rscnnReady; private String description; private Builder(ObjectTrainingStatus objectTrainingStatus) { @@ -44,6 +48,7 @@ private Builder(ObjectTrainingStatus objectTrainingStatus) { this.inProgress = objectTrainingStatus.inProgress; this.dataChanged = objectTrainingStatus.dataChanged; this.latestFailed = objectTrainingStatus.latestFailed; + this.rscnnReady = objectTrainingStatus.rscnnReady; this.description = objectTrainingStatus.description; } @@ -57,6 +62,7 @@ public Builder() {} * @param inProgress the inProgress * @param dataChanged the dataChanged * @param latestFailed the latestFailed + * @param rscnnReady the rscnnReady * @param description the description */ public Builder( @@ -64,11 +70,13 @@ public Builder( Boolean inProgress, Boolean dataChanged, Boolean latestFailed, + Boolean rscnnReady, String description) { this.ready = ready; this.inProgress = inProgress; this.dataChanged = dataChanged; this.latestFailed = latestFailed; + this.rscnnReady = rscnnReady; this.description = description; } @@ -125,6 +133,17 @@ public Builder latestFailed(Boolean latestFailed) { return this; } + /** + * Set the rscnnReady. + * + * @param rscnnReady the rscnnReady + * @return the ObjectTrainingStatus builder + */ + public Builder rscnnReady(Boolean rscnnReady) { + this.rscnnReady = rscnnReady; + return this; + } + /** * Set the description. * @@ -144,12 +163,14 @@ protected ObjectTrainingStatus(Builder builder) { builder.dataChanged, "dataChanged cannot be null"); com.ibm.cloud.sdk.core.util.Validator.notNull( builder.latestFailed, "latestFailed cannot be null"); + com.ibm.cloud.sdk.core.util.Validator.notNull(builder.rscnnReady, "rscnnReady cannot be null"); com.ibm.cloud.sdk.core.util.Validator.notNull( builder.description, "description cannot be null"); ready = builder.ready; inProgress = builder.inProgress; dataChanged = builder.dataChanged; latestFailed = builder.latestFailed; + rscnnReady = builder.rscnnReady; description = builder.description; } @@ -206,6 +227,17 @@ public Boolean latestFailed() { return latestFailed; } + /** + * Gets the rscnnReady. + * + *

Whether the model can be downloaded after the training status is `ready`. + * + * @return the rscnnReady + */ + public Boolean rscnnReady() { + return rscnnReady; + } + /** * Gets the description. * diff --git a/visual-recognition/src/test/java/com/ibm/watson/visual_recognition/v4/VisualRecognitionIT.java b/visual-recognition/src/test/java/com/ibm/watson/visual_recognition/v4/VisualRecognitionIT.java index 19be3093a1e..5cf1920d831 100644 --- a/visual-recognition/src/test/java/com/ibm/watson/visual_recognition/v4/VisualRecognitionIT.java +++ b/visual-recognition/src/test/java/com/ibm/watson/visual_recognition/v4/VisualRecognitionIT.java @@ -24,6 +24,7 @@ import com.ibm.watson.visual_recognition.v4.model.GetCollectionOptions; import com.ibm.watson.visual_recognition.v4.model.GetImageDetailsOptions; import com.ibm.watson.visual_recognition.v4.model.GetJpegImageOptions; +import com.ibm.watson.visual_recognition.v4.model.GetModelFileOptions; import com.ibm.watson.visual_recognition.v4.model.GetTrainingUsageOptions; import com.ibm.watson.visual_recognition.v4.model.ImageDetails; import com.ibm.watson.visual_recognition.v4.model.ImageDetailsList; @@ -59,8 +60,7 @@ public class VisualRecognitionIT extends WatsonServiceTest { private static final String VERSION = "2019-02-11"; private static final String RESOURCE = "src/test/resources/visual_recognition/v4/"; - - private static final String COLLECTION_ID = "10e96193-0e08-406b-b8fb-b5b9ea9fe99a"; + private static final String COLLECTION_ID = "a06f7036-0529-49ee-bdf6-82ddec276923"; private static final String GIRAFFE_CLASSNAME = "giraffe"; private static final String SINGLE_GIRAFFE_IMAGE_PATH = RESOURCE + "giraffe_to_classify.jpg"; private static final String GIRAFFE_POSITIVE_EXAMPLES_PATH = @@ -380,6 +380,19 @@ public void testTrainingOperations() throws FileNotFoundException { } } + @Test + public void testGetModelFile() { + GetModelFileOptions options = + new GetModelFileOptions.Builder() + .collectionId(COLLECTION_ID) + .feature("objects") + .modelFormat("rscnn") + .build(); + int statusCode = service.getModelFile(options).execute().getStatusCode(); + + assertEquals(200, statusCode); + } + @Test public void testDeleteUserData() { DeleteUserDataOptions deleteUserDataOptions =