use of com.ibm.watson.visual_recognition.v4.model.ListImagesOptions 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.ListImagesOptions in project java-sdk by watson-developer-cloud.
the class VisualRecognition method listImages.
/**
* List images.
*
* <p>Retrieves a list of images in a collection.
*
* @param listImagesOptions the {@link ListImagesOptions} containing the options for the call
* @return a {@link ServiceCall} with a result of type {@link ImageSummaryList}
*/
public ServiceCall<ImageSummaryList> listImages(ListImagesOptions listImagesOptions) {
com.ibm.cloud.sdk.core.util.Validator.notNull(listImagesOptions, "listImagesOptions cannot be null");
Map<String, String> pathParamsMap = new HashMap<String, String>();
pathParamsMap.put("collection_id", listImagesOptions.collectionId());
RequestBuilder builder = RequestBuilder.get(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v4/collections/{collection_id}/images", pathParamsMap));
Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("watson_vision_combined", "v4", "listImages");
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));
ResponseConverter<ImageSummaryList> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<ImageSummaryList>() {
}.getType());
return createServiceCall(builder.build(), responseConverter);
}
use of com.ibm.watson.visual_recognition.v4.model.ListImagesOptions in project java-sdk by watson-developer-cloud.
the class VisualRecognitionTest method testListImagesWOptions.
@Test
public void testListImagesWOptions() throws Throwable {
// Schedule some responses.
String mockResponseBody = "{\"images\": [{\"image_id\": \"imageId\", \"updated\": \"2019-01-01T12:00:00.000Z\"}]}";
String listImagesPath = "/v4/collections/testString/images";
server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
constructClientService();
// Construct an instance of the ListImagesOptions model
ListImagesOptions listImagesOptionsModel = new ListImagesOptions.Builder().collectionId("testString").build();
// Invoke operation with valid options model (positive test)
Response<ImageSummaryList> response = visualRecognitionService.listImages(listImagesOptionsModel).execute();
assertNotNull(response);
ImageSummaryList responseObj = response.getResult();
assertNotNull(responseObj);
// Verify the contents of the request
RecordedRequest request = server.takeRequest();
assertNotNull(request);
assertEquals(request.getMethod(), "GET");
// 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, listImagesPath);
}
Aggregations