Search in sources :

Example 1 with FileWithMetadata

use of com.ibm.cloud.sdk.core.service.model.FileWithMetadata in project cloudant-java-sdk by IBM.

the class TestUtilities method creatMockListFileWithMetadata.

public static List<FileWithMetadata> creatMockListFileWithMetadata() {
    List<FileWithMetadata> list = new ArrayList<FileWithMetadata>();
    byte[] fileBytes = { (byte) 0xde, (byte) 0xad, (byte) 0xbe, (byte) 0xef };
    InputStream inputStream = new ByteArrayInputStream(fileBytes);
    FileWithMetadata.Builder builder = new FileWithMetadata.Builder();
    builder.data(inputStream);
    FileWithMetadata fileWithMetadata = builder.build();
    list.add(fileWithMetadata);
    return list;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) FileWithMetadata(com.ibm.cloud.sdk.core.service.model.FileWithMetadata)

Example 2 with FileWithMetadata

use of com.ibm.cloud.sdk.core.service.model.FileWithMetadata in project java-sdk by watson-developer-cloud.

the class TestUtilities method creatMockListFileWithMetadata.

public static List<FileWithMetadata> creatMockListFileWithMetadata() {
    List<FileWithMetadata> list = new ArrayList<FileWithMetadata>();
    byte[] fileBytes = { (byte) 0xde, (byte) 0xad, (byte) 0xbe, (byte) 0xef };
    InputStream inputStream = new ByteArrayInputStream(fileBytes);
    FileWithMetadata.Builder builder = new FileWithMetadata.Builder();
    builder.data(inputStream);
    FileWithMetadata fileWithMetadata = builder.build();
    list.add(fileWithMetadata);
    return list;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) FileWithMetadata(com.ibm.cloud.sdk.core.service.model.FileWithMetadata)

Example 3 with FileWithMetadata

use of com.ibm.cloud.sdk.core.service.model.FileWithMetadata 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 4 with FileWithMetadata

use of com.ibm.cloud.sdk.core.service.model.FileWithMetadata 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 5 with FileWithMetadata

use of com.ibm.cloud.sdk.core.service.model.FileWithMetadata in project java-sdk by watson-developer-cloud.

the class VisualRecognition method analyze.

/**
 * Analyze images.
 *
 * <p>Analyze images by URL, by file, or both against your own collection. Make sure that
 * **training_status.objects.ready** is `true` for the feature before you use a collection to
 * analyze images.
 *
 * <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 analyzeOptions the {@link AnalyzeOptions} containing the options for the call
 * @return a {@link ServiceCall} with a result of type {@link AnalyzeResponse}
 */
public ServiceCall<AnalyzeResponse> analyze(AnalyzeOptions analyzeOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(analyzeOptions, "analyzeOptions cannot be null");
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v4/analyze"));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("watson_vision_combined", "v4", "analyze");
    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);
    for (String item : analyzeOptions.collectionIds()) {
        multipartBuilder.addFormDataPart("collection_ids", item);
    }
    for (String item : analyzeOptions.features()) {
        multipartBuilder.addFormDataPart("features", item);
    }
    if (analyzeOptions.imagesFile() != null) {
        for (FileWithMetadata item : analyzeOptions.imagesFile()) {
            okhttp3.RequestBody itemBody = RequestUtils.inputStreamBody(item.data(), item.contentType());
            multipartBuilder.addFormDataPart("images_file", item.filename(), itemBody);
        }
    }
    if (analyzeOptions.imageUrl() != null) {
        for (String item : analyzeOptions.imageUrl()) {
            multipartBuilder.addFormDataPart("image_url", item);
        }
    }
    if (analyzeOptions.threshold() != null) {
        multipartBuilder.addFormDataPart("threshold", String.valueOf(analyzeOptions.threshold()));
    }
    builder.body(multipartBuilder.build());
    ResponseConverter<AnalyzeResponse> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<AnalyzeResponse>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) AnalyzeResponse(com.ibm.watson.visual_recognition.v4.model.AnalyzeResponse) MultipartBody(okhttp3.MultipartBody) FileWithMetadata(com.ibm.cloud.sdk.core.service.model.FileWithMetadata)

Aggregations

FileWithMetadata (com.ibm.cloud.sdk.core.service.model.FileWithMetadata)21 InputStream (java.io.InputStream)17 ByteArrayInputStream (java.io.ByteArrayInputStream)16 ArrayList (java.util.ArrayList)16 WatsonServiceTest (com.ibm.watson.common.WatsonServiceTest)3 ImageDetailsList (com.ibm.watson.visual_recognition.v4.model.ImageDetailsList)3 File (java.io.File)3 Test (org.junit.Test)3 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)2 AddImagesOptions (com.ibm.watson.visual_recognition.v4.model.AddImagesOptions)2 AnalyzeResponse (com.ibm.watson.visual_recognition.v4.model.AnalyzeResponse)2 DeleteImageOptions (com.ibm.watson.visual_recognition.v4.model.DeleteImageOptions)2 ImageDetails (com.ibm.watson.visual_recognition.v4.model.ImageDetails)2 MultipartBody (okhttp3.MultipartBody)2 AddImageTrainingDataOptions (com.ibm.watson.visual_recognition.v4.model.AddImageTrainingDataOptions)1 AnalyzeOptions (com.ibm.watson.visual_recognition.v4.model.AnalyzeOptions)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