Search in sources :

Example 1 with EntityAnnotation

use of com.google.cloud.vision.v1.EntityAnnotation 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 2 with EntityAnnotation

use of com.google.cloud.vision.v1.EntityAnnotation in project java-docs-samples by GoogleCloudPlatform.

the class Detect method detectLabelsGcs.

/**
 * Detects labels in the specified remote image on Google Cloud Storage.
 *
 * @param gcsPath The path to the remote file on Google Cloud Storage to perform label detection
 *                on.
 * @param out A {@link PrintStream} to write detected features to.
 * @throws Exception on errors while closing the client.
 * @throws IOException on Input/Output errors.
 */
public static void detectLabelsGcs(String gcsPath, PrintStream out) throws Exception, IOException {
    List<AnnotateImageRequest> requests = new ArrayList<>();
    ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build();
    Image img = Image.newBuilder().setSource(imgSource).build();
    Feature feat = Feature.newBuilder().setType(Type.LABEL_DETECTION).build();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
    requests.add(request);
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
        BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
        List<AnnotateImageResponse> responses = response.getResponsesList();
        for (AnnotateImageResponse res : responses) {
            if (res.hasError()) {
                out.printf("Error: %s\n", res.getError().getMessage());
                return;
            }
            // For full list of available annotations, see http://g.co/cloud/vision/docs
            for (EntityAnnotation annotation : res.getLabelAnnotationsList()) {
                annotation.getAllFields().forEach((k, v) -> out.printf("%s : %s\n", k, v.toString()));
            }
        }
    }
}
Also used : AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ArrayList(java.util.ArrayList) ImageSource(com.google.cloud.vision.v1.ImageSource) WebImage(com.google.cloud.vision.v1.WebDetection.WebImage) 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)

Example 3 with EntityAnnotation

use of com.google.cloud.vision.v1.EntityAnnotation in project java-docs-samples by GoogleCloudPlatform.

the class Detect method detectLabels.

/**
 * Detects labels in the specified local image.
 *
 * @param filePath The path to the file to perform label detection on.
 * @param out A {@link PrintStream} to write detected labels to.
 * @throws Exception on errors while closing the client.
 * @throws IOException on Input/Output errors.
 */
public static void detectLabels(String filePath, PrintStream out) throws Exception, IOException {
    List<AnnotateImageRequest> requests = new ArrayList<>();
    ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));
    Image img = Image.newBuilder().setContent(imgBytes).build();
    Feature feat = Feature.newBuilder().setType(Type.LABEL_DETECTION).build();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
    requests.add(request);
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
        BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
        List<AnnotateImageResponse> responses = response.getResponsesList();
        for (AnnotateImageResponse res : responses) {
            if (res.hasError()) {
                out.printf("Error: %s\n", res.getError().getMessage());
                return;
            }
            // For full list of available annotations, see http://g.co/cloud/vision/docs
            for (EntityAnnotation annotation : res.getLabelAnnotationsList()) {
                annotation.getAllFields().forEach((k, v) -> out.printf("%s : %s\n", k, v.toString()));
            }
        }
    }
}
Also used : AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) ByteString(com.google.protobuf.ByteString) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ArrayList(java.util.ArrayList) WebImage(com.google.cloud.vision.v1.WebDetection.WebImage) Image(com.google.cloud.vision.v1.Image) EntityAnnotation(com.google.cloud.vision.v1.EntityAnnotation) Feature(com.google.cloud.vision.v1.Feature) FileInputStream(java.io.FileInputStream) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 4 with EntityAnnotation

use of com.google.cloud.vision.v1.EntityAnnotation in project google-cloud-java by GoogleCloudPlatform.

the class AnnotateImage method main.

public static void main(String... args) throws Exception {
    // Instantiates a client
    ImageAnnotatorClient vision = ImageAnnotatorClient.create();
    // The path to the image file to annotate
    // for example "./resources/wakeupcat.jpg";
    String fileName = "your/image/path.jpg";
    // Reads the image file into memory
    Path path = Paths.get(fileName);
    byte[] data = Files.readAllBytes(path);
    ByteString imgBytes = ByteString.copyFrom(data);
    // Builds the image annotation request
    List<AnnotateImageRequest> requests = new ArrayList<>();
    Image img = Image.newBuilder().setContent(imgBytes).build();
    Feature feat = Feature.newBuilder().setType(Type.LABEL_DETECTION).build();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
    requests.add(request);
    // Performs label detection on the image file
    BatchAnnotateImagesResponse response = vision.batchAnnotateImages(requests);
    List<AnnotateImageResponse> responses = response.getResponsesList();
    for (AnnotateImageResponse res : responses) {
        if (res.hasError()) {
            System.out.printf("Error: %s\n", res.getError().getMessage());
            return;
        }
        for (EntityAnnotation annotation : res.getLabelAnnotationsList()) {
            for (Map.Entry<FieldDescriptor, Object> entry : annotation.getAllFields().entrySet()) {
                System.out.printf("%s : %s\n", entry.getKey(), entry.getValue());
            }
        }
    }
}
Also used : Path(java.nio.file.Path) ByteString(com.google.protobuf.ByteString) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) Image(com.google.cloud.vision.v1.Image) Feature(com.google.cloud.vision.v1.Feature) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) EntityAnnotation(com.google.cloud.vision.v1.EntityAnnotation) Map(java.util.Map) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 5 with EntityAnnotation

use of com.google.cloud.vision.v1.EntityAnnotation in project java-docs-samples by GoogleCloudPlatform.

the class Detect method detectTextGcs.

/**
 * Detects text in the specified remote image on Google Cloud Storage.
 *
 * @param gcsPath The path to the remote file on Google Cloud Storage to detect text in.
 * @param out A {@link PrintStream} to write the detected text to.
 * @throws Exception on errors while closing the client.
 * @throws IOException on Input/Output errors.
 */
public static void detectTextGcs(String gcsPath, PrintStream out) throws Exception, IOException {
    List<AnnotateImageRequest> requests = new ArrayList<>();
    ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build();
    Image img = Image.newBuilder().setSource(imgSource).build();
    Feature feat = Feature.newBuilder().setType(Type.TEXT_DETECTION).build();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
    requests.add(request);
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
        BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
        List<AnnotateImageResponse> responses = response.getResponsesList();
        for (AnnotateImageResponse res : responses) {
            if (res.hasError()) {
                out.printf("Error: %s\n", res.getError().getMessage());
                return;
            }
            // For full list of available annotations, see http://g.co/cloud/vision/docs
            for (EntityAnnotation annotation : res.getTextAnnotationsList()) {
                out.printf("Text: %s\n", annotation.getDescription());
                out.printf("Position : %s\n", annotation.getBoundingPoly());
            }
        }
    }
}
Also used : AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ArrayList(java.util.ArrayList) ImageSource(com.google.cloud.vision.v1.ImageSource) WebImage(com.google.cloud.vision.v1.WebDetection.WebImage) 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)

Aggregations

AnnotateImageRequest (com.google.cloud.vision.v1.AnnotateImageRequest)12 AnnotateImageResponse (com.google.cloud.vision.v1.AnnotateImageResponse)12 BatchAnnotateImagesResponse (com.google.cloud.vision.v1.BatchAnnotateImagesResponse)12 EntityAnnotation (com.google.cloud.vision.v1.EntityAnnotation)12 Feature (com.google.cloud.vision.v1.Feature)12 Image (com.google.cloud.vision.v1.Image)12 ImageAnnotatorClient (com.google.cloud.vision.v1.ImageAnnotatorClient)11 ArrayList (java.util.ArrayList)11 WebImage (com.google.cloud.vision.v1.WebDetection.WebImage)9 ByteString (com.google.protobuf.ByteString)6 ImageSource (com.google.cloud.vision.v1.ImageSource)5 FileInputStream (java.io.FileInputStream)4 LocationInfo (com.google.cloud.vision.v1.LocationInfo)3 Path (java.nio.file.Path)2 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)1 Map (java.util.Map)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1