Search in sources :

Example 1 with ImageDetailsList

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

the class VisualRecognitionIT method testImageOperations.

/**
 * Test image operations.
 *
 * @throws IOException Signals that an I/O exception has occurred.
 */
@Test
public void testImageOperations() throws IOException {
    // create new collection so we don't run into duplicate image issues
    String testCollectionId = createTestCollection();
    List<String> imageUrlList = Arrays.asList(CAT_IMAGE_URL, DOG_IMAGE_URL);
    FileWithMetadata turtleFile = new FileWithMetadata.Builder().data(new File(SINGLE_TURTLE_IMAGE_PATH)).contentType("image/jpeg").build();
    AddImagesOptions addImagesOptions = new AddImagesOptions.Builder().imageUrl(imageUrlList).addImagesFile(turtleFile).collectionId(testCollectionId).build();
    ImageDetailsList imageDetailsList = service.addImages(addImagesOptions).execute().getResult();
    assertNotNull(imageDetailsList);
    String singleImageId = null;
    Set<String> addedImageIds = new HashSet<>();
    for (ImageDetails imageDetails : imageDetailsList.getImages()) {
        addedImageIds.add(imageDetails.getImageId());
        if (singleImageId == null) {
            singleImageId = imageDetails.getImageId();
        }
    }
    try {
        // test get
        GetImageDetailsOptions getImageDetailsOptions = new GetImageDetailsOptions.Builder().collectionId(testCollectionId).imageId(singleImageId).build();
        ImageDetails imageDetails = service.getImageDetails(getImageDetailsOptions).execute().getResult();
        assertNotNull(imageDetails);
        assertEquals(singleImageId, imageDetails.getImageId());
        // test get JPEG
        GetJpegImageOptions getJpegImageOptions = new GetJpegImageOptions.Builder().collectionId(testCollectionId).imageId(singleImageId).size(GetJpegImageOptions.Size.FULL).build();
        InputStream imageStream = service.getJpegImage(getJpegImageOptions).execute().getResult();
        assertNotNull(imageStream);
        imageStream.close();
    } finally {
        // delete images
        for (String imageId : addedImageIds) {
            DeleteImageOptions deleteImageOptions = new DeleteImageOptions.Builder().imageId(imageId).collectionId(testCollectionId).build();
            service.deleteImage(deleteImageOptions).execute();
        }
        // test list and delete
        ListImagesOptions listImagesOptions = new ListImagesOptions.Builder().collectionId(testCollectionId).build();
        ImageSummaryList imageSummaryList = service.listImages(listImagesOptions).execute().getResult();
        assertNotNull(imageSummaryList);
        for (ImageSummary imageSummary : imageSummaryList.getImages()) {
            assertFalse(addedImageIds.contains(imageSummary.getImageId()));
        }
        // remove test collection
        deleteTestCollection(testCollectionId);
    }
}
Also used : GetJpegImageOptions(com.ibm.watson.visual_recognition.v4.model.GetJpegImageOptions) DeleteImageOptions(com.ibm.watson.visual_recognition.v4.model.DeleteImageOptions) ListImagesOptions(com.ibm.watson.visual_recognition.v4.model.ListImagesOptions) InputStream(java.io.InputStream) ImageDetails(com.ibm.watson.visual_recognition.v4.model.ImageDetails) ImageSummaryList(com.ibm.watson.visual_recognition.v4.model.ImageSummaryList) ImageSummary(com.ibm.watson.visual_recognition.v4.model.ImageSummary) AddImagesOptions(com.ibm.watson.visual_recognition.v4.model.AddImagesOptions) FileWithMetadata(com.ibm.cloud.sdk.core.service.model.FileWithMetadata) File(java.io.File) GetImageDetailsOptions(com.ibm.watson.visual_recognition.v4.model.GetImageDetailsOptions) ImageDetailsList(com.ibm.watson.visual_recognition.v4.model.ImageDetailsList) WatsonServiceTest(com.ibm.watson.common.WatsonServiceTest) Test(org.junit.Test)

Example 2 with ImageDetailsList

use of com.ibm.watson.visual_recognition.v4.model.ImageDetailsList 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 3 with ImageDetailsList

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

the class VisualRecognitionTest method testAddImagesWOptions.

@Test
public void testAddImagesWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "{\"images\": [{\"image_id\": \"imageId\", \"updated\": \"2019-01-01T12:00:00.000Z\", \"created\": \"2019-01-01T12:00:00.000Z\", \"source\": {\"type\": \"file\", \"filename\": \"filename\", \"archive_filename\": \"archiveFilename\", \"source_url\": \"sourceUrl\", \"resolved_url\": \"resolvedUrl\"}, \"dimensions\": {\"height\": 6, \"width\": 5}, \"errors\": [{\"code\": \"invalid_field\", \"message\": \"message\", \"more_info\": \"moreInfo\", \"target\": {\"type\": \"field\", \"name\": \"name\"}}], \"training_data\": {\"objects\": [{\"object\": \"object\", \"location\": {\"top\": 3, \"left\": 4, \"width\": 5, \"height\": 6}}]}}], \"warnings\": [{\"code\": \"invalid_field\", \"message\": \"message\", \"more_info\": \"moreInfo\"}], \"trace\": \"trace\"}";
    String addImagesPath = "/v4/collections/testString/images";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the AddImagesOptions model
    AddImagesOptions addImagesOptionsModel = new AddImagesOptions.Builder().collectionId("testString").imagesFile(mockListFileWithMetadata).imageUrl(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).trainingData("{\"objects\":[{\"object\":\"2018-Fit\",\"location\":{\"left\":33,\"top\":8,\"width\":760,\"height\":419}}]}").build();
    // Invoke operation with valid options model (positive test)
    Response<ImageDetailsList> response = visualRecognitionService.addImages(addImagesOptionsModel).execute();
    assertNotNull(response);
    ImageDetailsList 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, addImagesPath);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) AddImagesOptions(com.ibm.watson.visual_recognition.v4.model.AddImagesOptions) ImageDetailsList(com.ibm.watson.visual_recognition.v4.model.ImageDetailsList) Test(org.testng.annotations.Test)

Example 4 with ImageDetailsList

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

the class VisualRecognition method addImages.

/**
 * Add images.
 *
 * <p>Add images to a collection by URL, by file, or both.
 *
 * <p>Encode the image and .zip file names in UTF-8 if they contain non-ASCII characters. The
 * service assumes UTF-8 encoding if it encounters non-ASCII characters.
 *
 * @param addImagesOptions the {@link AddImagesOptions} containing the options for the call
 * @return a {@link ServiceCall} with a result of type {@link ImageDetailsList}
 */
public ServiceCall<ImageDetailsList> addImages(AddImagesOptions addImagesOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(addImagesOptions, "addImagesOptions cannot be null");
    com.ibm.cloud.sdk.core.util.Validator.isTrue((addImagesOptions.imagesFile() != null) || (addImagesOptions.imageUrl() != null) || (addImagesOptions.trainingData() != null), "At least one of imagesFile, imageUrl, or trainingData must be supplied.");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("collection_id", addImagesOptions.collectionId());
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v4/collections/{collection_id}/images", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("watson_vision_combined", "v4", "addImages");
    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));
    MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
    multipartBuilder.setType(MultipartBody.FORM);
    if (addImagesOptions.imagesFile() != null) {
        for (FileWithMetadata item : addImagesOptions.imagesFile()) {
            okhttp3.RequestBody itemBody = RequestUtils.inputStreamBody(item.data(), item.contentType());
            multipartBuilder.addFormDataPart("images_file", item.filename(), itemBody);
        }
    }
    if (addImagesOptions.imageUrl() != null) {
        for (String item : addImagesOptions.imageUrl()) {
            multipartBuilder.addFormDataPart("image_url", item);
        }
    }
    if (addImagesOptions.trainingData() != null) {
        multipartBuilder.addFormDataPart("training_data", addImagesOptions.trainingData());
    }
    builder.body(multipartBuilder.build());
    ResponseConverter<ImageDetailsList> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<ImageDetailsList>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) MultipartBody(okhttp3.MultipartBody) FileWithMetadata(com.ibm.cloud.sdk.core.service.model.FileWithMetadata) ImageDetailsList(com.ibm.watson.visual_recognition.v4.model.ImageDetailsList)

Aggregations

ImageDetailsList (com.ibm.watson.visual_recognition.v4.model.ImageDetailsList)4 FileWithMetadata (com.ibm.cloud.sdk.core.service.model.FileWithMetadata)3 AddImagesOptions (com.ibm.watson.visual_recognition.v4.model.AddImagesOptions)3 WatsonServiceTest (com.ibm.watson.common.WatsonServiceTest)2 DeleteImageOptions (com.ibm.watson.visual_recognition.v4.model.DeleteImageOptions)2 ImageDetails (com.ibm.watson.visual_recognition.v4.model.ImageDetails)2 File (java.io.File)2 Test (org.junit.Test)2 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)1 AddImageTrainingDataOptions (com.ibm.watson.visual_recognition.v4.model.AddImageTrainingDataOptions)1 Collection (com.ibm.watson.visual_recognition.v4.model.Collection)1 GetImageDetailsOptions (com.ibm.watson.visual_recognition.v4.model.GetImageDetailsOptions)1 GetJpegImageOptions (com.ibm.watson.visual_recognition.v4.model.GetJpegImageOptions)1 ImageSummary (com.ibm.watson.visual_recognition.v4.model.ImageSummary)1 ImageSummaryList (com.ibm.watson.visual_recognition.v4.model.ImageSummaryList)1 ListImagesOptions (com.ibm.watson.visual_recognition.v4.model.ListImagesOptions)1 Location (com.ibm.watson.visual_recognition.v4.model.Location)1 TrainOptions (com.ibm.watson.visual_recognition.v4.model.TrainOptions)1 TrainingDataObject (com.ibm.watson.visual_recognition.v4.model.TrainingDataObject)1 TrainingDataObjects (com.ibm.watson.visual_recognition.v4.model.TrainingDataObjects)1