use of com.ibm.watson.visual_recognition.v4.model.DeleteImageOptions 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.watson.visual_recognition.v4.model.DeleteImageOptions 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.watson.visual_recognition.v4.model.DeleteImageOptions in project java-sdk by watson-developer-cloud.
the class VisualRecognitionTest method testDeleteImageWOptions.
@Test
public void testDeleteImageWOptions() throws Throwable {
// Schedule some responses.
String mockResponseBody = "";
String deleteImagePath = "/v4/collections/testString/images/testString";
server.enqueue(new MockResponse().setResponseCode(200).setBody(mockResponseBody));
constructClientService();
// Construct an instance of the DeleteImageOptions model
DeleteImageOptions deleteImageOptionsModel = new DeleteImageOptions.Builder().collectionId("testString").imageId("testString").build();
// Invoke operation with valid options model (positive test)
Response<Void> response = visualRecognitionService.deleteImage(deleteImageOptionsModel).execute();
assertNotNull(response);
Void responseObj = response.getResult();
// Response does not have a return type. Check that the result is null.
assertNull(responseObj);
// Verify the contents of the request
RecordedRequest request = server.takeRequest();
assertNotNull(request);
assertEquals(request.getMethod(), "DELETE");
// 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, deleteImagePath);
}
Aggregations