Search in sources :

Example 71 with AnnotateImageRequest

use of com.google.cloud.vision.v1p3beta1.AnnotateImageRequest in project java-vision by googleapis.

the class DetectWebEntities method detectWebEntities.

// Find web entities given a local image.
public static void detectWebEntities(String filePath) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
        // Read in the local image
        ByteString contents = ByteString.readFrom(new FileInputStream(filePath));
        // Build the image
        Image image = Image.newBuilder().setContent(contents).build();
        // Create the request with the image and the specified feature: web detection
        AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(Feature.newBuilder().setType(Type.WEB_DETECTION)).setImage(image).build();
        // Perform the request
        BatchAnnotateImagesResponse response = client.batchAnnotateImages(Arrays.asList(request));
        // Display the results
        response.getResponsesList().stream().forEach(r -> r.getWebDetection().getWebEntitiesList().stream().forEach(entity -> {
            System.out.format("Description: %s%n", entity.getDescription());
            System.out.format("Score: %f%n", entity.getScore());
        }));
    }
}
Also used : ByteString(com.google.protobuf.ByteString) Arrays(java.util.Arrays) Image(com.google.cloud.vision.v1.Image) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) Type(com.google.cloud.vision.v1.Feature.Type) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Feature(com.google.cloud.vision.v1.Feature) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) ByteString(com.google.protobuf.ByteString) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) Image(com.google.cloud.vision.v1.Image) FileInputStream(java.io.FileInputStream) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 72 with AnnotateImageRequest

use of com.google.cloud.vision.v1p3beta1.AnnotateImageRequest in project java-vision by googleapis.

the class DetectWebEntitiesGcs method detectWebEntitiesGcs.

// Find web entities given the remote image on Google Cloud Storage.
public static void detectWebEntitiesGcs(String gcsPath) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
        // Set the image source to the given gs uri
        ImageSource imageSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build();
        // Build the image
        Image image = Image.newBuilder().setSource(imageSource).build();
        // Create the request with the image and the specified feature: web detection
        AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(Feature.newBuilder().setType(Feature.Type.WEB_DETECTION)).setImage(image).build();
        // Perform the request
        BatchAnnotateImagesResponse response = client.batchAnnotateImages(Arrays.asList(request));
        // Display the results
        response.getResponsesList().stream().forEach(r -> r.getWebDetection().getWebEntitiesList().stream().forEach(entity -> {
            System.out.format("Description: %s%n", entity.getDescription());
            System.out.format("Score: %f%n", entity.getScore());
        }));
    }
}
Also used : Arrays(java.util.Arrays) ImageSource(com.google.cloud.vision.v1.ImageSource) Image(com.google.cloud.vision.v1.Image) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse) IOException(java.io.IOException) Feature(com.google.cloud.vision.v1.Feature) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) ImageSource(com.google.cloud.vision.v1.ImageSource) Image(com.google.cloud.vision.v1.Image) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 73 with AnnotateImageRequest

use of com.google.cloud.vision.v1p3beta1.AnnotateImageRequest in project java-vision by googleapis.

the class DetectWebEntitiesIncludeGeoResults method detectWebEntitiesIncludeGeoResults.

// Find web entities given a local image.
public static void detectWebEntitiesIncludeGeoResults(String filePath) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
        // Read in the local image
        ByteString contents = ByteString.readFrom(new FileInputStream(filePath));
        // Build the image
        Image image = Image.newBuilder().setContent(contents).build();
        // Enable `IncludeGeoResults`
        WebDetectionParams webDetectionParams = WebDetectionParams.newBuilder().setIncludeGeoResults(true).build();
        // Set the parameters for the image
        ImageContext imageContext = ImageContext.newBuilder().setWebDetectionParams(webDetectionParams).build();
        // Create the request with the image, imageContext, and the specified feature: web detection
        AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(Feature.newBuilder().setType(Type.WEB_DETECTION)).setImage(image).setImageContext(imageContext).build();
        // Perform the request
        BatchAnnotateImagesResponse response = client.batchAnnotateImages(Arrays.asList(request));
        // Display the results
        response.getResponsesList().stream().forEach(r -> r.getWebDetection().getWebEntitiesList().stream().forEach(entity -> {
            System.out.format("Description: %s%n", entity.getDescription());
            System.out.format("Score: %f%n", entity.getScore());
        }));
    }
}
Also used : WebDetectionParams(com.google.cloud.vision.v1.WebDetectionParams) WebDetectionParams(com.google.cloud.vision.v1.WebDetectionParams) ByteString(com.google.protobuf.ByteString) Arrays(java.util.Arrays) Image(com.google.cloud.vision.v1.Image) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) Type(com.google.cloud.vision.v1.Feature.Type) ImageContext(com.google.cloud.vision.v1.ImageContext) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Feature(com.google.cloud.vision.v1.Feature) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) ByteString(com.google.protobuf.ByteString) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) Image(com.google.cloud.vision.v1.Image) FileInputStream(java.io.FileInputStream) ImageContext(com.google.cloud.vision.v1.ImageContext) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 74 with AnnotateImageRequest

use of com.google.cloud.vision.v1p3beta1.AnnotateImageRequest in project java-vision by googleapis.

the class AsyncBatchAnnotateImages method asyncBatchAnnotateImages.

public static void asyncBatchAnnotateImages(String inputImageUri, String outputUri) throws IOException, ExecutionException, InterruptedException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create()) {
        // You can send multiple images to be annotated, this sample demonstrates how to do this with
        // one image. If you want to use multiple images, you have to create a `AnnotateImageRequest`
        // object for each image that you want annotated.
        // First specify where the vision api can find the image
        ImageSource source = ImageSource.newBuilder().setImageUri(inputImageUri).build();
        Image image = Image.newBuilder().setSource(source).build();
        // Set the type of annotation you want to perform on the image
        // https://cloud.google.com/vision/docs/reference/rpc/google.cloud.vision.v1#google.cloud.vision.v1.Feature.Type
        Feature feature = Feature.newBuilder().setType(Feature.Type.LABEL_DETECTION).build();
        // Build the request object for that one image. Note: for additional images you have to create
        // additional `AnnotateImageRequest` objects and store them in a list to be used below.
        AnnotateImageRequest imageRequest = AnnotateImageRequest.newBuilder().setImage(image).addFeatures(feature).build();
        // Set where to store the results for the images that will be annotated.
        GcsDestination gcsDestination = GcsDestination.newBuilder().setUri(outputUri).build();
        OutputConfig outputConfig = OutputConfig.newBuilder().setGcsDestination(gcsDestination).setBatchSize(// The max number of responses to output in each JSON file
        2).build();
        // Add each `AnnotateImageRequest` object to the batch request and add the output config.
        AsyncBatchAnnotateImagesRequest request = AsyncBatchAnnotateImagesRequest.newBuilder().addRequests(imageRequest).setOutputConfig(outputConfig).build();
        // Make the asynchronous batch request.
        AsyncBatchAnnotateImagesResponse response = imageAnnotatorClient.asyncBatchAnnotateImagesAsync(request).get();
        // The output is written to GCS with the provided output_uri as prefix
        String gcsOutputUri = response.getOutputConfig().getGcsDestination().getUri();
        System.out.format("Output written to GCS with prefix: %s%n", gcsOutputUri);
    }
}
Also used : AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) OutputConfig(com.google.cloud.vision.v1.OutputConfig) AsyncBatchAnnotateImagesRequest(com.google.cloud.vision.v1.AsyncBatchAnnotateImagesRequest) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) AsyncBatchAnnotateImagesResponse(com.google.cloud.vision.v1.AsyncBatchAnnotateImagesResponse) ImageSource(com.google.cloud.vision.v1.ImageSource) Image(com.google.cloud.vision.v1.Image) GcsDestination(com.google.cloud.vision.v1.GcsDestination) Feature(com.google.cloud.vision.v1.Feature)

Example 75 with AnnotateImageRequest

use of com.google.cloud.vision.v1p3beta1.AnnotateImageRequest in project java-vision by googleapis.

the class SetEndpoint method setEndpoint.

// Change your endpoint
public static void setEndpoint() throws IOException {
    // [START vision_set_endpoint]
    ImageAnnotatorSettings settings = ImageAnnotatorSettings.newBuilder().setEndpoint("eu-vision.googleapis.com:443").build();
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    ImageAnnotatorClient client = ImageAnnotatorClient.create(settings);
    // [END vision_set_endpoint]
    ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri("gs://cloud-samples-data/vision/text/screen.jpg").build();
    Image image = Image.newBuilder().setSource(imgSource).build();
    Feature feature = Feature.newBuilder().setType(Feature.Type.TEXT_DETECTION).build();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feature).setImage(image).build();
    List<AnnotateImageRequest> requests = new ArrayList<>();
    requests.add(request);
    BatchAnnotateImagesResponse batchResponse = client.batchAnnotateImages(requests);
    for (AnnotateImageResponse response : batchResponse.getResponsesList()) {
        for (EntityAnnotation annotation : response.getTextAnnotationsList()) {
            System.out.format("Text: %s%n", annotation.getDescription());
            System.out.println("Position:");
            System.out.format("%s%n", annotation.getBoundingPoly());
        }
    }
    client.close();
}
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) ImageAnnotatorSettings(com.google.cloud.vision.v1.ImageAnnotatorSettings) ImageSource(com.google.cloud.vision.v1.ImageSource) 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)77 Image (com.google.cloud.vision.v1.Image)71 BatchAnnotateImagesResponse (com.google.cloud.vision.v1.BatchAnnotateImagesResponse)69 Feature (com.google.cloud.vision.v1.Feature)69 ArrayList (java.util.ArrayList)66 ImageAnnotatorClient (com.google.cloud.vision.v1.ImageAnnotatorClient)65 AnnotateImageResponse (com.google.cloud.vision.v1.AnnotateImageResponse)62 ByteString (com.google.protobuf.ByteString)44 ImageSource (com.google.cloud.vision.v1.ImageSource)41 FileInputStream (java.io.FileInputStream)32 EntityAnnotation (com.google.cloud.vision.v1.EntityAnnotation)28 WebImage (com.google.cloud.vision.v1.WebDetection.WebImage)26 IOException (java.io.IOException)13 SafeSearchAnnotation (com.google.cloud.vision.v1.SafeSearchAnnotation)11 WebDetection (com.google.cloud.vision.v1.WebDetection)11 ImageContext (com.google.cloud.vision.v1.ImageContext)10 LocationInfo (com.google.cloud.vision.v1.LocationInfo)10 Test (org.junit.Test)10 CropHint (com.google.cloud.vision.v1.CropHint)9 WebPage (com.google.cloud.vision.v1.WebDetection.WebPage)9