Search in sources :

Example 16 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 17 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 18 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 19 with FileWithMetadata

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

Example 20 with FileWithMetadata

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

the class VisualRecognitionIT method testAnalyzeWithFiles.

/**
 * Test analyze with files.
 *
 * @throws FileNotFoundException the file not found exception
 */
@Test
public void testAnalyzeWithFiles() throws FileNotFoundException {
    FileWithMetadata giraffeImage = new FileWithMetadata.Builder().data(new File(SINGLE_GIRAFFE_IMAGE_PATH)).contentType("image/jpeg").build();
    FileWithMetadata turtleImage = new FileWithMetadata.Builder().data(new File(SINGLE_TURTLE_IMAGE_PATH)).contentType("image/jpeg").build();
    List<FileWithMetadata> filesToAnalyze = Arrays.asList(giraffeImage, turtleImage);
    List<String> collectionIds = Collections.singletonList(COLLECTION_ID);
    AnalyzeOptions options = new AnalyzeOptions.Builder().imagesFile(filesToAnalyze).collectionIds(collectionIds).addFeatures(AnalyzeOptions.Features.OBJECTS).threshold(.5f).build();
    AnalyzeResponse response = service.analyze(options).execute().getResult();
    assertNotNull(response);
    assertEquals(2, response.getImages().size());
}
Also used : AnalyzeOptions(com.ibm.watson.visual_recognition.v4.model.AnalyzeOptions) FileWithMetadata(com.ibm.cloud.sdk.core.service.model.FileWithMetadata) File(java.io.File) AnalyzeResponse(com.ibm.watson.visual_recognition.v4.model.AnalyzeResponse) WatsonServiceTest(com.ibm.watson.common.WatsonServiceTest) Test(org.junit.Test)

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