Search in sources :

Example 1 with AddImageTrainingDataOptions

use of com.ibm.watson.visual_recognition.v4.model.AddImageTrainingDataOptions in project java-sdk by watson-developer-cloud.

the class VisualRecognitionIT method testTrainingOperations.

/**
 * Test training operations.
 *
 * @throws FileNotFoundException the file not found exception
 */
@Test
public void testTrainingOperations() throws FileNotFoundException {
    String testCollectionId = createTestCollection();
    // start by adding images for training
    FileWithMetadata giraffeFileZip = new FileWithMetadata.Builder().data(new File(GIRAFFE_POSITIVE_EXAMPLES_PATH)).contentType(HttpMediaType.APPLICATION_ZIP).build();
    AddImagesOptions addImagesOptions = new AddImagesOptions.Builder().addImagesFile(giraffeFileZip).collectionId(testCollectionId).build();
    ImageDetailsList imageDetailsList = service.addImages(addImagesOptions).execute().getResult();
    String imageIdForTraining = null;
    Set<String> addedImageIds = new HashSet<>();
    for (ImageDetails imageDetails : imageDetailsList.getImages()) {
        addedImageIds.add(imageDetails.getImageId());
        if (imageIdForTraining == null) {
            imageIdForTraining = imageDetails.getImageId();
        }
    }
    try {
        Long top = 64L;
        Long left = 270L;
        Long width = 755L;
        Long height = 784L;
        Location testLocation = new Location.Builder().top(top).left(left).width(width).height(height).build();
        TrainingDataObject trainingDataObject = new TrainingDataObject.Builder().object(GIRAFFE_CLASSNAME).location(testLocation).build();
        // test adding training data
        AddImageTrainingDataOptions addTrainingDataOptions = new AddImageTrainingDataOptions.Builder().collectionId(testCollectionId).addObjects(trainingDataObject).imageId(imageIdForTraining).build();
        TrainingDataObjects trainingDataObjects = service.addImageTrainingData(addTrainingDataOptions).execute().getResult();
        assertNotNull(trainingDataObjects);
        assertEquals(GIRAFFE_CLASSNAME, trainingDataObjects.getObjects().get(0).object());
        assertEquals(top, trainingDataObjects.getObjects().get(0).location().top());
        assertEquals(left, trainingDataObjects.getObjects().get(0).location().left());
        assertEquals(width, trainingDataObjects.getObjects().get(0).location().width());
        assertEquals(height, trainingDataObjects.getObjects().get(0).location().height());
        // test train
        TrainOptions trainOptions = new TrainOptions.Builder().collectionId(testCollectionId).build();
        Collection trainingCollection = service.train(trainOptions).execute().getResult();
        assertNotNull(trainingCollection);
        assertTrue(trainingCollection.getTrainingStatus().getObjects().inProgress() || trainingCollection.getTrainingStatus().getObjects().ready());
    } finally {
        // delete images we added earlier
        for (String imageId : addedImageIds) {
            DeleteImageOptions deleteImageOptions = new DeleteImageOptions.Builder().collectionId(testCollectionId).imageId(imageId).build();
            service.deleteImage(deleteImageOptions).execute();
        }
        deleteTestCollection(testCollectionId);
    }
}
Also used : DeleteImageOptions(com.ibm.watson.visual_recognition.v4.model.DeleteImageOptions) TrainingDataObject(com.ibm.watson.visual_recognition.v4.model.TrainingDataObject) ImageDetails(com.ibm.watson.visual_recognition.v4.model.ImageDetails) TrainOptions(com.ibm.watson.visual_recognition.v4.model.TrainOptions) AddImageTrainingDataOptions(com.ibm.watson.visual_recognition.v4.model.AddImageTrainingDataOptions) AddImagesOptions(com.ibm.watson.visual_recognition.v4.model.AddImagesOptions) TrainingDataObjects(com.ibm.watson.visual_recognition.v4.model.TrainingDataObjects) Collection(com.ibm.watson.visual_recognition.v4.model.Collection) FileWithMetadata(com.ibm.cloud.sdk.core.service.model.FileWithMetadata) File(java.io.File) ImageDetailsList(com.ibm.watson.visual_recognition.v4.model.ImageDetailsList) Location(com.ibm.watson.visual_recognition.v4.model.Location) WatsonServiceTest(com.ibm.watson.common.WatsonServiceTest) Test(org.junit.Test)

Example 2 with AddImageTrainingDataOptions

use of com.ibm.watson.visual_recognition.v4.model.AddImageTrainingDataOptions in project java-sdk by watson-developer-cloud.

the class VisualRecognitionTest method testAddImageTrainingDataWOptions.

@Test
public void testAddImageTrainingDataWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "{\"objects\": [{\"object\": \"object\", \"location\": {\"top\": 3, \"left\": 4, \"width\": 5, \"height\": 6}}]}";
    String addImageTrainingDataPath = "/v4/collections/testString/images/testString/training_data";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the Location model
    Location locationModel = new Location.Builder().top(Long.valueOf("26")).left(Long.valueOf("26")).width(Long.valueOf("26")).height(Long.valueOf("26")).build();
    // Construct an instance of the TrainingDataObject model
    TrainingDataObject trainingDataObjectModel = new TrainingDataObject.Builder().object("testString").location(locationModel).build();
    // Construct an instance of the AddImageTrainingDataOptions model
    AddImageTrainingDataOptions addImageTrainingDataOptionsModel = new AddImageTrainingDataOptions.Builder().collectionId("testString").imageId("testString").objects(new java.util.ArrayList<TrainingDataObject>(java.util.Arrays.asList(trainingDataObjectModel))).build();
    // Invoke operation with valid options model (positive test)
    Response<TrainingDataObjects> response = visualRecognitionService.addImageTrainingData(addImageTrainingDataOptionsModel).execute();
    assertNotNull(response);
    TrainingDataObjects responseObj = response.getResult();
    assertNotNull(responseObj);
    // Verify the contents of the request
    RecordedRequest request = server.takeRequest();
    assertNotNull(request);
    assertEquals(request.getMethod(), "POST");
    // Check query
    Map<String, String> query = TestUtilities.parseQueryString(request);
    assertNotNull(query);
    // Get query params
    assertEquals(query.get("version"), "testString");
    // Check request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, addImageTrainingDataPath);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) TrainingDataObject(com.ibm.watson.visual_recognition.v4.model.TrainingDataObject) AddImageTrainingDataOptions(com.ibm.watson.visual_recognition.v4.model.AddImageTrainingDataOptions) TrainingDataObjects(com.ibm.watson.visual_recognition.v4.model.TrainingDataObjects) Location(com.ibm.watson.visual_recognition.v4.model.Location) Test(org.testng.annotations.Test)

Example 3 with AddImageTrainingDataOptions

use of com.ibm.watson.visual_recognition.v4.model.AddImageTrainingDataOptions in project java-sdk by watson-developer-cloud.

the class VisualRecognition method addImageTrainingData.

/**
 * Add training data to an image.
 *
 * <p>Add, update, or delete training data for an image. Encode the object name in UTF-8 if it
 * contains non-ASCII characters. The service assumes UTF-8 encoding if it encounters non-ASCII
 * characters.
 *
 * <p>Elements in the request replace the existing elements.
 *
 * <p>- To update the training data, provide both the unchanged and the new or changed values.
 *
 * <p>- To delete the training data, provide an empty value for the training data.
 *
 * @param addImageTrainingDataOptions the {@link AddImageTrainingDataOptions} containing the
 *     options for the call
 * @return a {@link ServiceCall} with a result of type {@link TrainingDataObjects}
 */
public ServiceCall<TrainingDataObjects> addImageTrainingData(AddImageTrainingDataOptions addImageTrainingDataOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(addImageTrainingDataOptions, "addImageTrainingDataOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("collection_id", addImageTrainingDataOptions.collectionId());
    pathParamsMap.put("image_id", addImageTrainingDataOptions.imageId());
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v4/collections/{collection_id}/images/{image_id}/training_data", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("watson_vision_combined", "v4", "addImageTrainingData");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    builder.query("version", String.valueOf(this.version));
    final JsonObject contentJson = new JsonObject();
    if (addImageTrainingDataOptions.objects() != null) {
        contentJson.add("objects", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(addImageTrainingDataOptions.objects()));
    }
    builder.bodyJson(contentJson);
    ResponseConverter<TrainingDataObjects> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<TrainingDataObjects>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) JsonObject(com.google.gson.JsonObject) TrainingDataObjects(com.ibm.watson.visual_recognition.v4.model.TrainingDataObjects)

Aggregations

TrainingDataObjects (com.ibm.watson.visual_recognition.v4.model.TrainingDataObjects)3 AddImageTrainingDataOptions (com.ibm.watson.visual_recognition.v4.model.AddImageTrainingDataOptions)2 Location (com.ibm.watson.visual_recognition.v4.model.Location)2 TrainingDataObject (com.ibm.watson.visual_recognition.v4.model.TrainingDataObject)2 JsonObject (com.google.gson.JsonObject)1 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)1 FileWithMetadata (com.ibm.cloud.sdk.core.service.model.FileWithMetadata)1 WatsonServiceTest (com.ibm.watson.common.WatsonServiceTest)1 AddImagesOptions (com.ibm.watson.visual_recognition.v4.model.AddImagesOptions)1 Collection (com.ibm.watson.visual_recognition.v4.model.Collection)1 DeleteImageOptions (com.ibm.watson.visual_recognition.v4.model.DeleteImageOptions)1 ImageDetails (com.ibm.watson.visual_recognition.v4.model.ImageDetails)1 ImageDetailsList (com.ibm.watson.visual_recognition.v4.model.ImageDetailsList)1 TrainOptions (com.ibm.watson.visual_recognition.v4.model.TrainOptions)1 File (java.io.File)1 HashMap (java.util.HashMap)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)1 Test (org.junit.Test)1 Test (org.testng.annotations.Test)1