Search in sources :

Example 16 with GcsDestination

use of com.google.cloud.vision.v1p4beta1.GcsDestination 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)

Aggregations

BatchTranslateMetadata (com.google.cloud.translate.v3.BatchTranslateMetadata)4 BatchTranslateResponse (com.google.cloud.translate.v3.BatchTranslateResponse)4 BatchTranslateTextRequest (com.google.cloud.translate.v3.BatchTranslateTextRequest)4 GcsDestination (com.google.cloud.translate.v3.GcsDestination)4 GcsSource (com.google.cloud.translate.v3.GcsSource)4 InputConfig (com.google.cloud.translate.v3.InputConfig)4 LocationName (com.google.cloud.translate.v3.LocationName)4 OutputConfig (com.google.cloud.translate.v3.OutputConfig)4 TranslationServiceClient (com.google.cloud.translate.v3.TranslationServiceClient)4 GcsDestination (com.google.cloud.vision.v1.GcsDestination)4 OutputConfig (com.google.cloud.vision.v1.OutputConfig)4 Blob (com.google.cloud.storage.Blob)3 Bucket (com.google.cloud.storage.Bucket)3 Storage (com.google.cloud.storage.Storage)3 Page (com.google.api.gax.paging.Page)2 GcsDestination (com.google.cloud.automl.v1.GcsDestination)2 GlossaryName (com.google.cloud.translate.v3.GlossaryName)2 TranslateTextGlossaryConfig (com.google.cloud.translate.v3.TranslateTextGlossaryConfig)2 GcsDestination (com.google.cloud.translate.v3beta1.GcsDestination)2 GcsSource (com.google.cloud.translate.v3beta1.GcsSource)2