Search in sources :

Example 1 with AnnotateImageRequest

use of com.google.cloud.vision.v1p4beta1.AnnotateImageRequest in project google-cloud-java by GoogleCloudPlatform.

the class ImageAnnotatorClientTest method batchAnnotateImagesTest.

@Test
@SuppressWarnings("all")
public void batchAnnotateImagesTest() {
    BatchAnnotateImagesResponse expectedResponse = BatchAnnotateImagesResponse.newBuilder().build();
    mockImageAnnotator.addResponse(expectedResponse);
    List<AnnotateImageRequest> requests = new ArrayList<>();
    BatchAnnotateImagesResponse actualResponse = client.batchAnnotateImages(requests);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<GeneratedMessageV3> actualRequests = mockImageAnnotator.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    BatchAnnotateImagesRequest actualRequest = (BatchAnnotateImagesRequest) actualRequests.get(0);
    Assert.assertEquals(requests, actualRequest.getRequestsList());
}
Also used : BatchAnnotateImagesRequest(com.google.cloud.vision.v1.BatchAnnotateImagesRequest) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) ArrayList(java.util.ArrayList) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse) Test(org.junit.Test)

Example 2 with AnnotateImageRequest

use of com.google.cloud.vision.v1p4beta1.AnnotateImageRequest in project spring-cloud-gcp by spring-cloud.

the class VisionController method uploadImage.

/**
 * This method downloads an image from a URL and sends its contents to the Vision API for label detection.
 *
 * @param imageUrl the URL of the image
 * @return a string with the list of labels and percentage of certainty
 * @throws Exception if the Vision API call produces an error
 */
@GetMapping("/vision")
public String uploadImage(String imageUrl) throws Exception {
    // Copies the content of the image to memory.
    byte[] imageBytes = StreamUtils.copyToByteArray(this.resourceLoader.getResource(imageUrl).getInputStream());
    BatchAnnotateImagesResponse responses;
    Image image = Image.newBuilder().setContent(ByteString.copyFrom(imageBytes)).build();
    // Sets the type of request to label detection, to detect broad sets of categories in an image.
    Feature feature = Feature.newBuilder().setType(Feature.Type.LABEL_DETECTION).build();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().setImage(image).addFeatures(feature).build();
    responses = this.imageAnnotatorClient.batchAnnotateImages(Collections.singletonList(request));
    StringBuilder responseBuilder = new StringBuilder("<table border=\"1\">");
    responseBuilder.append("<tr><th>description</th><th>score</th></tr>");
    // We're only expecting one response.
    if (responses.getResponsesCount() == 1) {
        AnnotateImageResponse response = responses.getResponses(0);
        if (response.hasError()) {
            throw new Exception(response.getError().getMessage());
        }
        for (EntityAnnotation annotation : response.getLabelAnnotationsList()) {
            responseBuilder.append("<tr><td>").append(annotation.getDescription()).append("</td><td>").append(annotation.getScore()).append("</td></tr>");
        }
    }
    responseBuilder.append("</table>");
    responseBuilder.append("<p><img src='" + imageUrl + "'/></p>");
    return responseBuilder.toString();
}
Also used : AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) Image(com.google.cloud.vision.v1.Image) EntityAnnotation(com.google.cloud.vision.v1.EntityAnnotation) Feature(com.google.cloud.vision.v1.Feature) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 3 with AnnotateImageRequest

use of com.google.cloud.vision.v1p4beta1.AnnotateImageRequest in project beam by apache.

the class CloudVisionTest method shouldConvertStringToRequest.

@Test
public void shouldConvertStringToRequest() {
    CloudVision.AnnotateImagesFromGcsUri annotateImagesFromGcsUri = CloudVision.annotateImagesFromGcsUri(null, features, 1, 1);
    AnnotateImageRequest request = annotateImagesFromGcsUri.mapToRequest(TEST_URI, null);
    assertEquals(1, request.getFeaturesCount());
    assertEquals(TEST_URI, request.getImage().getSource().getGcsImageUri());
}
Also used : AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) Test(org.junit.Test)

Example 4 with AnnotateImageRequest

use of com.google.cloud.vision.v1p4beta1.AnnotateImageRequest in project beam by apache.

the class CloudVisionTest method shouldConvertKVOfBytesToRequest.

@Test
public void shouldConvertKVOfBytesToRequest() {
    CloudVision.AnnotateImagesFromBytesWithContext annotateImagesFromBytesWithContext = CloudVision.annotateImagesFromBytesWithContext(features, 1, 1);
    AnnotateImageRequest request = annotateImagesFromBytesWithContext.mapToRequest(KV.of(TEST_BYTES, null), null);
    assertEquals(1, request.getFeaturesCount());
    assertEquals(TEST_BYTES, request.getImage().getContent());
}
Also used : AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) Test(org.junit.Test)

Example 5 with AnnotateImageRequest

use of com.google.cloud.vision.v1p4beta1.AnnotateImageRequest in project beam by apache.

the class CloudVisionTest method shouldConvertByteStringToRequest.

@Test
public void shouldConvertByteStringToRequest() {
    CloudVision.AnnotateImagesFromBytes annotateImagesFromBytes = CloudVision.annotateImagesFromBytes(null, features, 1, 1);
    AnnotateImageRequest request = annotateImagesFromBytes.mapToRequest(TEST_BYTES, null);
    assertEquals(1, request.getFeaturesCount());
    assertEquals(TEST_BYTES, request.getImage().getContent());
}
Also used : AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) Test(org.junit.Test)

Aggregations

AnnotateImageRequest (com.google.cloud.vision.v1.AnnotateImageRequest)74 Image (com.google.cloud.vision.v1.Image)68 BatchAnnotateImagesResponse (com.google.cloud.vision.v1.BatchAnnotateImagesResponse)66 Feature (com.google.cloud.vision.v1.Feature)66 ArrayList (java.util.ArrayList)65 ImageAnnotatorClient (com.google.cloud.vision.v1.ImageAnnotatorClient)62 AnnotateImageResponse (com.google.cloud.vision.v1.AnnotateImageResponse)59 ByteString (com.google.protobuf.ByteString)43 ImageSource (com.google.cloud.vision.v1.ImageSource)39 FileInputStream (java.io.FileInputStream)31 EntityAnnotation (com.google.cloud.vision.v1.EntityAnnotation)27 WebImage (com.google.cloud.vision.v1.WebDetection.WebImage)26 WebDetection (com.google.cloud.vision.v1.WebDetection)11 IOException (java.io.IOException)11 ImageContext (com.google.cloud.vision.v1.ImageContext)10 LocationInfo (com.google.cloud.vision.v1.LocationInfo)10 SafeSearchAnnotation (com.google.cloud.vision.v1.SafeSearchAnnotation)10 Test (org.junit.Test)10 CropHint (com.google.cloud.vision.v1.CropHint)9 WebPage (com.google.cloud.vision.v1.WebDetection.WebPage)9