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;
}
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;
}
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);
}
}
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);
}
}
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);
}
Aggregations