Search in sources :

Example 1 with Type

use of com.google.cloud.vision.v1.Feature.Type 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)

Aggregations

AnnotateImageRequest (com.google.cloud.vision.v1.AnnotateImageRequest)1 AnnotateImageResponse (com.google.cloud.vision.v1.AnnotateImageResponse)1 BatchAnnotateImagesResponse (com.google.cloud.vision.v1.BatchAnnotateImagesResponse)1 EntityAnnotation (com.google.cloud.vision.v1.EntityAnnotation)1 Feature (com.google.cloud.vision.v1.Feature)1 Image (com.google.cloud.vision.v1.Image)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1