Search in sources :

Example 1 with GetJpegImageOptions

use of com.ibm.watson.visual_recognition.v4.model.GetJpegImageOptions 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 2 with GetJpegImageOptions

use of com.ibm.watson.visual_recognition.v4.model.GetJpegImageOptions in project java-sdk by watson-developer-cloud.

the class VisualRecognitionTest method testGetJpegImageWOptions.

@Test
public void testGetJpegImageWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "This is a mock binary response.";
    String getJpegImagePath = "/v4/collections/testString/images/testString/jpeg";
    server.enqueue(new MockResponse().setHeader("Content-type", "image/jpeg").setResponseCode(200).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the GetJpegImageOptions model
    GetJpegImageOptions getJpegImageOptionsModel = new GetJpegImageOptions.Builder().collectionId("testString").imageId("testString").size("full").build();
    // Invoke operation with valid options model (positive test)
    Response<InputStream> response = visualRecognitionService.getJpegImage(getJpegImageOptionsModel).execute();
    assertNotNull(response);
    InputStream 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");
    assertEquals(query.get("size"), "full");
    // Check request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, getJpegImagePath);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) GetJpegImageOptions(com.ibm.watson.visual_recognition.v4.model.GetJpegImageOptions) InputStream(java.io.InputStream) Test(org.testng.annotations.Test)

Aggregations

GetJpegImageOptions (com.ibm.watson.visual_recognition.v4.model.GetJpegImageOptions)2 InputStream (java.io.InputStream)2 FileWithMetadata (com.ibm.cloud.sdk.core.service.model.FileWithMetadata)1 WatsonServiceTest (com.ibm.watson.common.WatsonServiceTest)1 AddImagesOptions (com.ibm.watson.visual_recognition.v4.model.AddImagesOptions)1 DeleteImageOptions (com.ibm.watson.visual_recognition.v4.model.DeleteImageOptions)1 GetImageDetailsOptions (com.ibm.watson.visual_recognition.v4.model.GetImageDetailsOptions)1 ImageDetails (com.ibm.watson.visual_recognition.v4.model.ImageDetails)1 ImageDetailsList (com.ibm.watson.visual_recognition.v4.model.ImageDetailsList)1 ImageSummary (com.ibm.watson.visual_recognition.v4.model.ImageSummary)1 ImageSummaryList (com.ibm.watson.visual_recognition.v4.model.ImageSummaryList)1 ListImagesOptions (com.ibm.watson.visual_recognition.v4.model.ListImagesOptions)1 File (java.io.File)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)1 Test (org.junit.Test)1 Test (org.testng.annotations.Test)1