Search in sources :

Example 11 with Collection

use of li.naska.bgg.resource.v3.model.Collection in project java-sdk by watson-developer-cloud.

the class Discovery method getCollection.

/**
 * Get collection details.
 *
 * @param getCollectionOptions the {@link GetCollectionOptions} containing the options for the call
 * @return a {@link ServiceCall} with a response type of {@link Collection}
 */
public ServiceCall<Collection> getCollection(GetCollectionOptions getCollectionOptions) {
    Validator.notNull(getCollectionOptions, "getCollectionOptions cannot be null");
    String[] pathSegments = { "v1/environments", "collections" };
    String[] pathParameters = { getCollectionOptions.environmentId(), getCollectionOptions.collectionId() };
    RequestBuilder builder = RequestBuilder.get(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
    builder.query(VERSION, versionDate);
    return createServiceCall(builder.build(), ResponseConverterUtils.getObject(Collection.class));
}
Also used : RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) Collection(com.ibm.watson.developer_cloud.discovery.v1.model.Collection)

Example 12 with Collection

use of li.naska.bgg.resource.v3.model.Collection in project java-sdk by watson-developer-cloud.

the class Discovery method updateCollection.

/**
 * Update a collection.
 *
 * @param updateCollectionOptions the {@link UpdateCollectionOptions} containing the options for the call
 * @return a {@link ServiceCall} with a response type of {@link Collection}
 */
public ServiceCall<Collection> updateCollection(UpdateCollectionOptions updateCollectionOptions) {
    Validator.notNull(updateCollectionOptions, "updateCollectionOptions cannot be null");
    String[] pathSegments = { "v1/environments", "collections" };
    String[] pathParameters = { updateCollectionOptions.environmentId(), updateCollectionOptions.collectionId() };
    RequestBuilder builder = RequestBuilder.put(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
    builder.query(VERSION, versionDate);
    final JsonObject contentJson = new JsonObject();
    if (updateCollectionOptions.name() != null) {
        contentJson.addProperty("name", updateCollectionOptions.name());
    }
    if (updateCollectionOptions.description() != null) {
        contentJson.addProperty("description", updateCollectionOptions.description());
    }
    if (updateCollectionOptions.configurationId() != null) {
        contentJson.addProperty("configuration_id", updateCollectionOptions.configurationId());
    }
    builder.bodyJson(contentJson);
    return createServiceCall(builder.build(), ResponseConverterUtils.getObject(Collection.class));
}
Also used : RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) JsonObject(com.google.gson.JsonObject) Collection(com.ibm.watson.developer_cloud.discovery.v1.model.Collection)

Example 13 with Collection

use of li.naska.bgg.resource.v3.model.Collection 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 14 with Collection

use of li.naska.bgg.resource.v3.model.Collection in project java-sdk by watson-developer-cloud.

the class VisualRecognitionTest method testGetCollectionWOptions.

@Test
public void testGetCollectionWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "{\"collection_id\": \"collectionId\", \"name\": \"name\", \"description\": \"description\", \"created\": \"2019-01-01T12:00:00.000Z\", \"updated\": \"2019-01-01T12:00:00.000Z\", \"image_count\": 10, \"training_status\": {\"objects\": {\"ready\": false, \"in_progress\": true, \"data_changed\": false, \"latest_failed\": true, \"rscnn_ready\": true, \"description\": \"description\"}}}";
    String getCollectionPath = "/v4/collections/testString";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the GetCollectionOptions model
    GetCollectionOptions getCollectionOptionsModel = new GetCollectionOptions.Builder().collectionId("testString").build();
    // Invoke operation with valid options model (positive test)
    Response<Collection> response = visualRecognitionService.getCollection(getCollectionOptionsModel).execute();
    assertNotNull(response);
    Collection responseObj = response.getResult();
    assertNotNull(responseObj);
    // Verify the contents of the request
    RecordedRequest request = server.takeRequest();
    assertNotNull(request);
    assertEquals(request.getMethod(), "GET");
    // 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, getCollectionPath);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) GetCollectionOptions(com.ibm.watson.visual_recognition.v4.model.GetCollectionOptions) Collection(com.ibm.watson.visual_recognition.v4.model.Collection) Test(org.testng.annotations.Test)

Example 15 with Collection

use of li.naska.bgg.resource.v3.model.Collection in project java-sdk by watson-developer-cloud.

the class VisualRecognition method updateCollection.

/**
 * Update a collection.
 *
 * <p>Update the name or description of a collection.
 *
 * <p>Encode the name and description in UTF-8 if they contain non-ASCII characters. The service
 * assumes UTF-8 encoding if it encounters non-ASCII characters.
 *
 * @param updateCollectionOptions the {@link UpdateCollectionOptions} containing the options for
 *     the call
 * @return a {@link ServiceCall} with a result of type {@link Collection}
 */
public ServiceCall<Collection> updateCollection(UpdateCollectionOptions updateCollectionOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(updateCollectionOptions, "updateCollectionOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("collection_id", updateCollectionOptions.collectionId());
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v4/collections/{collection_id}", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("watson_vision_combined", "v4", "updateCollection");
    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 (updateCollectionOptions.name() != null) {
        contentJson.addProperty("name", updateCollectionOptions.name());
    }
    if (updateCollectionOptions.description() != null) {
        contentJson.addProperty("description", updateCollectionOptions.description());
    }
    builder.bodyJson(contentJson);
    ResponseConverter<Collection> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<Collection>() {
    }.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) Collection(com.ibm.watson.visual_recognition.v4.model.Collection)

Aggregations

Collection (com.ibm.watson.developer_cloud.discovery.v1.model.Collection)26 Test (org.junit.Test)22 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)18 Collection (com.ibm.watson.visual_recognition.v4.model.Collection)11 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)11 DocumentAccepted (com.ibm.watson.developer_cloud.discovery.v1.model.DocumentAccepted)10 JsonObject (com.google.gson.JsonObject)9 CreateCollectionOptions (com.ibm.watson.developer_cloud.discovery.v1.model.CreateCollectionOptions)9 Collection (com.ibm.watson.discovery.v1.model.Collection)9 Configuration (com.ibm.watson.developer_cloud.discovery.v1.model.Configuration)8 ByteArrayInputStream (java.io.ByteArrayInputStream)8 FileInputStream (java.io.FileInputStream)8 InputStream (java.io.InputStream)8 MockResponse (okhttp3.mockwebserver.MockResponse)7 Test (org.testng.annotations.Test)7 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)6 AddDocumentOptions (com.ibm.watson.developer_cloud.discovery.v1.model.AddDocumentOptions)5 QueryOptions (com.ibm.watson.developer_cloud.discovery.v1.model.QueryOptions)5 QueryResponse (com.ibm.watson.developer_cloud.discovery.v1.model.QueryResponse)5 HashMap (java.util.HashMap)5