From c6d960f6cec537c1dd0c2e20a443cc74804bba77 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Fri, 25 Oct 2019 15:29:36 -0700 Subject: [PATCH] feat: make repo releasable, add parent/bom (#1) * feat: make repo releasable, add parent/bom * deps: fix dependency declarations --- .../v1/VisionAsyncBatchAnnotateImages.java | 123 +++++++++++++++ .../vision/v1/VisionBatchAnnotateFiles.java | 145 ++++++++++++++++++ .../v1/VisionBatchAnnotateFilesGcs.java | 135 ++++++++++++++++ 3 files changed, 403 insertions(+) create mode 100644 vision/src/main/java/com/google/cloud/examples/vision/v1/VisionAsyncBatchAnnotateImages.java create mode 100644 vision/src/main/java/com/google/cloud/examples/vision/v1/VisionBatchAnnotateFiles.java create mode 100644 vision/src/main/java/com/google/cloud/examples/vision/v1/VisionBatchAnnotateFilesGcs.java diff --git a/vision/src/main/java/com/google/cloud/examples/vision/v1/VisionAsyncBatchAnnotateImages.java b/vision/src/main/java/com/google/cloud/examples/vision/v1/VisionAsyncBatchAnnotateImages.java new file mode 100644 index 00000000000..f7ebf8ff6c0 --- /dev/null +++ b/vision/src/main/java/com/google/cloud/examples/vision/v1/VisionAsyncBatchAnnotateImages.java @@ -0,0 +1,123 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +// DO NOT EDIT! This is a generated sample ("LongRunningRequestAsync", +// "vision_async_batch_annotate_images") +// sample-metadata: +// title: Async Batch Image Annotation +// description: Perform async batch image annotation +// usage: gradle run +// -PmainClass=com.google.cloud.examples.vision.v1.VisionAsyncBatchAnnotateImages +// [--args='[--input_image_uri "gs://cloud-samples-data/vision/label/wakeupcat.jpg"] [--output_uri +// "gs://your-bucket/prefix/"]'] + +package com.google.cloud.examples.vision.v1; + +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.vision.v1.AnnotateImageRequest; +import com.google.cloud.vision.v1.AsyncBatchAnnotateImagesRequest; +import com.google.cloud.vision.v1.AsyncBatchAnnotateImagesResponse; +import com.google.cloud.vision.v1.Feature; +import com.google.cloud.vision.v1.GcsDestination; +import com.google.cloud.vision.v1.Image; +import com.google.cloud.vision.v1.ImageAnnotatorClient; +import com.google.cloud.vision.v1.ImageSource; +import com.google.cloud.vision.v1.OperationMetadata; +import com.google.cloud.vision.v1.OutputConfig; +import java.util.Arrays; +import java.util.List; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.DefaultParser; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.Options; + +public class VisionAsyncBatchAnnotateImages { + // [START vision_async_batch_annotate_images] + /* + * Please include the following imports to run this sample. + * + * import com.google.api.gax.longrunning.OperationFuture; + * import com.google.cloud.vision.v1.AnnotateImageRequest; + * import com.google.cloud.vision.v1.AsyncBatchAnnotateImagesRequest; + * import com.google.cloud.vision.v1.AsyncBatchAnnotateImagesResponse; + * import com.google.cloud.vision.v1.Feature; + * import com.google.cloud.vision.v1.GcsDestination; + * import com.google.cloud.vision.v1.Image; + * import com.google.cloud.vision.v1.ImageAnnotatorClient; + * import com.google.cloud.vision.v1.ImageSource; + * import com.google.cloud.vision.v1.OperationMetadata; + * import com.google.cloud.vision.v1.OutputConfig; + * import java.util.Arrays; + * import java.util.List; + */ + + /** Perform async batch image annotation */ + public static void sampleAsyncBatchAnnotateImages(String inputImageUri, String outputUri) { + try (ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create()) { + // inputImageUri = "gs://cloud-samples-data/vision/label/wakeupcat.jpg"; + // outputUri = "gs://your-bucket/prefix/"; + ImageSource source = ImageSource.newBuilder().setImageUri(inputImageUri).build(); + Image image = Image.newBuilder().setSource(source).build(); + Feature.Type type = Feature.Type.LABEL_DETECTION; + Feature featuresElement = Feature.newBuilder().setType(type).build(); + Feature.Type type2 = Feature.Type.IMAGE_PROPERTIES; + Feature featuresElement2 = Feature.newBuilder().setType(type2).build(); + List features = Arrays.asList(featuresElement, featuresElement2); + AnnotateImageRequest requestsElement = + AnnotateImageRequest.newBuilder().setImage(image).addAllFeatures(features).build(); + List requests = Arrays.asList(requestsElement); + GcsDestination gcsDestination = GcsDestination.newBuilder().setUri(outputUri).build(); + + // The max number of responses to output in each JSON file + int batchSize = 2; + OutputConfig outputConfig = + OutputConfig.newBuilder() + .setGcsDestination(gcsDestination) + .setBatchSize(batchSize) + .build(); + AsyncBatchAnnotateImagesRequest request = + AsyncBatchAnnotateImagesRequest.newBuilder() + .addAllRequests(requests) + .setOutputConfig(outputConfig) + .build(); + OperationFuture future = + imageAnnotatorClient.asyncBatchAnnotateImagesAsync(request); + + System.out.println("Waiting for operation to complete..."); + AsyncBatchAnnotateImagesResponse response = future.get(); + // The output is written to GCS with the provided output_uri as prefix + String gcsOutputUri = response.getOutputConfig().getGcsDestination().getUri(); + System.out.printf("Output written to GCS with prefix: %s\n", gcsOutputUri); + } catch (Exception exception) { + System.err.println("Failed to create the client due to: " + exception); + } + } + // [END vision_async_batch_annotate_images] + + public static void main(String[] args) throws Exception { + Options options = new Options(); + options.addOption( + Option.builder("").required(false).hasArg(true).longOpt("input_image_uri").build()); + options.addOption( + Option.builder("").required(false).hasArg(true).longOpt("output_uri").build()); + + CommandLine cl = (new DefaultParser()).parse(options, args); + String inputImageUri = + cl.getOptionValue("input_image_uri", "gs://cloud-samples-data/vision/label/wakeupcat.jpg"); + String outputUri = cl.getOptionValue("output_uri", "gs://your-bucket/prefix/"); + + sampleAsyncBatchAnnotateImages(inputImageUri, outputUri); + } +} diff --git a/vision/src/main/java/com/google/cloud/examples/vision/v1/VisionBatchAnnotateFiles.java b/vision/src/main/java/com/google/cloud/examples/vision/v1/VisionBatchAnnotateFiles.java new file mode 100644 index 00000000000..d3fdc3df774 --- /dev/null +++ b/vision/src/main/java/com/google/cloud/examples/vision/v1/VisionBatchAnnotateFiles.java @@ -0,0 +1,145 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +// DO NOT EDIT! This is a generated sample ("Request", "vision_batch_annotate_files") +// sample-metadata: +// title: +// description: Perform batch file annotation +// usage: gradle run -PmainClass=com.google.cloud.examples.vision.v1.VisionBatchAnnotateFiles +// [--args='[--file_path "resources/kafka.pdf"]'] + +package com.google.cloud.examples.vision.v1; + +import com.google.cloud.vision.v1.AnnotateFileRequest; +import com.google.cloud.vision.v1.AnnotateImageResponse; +import com.google.cloud.vision.v1.BatchAnnotateFilesRequest; +import com.google.cloud.vision.v1.BatchAnnotateFilesResponse; +import com.google.cloud.vision.v1.Block; +import com.google.cloud.vision.v1.Feature; +import com.google.cloud.vision.v1.ImageAnnotatorClient; +import com.google.cloud.vision.v1.InputConfig; +import com.google.cloud.vision.v1.Page; +import com.google.cloud.vision.v1.Paragraph; +import com.google.cloud.vision.v1.Symbol; +import com.google.cloud.vision.v1.Word; +import com.google.protobuf.ByteString; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.List; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.DefaultParser; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.Options; + +public class VisionBatchAnnotateFiles { + // [START vision_batch_annotate_files] + /* + * Please include the following imports to run this sample. + * + * import com.google.cloud.vision.v1.AnnotateFileRequest; + * import com.google.cloud.vision.v1.AnnotateImageResponse; + * import com.google.cloud.vision.v1.BatchAnnotateFilesRequest; + * import com.google.cloud.vision.v1.BatchAnnotateFilesResponse; + * import com.google.cloud.vision.v1.Block; + * import com.google.cloud.vision.v1.Feature; + * import com.google.cloud.vision.v1.ImageAnnotatorClient; + * import com.google.cloud.vision.v1.InputConfig; + * import com.google.cloud.vision.v1.Page; + * import com.google.cloud.vision.v1.Paragraph; + * import com.google.cloud.vision.v1.Symbol; + * import com.google.cloud.vision.v1.Word; + * import com.google.protobuf.ByteString; + * import java.nio.file.Files; + * import java.nio.file.Path; + * import java.nio.file.Paths; + * import java.util.Arrays; + * import java.util.List; + */ + + /** + * Perform batch file annotation + * + * @param filePath Path to local pdf file, e.g. /path/document.pdf + */ + public static void sampleBatchAnnotateFiles(String filePath) { + try (ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create()) { + // filePath = "resources/kafka.pdf"; + + // Supported mime_type: application/pdf, image/tiff, image/gif + String mimeType = "application/pdf"; + Path path = Paths.get(filePath); + byte[] data = Files.readAllBytes(path); + ByteString content = ByteString.copyFrom(data); + InputConfig inputConfig = + InputConfig.newBuilder().setMimeType(mimeType).setContent(content).build(); + Feature.Type type = Feature.Type.DOCUMENT_TEXT_DETECTION; + Feature featuresElement = Feature.newBuilder().setType(type).build(); + List features = Arrays.asList(featuresElement); + + // The service can process up to 5 pages per document file. Here we specify the first, second, + // and + // last page of the document to be processed. + int pagesElement = 1; + int pagesElement2 = 2; + int pagesElement3 = -1; + List pages = Arrays.asList(pagesElement, pagesElement2, pagesElement3); + AnnotateFileRequest requestsElement = + AnnotateFileRequest.newBuilder() + .setInputConfig(inputConfig) + .addAllFeatures(features) + .addAllPages(pages) + .build(); + List requests = Arrays.asList(requestsElement); + BatchAnnotateFilesRequest request = + BatchAnnotateFilesRequest.newBuilder().addAllRequests(requests).build(); + BatchAnnotateFilesResponse response = imageAnnotatorClient.batchAnnotateFiles(request); + for (AnnotateImageResponse imageResponse : + response.getResponsesList().get(0).getResponsesList()) { + System.out.printf("Full text: %s\n", imageResponse.getFullTextAnnotation().getText()); + for (Page page : imageResponse.getFullTextAnnotation().getPagesList()) { + for (Block block : page.getBlocksList()) { + System.out.printf("\nBlock confidence: %s\n", block.getConfidence()); + for (Paragraph par : block.getParagraphsList()) { + System.out.printf("\tParagraph confidence: %s\n", par.getConfidence()); + for (Word word : par.getWordsList()) { + System.out.printf("\t\tWord confidence: %s\n", word.getConfidence()); + for (Symbol symbol : word.getSymbolsList()) { + System.out.printf( + "\t\t\tSymbol: %s, (confidence: %s)\n", + symbol.getText(), symbol.getConfidence()); + } + } + } + } + } + } + } catch (Exception exception) { + System.err.println("Failed to create the client due to: " + exception); + } + } + // [END vision_batch_annotate_files] + + public static void main(String[] args) throws Exception { + Options options = new Options(); + options.addOption(Option.builder("").required(false).hasArg(true).longOpt("file_path").build()); + + CommandLine cl = (new DefaultParser()).parse(options, args); + String filePath = cl.getOptionValue("file_path", "resources/kafka.pdf"); + + sampleBatchAnnotateFiles(filePath); + } +} diff --git a/vision/src/main/java/com/google/cloud/examples/vision/v1/VisionBatchAnnotateFilesGcs.java b/vision/src/main/java/com/google/cloud/examples/vision/v1/VisionBatchAnnotateFilesGcs.java new file mode 100644 index 00000000000..2625c079309 --- /dev/null +++ b/vision/src/main/java/com/google/cloud/examples/vision/v1/VisionBatchAnnotateFilesGcs.java @@ -0,0 +1,135 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +// DO NOT EDIT! This is a generated sample ("Request", "vision_batch_annotate_files_gcs") +// sample-metadata: +// title: +// description: Perform batch file annotation +// usage: gradle run -PmainClass=com.google.cloud.examples.vision.v1.VisionBatchAnnotateFilesGcs +// [--args='[--storage_uri "gs://cloud-samples-data/vision/document_understanding/kafka.pdf"]'] + +package com.google.cloud.examples.vision.v1; + +import com.google.cloud.vision.v1.AnnotateFileRequest; +import com.google.cloud.vision.v1.AnnotateImageResponse; +import com.google.cloud.vision.v1.BatchAnnotateFilesRequest; +import com.google.cloud.vision.v1.BatchAnnotateFilesResponse; +import com.google.cloud.vision.v1.Block; +import com.google.cloud.vision.v1.Feature; +import com.google.cloud.vision.v1.GcsSource; +import com.google.cloud.vision.v1.ImageAnnotatorClient; +import com.google.cloud.vision.v1.InputConfig; +import com.google.cloud.vision.v1.Page; +import com.google.cloud.vision.v1.Paragraph; +import com.google.cloud.vision.v1.Symbol; +import com.google.cloud.vision.v1.Word; +import java.util.Arrays; +import java.util.List; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.DefaultParser; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.Options; + +public class VisionBatchAnnotateFilesGcs { + // [START vision_batch_annotate_files_gcs] + /* + * Please include the following imports to run this sample. + * + * import com.google.cloud.vision.v1.AnnotateFileRequest; + * import com.google.cloud.vision.v1.AnnotateImageResponse; + * import com.google.cloud.vision.v1.BatchAnnotateFilesRequest; + * import com.google.cloud.vision.v1.BatchAnnotateFilesResponse; + * import com.google.cloud.vision.v1.Block; + * import com.google.cloud.vision.v1.Feature; + * import com.google.cloud.vision.v1.GcsSource; + * import com.google.cloud.vision.v1.ImageAnnotatorClient; + * import com.google.cloud.vision.v1.InputConfig; + * import com.google.cloud.vision.v1.Page; + * import com.google.cloud.vision.v1.Paragraph; + * import com.google.cloud.vision.v1.Symbol; + * import com.google.cloud.vision.v1.Word; + * import java.util.Arrays; + * import java.util.List; + */ + + /** + * Perform batch file annotation + * + * @param storageUri Cloud Storage URI to source image in the format gs://[bucket]/[file] + */ + public static void sampleBatchAnnotateFiles(String storageUri) { + try (ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create()) { + // storageUri = "gs://cloud-samples-data/vision/document_understanding/kafka.pdf"; + GcsSource gcsSource = GcsSource.newBuilder().setUri(storageUri).build(); + InputConfig inputConfig = InputConfig.newBuilder().setGcsSource(gcsSource).build(); + Feature.Type type = Feature.Type.DOCUMENT_TEXT_DETECTION; + Feature featuresElement = Feature.newBuilder().setType(type).build(); + List features = Arrays.asList(featuresElement); + + // The service can process up to 5 pages per document file. + // Here we specify the first, second, and last page of the document to be processed. + int pagesElement = 1; + int pagesElement2 = 2; + int pagesElement3 = -1; + List pages = Arrays.asList(pagesElement, pagesElement2, pagesElement3); + AnnotateFileRequest requestsElement = + AnnotateFileRequest.newBuilder() + .setInputConfig(inputConfig) + .addAllFeatures(features) + .addAllPages(pages) + .build(); + List requests = Arrays.asList(requestsElement); + BatchAnnotateFilesRequest request = + BatchAnnotateFilesRequest.newBuilder().addAllRequests(requests).build(); + BatchAnnotateFilesResponse response = imageAnnotatorClient.batchAnnotateFiles(request); + for (AnnotateImageResponse imageResponse : + response.getResponsesList().get(0).getResponsesList()) { + System.out.printf("Full text: %s\n", imageResponse.getFullTextAnnotation().getText()); + for (Page page : imageResponse.getFullTextAnnotation().getPagesList()) { + for (Block block : page.getBlocksList()) { + System.out.printf("\nBlock confidence: %s\n", block.getConfidence()); + for (Paragraph par : block.getParagraphsList()) { + System.out.printf("\tParagraph confidence: %s\n", par.getConfidence()); + for (Word word : par.getWordsList()) { + System.out.printf("\t\tWord confidence: %s\n", word.getConfidence()); + for (Symbol symbol : word.getSymbolsList()) { + System.out.printf( + "\t\t\tSymbol: %s, (confidence: %s)\n", + symbol.getText(), symbol.getConfidence()); + } + } + } + } + } + } + } catch (Exception exception) { + System.err.println("Failed to create the client due to: " + exception); + } + } + // [END vision_batch_annotate_files_gcs] + + public static void main(String[] args) throws Exception { + Options options = new Options(); + options.addOption( + Option.builder("").required(false).hasArg(true).longOpt("storage_uri").build()); + + CommandLine cl = (new DefaultParser()).parse(options, args); + String storageUri = + cl.getOptionValue( + "storage_uri", "gs://cloud-samples-data/vision/document_understanding/kafka.pdf"); + + sampleBatchAnnotateFiles(storageUri); + } +}