|
| 1 | +/* |
| 2 | + * Copyright 2019 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +// DO NOT EDIT! This is a generated sample ("Request", "vision_batch_annotate_files") |
| 17 | +// sample-metadata: |
| 18 | +// title: |
| 19 | +// description: Perform batch file annotation |
| 20 | +// usage: gradle run -PmainClass=com.google.cloud.examples.vision.v1.VisionBatchAnnotateFiles |
| 21 | +// [--args='[--file_path "resources/kafka.pdf"]'] |
| 22 | + |
| 23 | +package com.google.cloud.examples.vision.v1; |
| 24 | + |
| 25 | +import com.google.cloud.vision.v1.AnnotateFileRequest; |
| 26 | +import com.google.cloud.vision.v1.AnnotateImageResponse; |
| 27 | +import com.google.cloud.vision.v1.BatchAnnotateFilesRequest; |
| 28 | +import com.google.cloud.vision.v1.BatchAnnotateFilesResponse; |
| 29 | +import com.google.cloud.vision.v1.Block; |
| 30 | +import com.google.cloud.vision.v1.Feature; |
| 31 | +import com.google.cloud.vision.v1.ImageAnnotatorClient; |
| 32 | +import com.google.cloud.vision.v1.InputConfig; |
| 33 | +import com.google.cloud.vision.v1.Page; |
| 34 | +import com.google.cloud.vision.v1.Paragraph; |
| 35 | +import com.google.cloud.vision.v1.Symbol; |
| 36 | +import com.google.cloud.vision.v1.Word; |
| 37 | +import com.google.protobuf.ByteString; |
| 38 | +import java.nio.file.Files; |
| 39 | +import java.nio.file.Path; |
| 40 | +import java.nio.file.Paths; |
| 41 | +import java.util.Arrays; |
| 42 | +import java.util.List; |
| 43 | +import org.apache.commons.cli.CommandLine; |
| 44 | +import org.apache.commons.cli.DefaultParser; |
| 45 | +import org.apache.commons.cli.Option; |
| 46 | +import org.apache.commons.cli.Options; |
| 47 | + |
| 48 | +public class VisionBatchAnnotateFiles { |
| 49 | + // [START vision_batch_annotate_files] |
| 50 | + /* |
| 51 | + * Please include the following imports to run this sample. |
| 52 | + * |
| 53 | + * import com.google.cloud.vision.v1.AnnotateFileRequest; |
| 54 | + * import com.google.cloud.vision.v1.AnnotateImageResponse; |
| 55 | + * import com.google.cloud.vision.v1.BatchAnnotateFilesRequest; |
| 56 | + * import com.google.cloud.vision.v1.BatchAnnotateFilesResponse; |
| 57 | + * import com.google.cloud.vision.v1.Block; |
| 58 | + * import com.google.cloud.vision.v1.Feature; |
| 59 | + * import com.google.cloud.vision.v1.ImageAnnotatorClient; |
| 60 | + * import com.google.cloud.vision.v1.InputConfig; |
| 61 | + * import com.google.cloud.vision.v1.Page; |
| 62 | + * import com.google.cloud.vision.v1.Paragraph; |
| 63 | + * import com.google.cloud.vision.v1.Symbol; |
| 64 | + * import com.google.cloud.vision.v1.Word; |
| 65 | + * import com.google.protobuf.ByteString; |
| 66 | + * import java.nio.file.Files; |
| 67 | + * import java.nio.file.Path; |
| 68 | + * import java.nio.file.Paths; |
| 69 | + * import java.util.Arrays; |
| 70 | + * import java.util.List; |
| 71 | + */ |
| 72 | + |
| 73 | + /** |
| 74 | + * Perform batch file annotation |
| 75 | + * |
| 76 | + * @param filePath Path to local pdf file, e.g. /path/document.pdf |
| 77 | + */ |
| 78 | + public static void sampleBatchAnnotateFiles(String filePath) { |
| 79 | + try (ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create()) { |
| 80 | + // filePath = "resources/kafka.pdf"; |
| 81 | + |
| 82 | + // Supported mime_type: application/pdf, image/tiff, image/gif |
| 83 | + String mimeType = "application/pdf"; |
| 84 | + Path path = Paths.get(filePath); |
| 85 | + byte[] data = Files.readAllBytes(path); |
| 86 | + ByteString content = ByteString.copyFrom(data); |
| 87 | + InputConfig inputConfig = |
| 88 | + InputConfig.newBuilder().setMimeType(mimeType).setContent(content).build(); |
| 89 | + Feature.Type type = Feature.Type.DOCUMENT_TEXT_DETECTION; |
| 90 | + Feature featuresElement = Feature.newBuilder().setType(type).build(); |
| 91 | + List<Feature> features = Arrays.asList(featuresElement); |
| 92 | + |
| 93 | + // The service can process up to 5 pages per document file. Here we specify the first, second, |
| 94 | + // and |
| 95 | + // last page of the document to be processed. |
| 96 | + int pagesElement = 1; |
| 97 | + int pagesElement2 = 2; |
| 98 | + int pagesElement3 = -1; |
| 99 | + List<Integer> pages = Arrays.asList(pagesElement, pagesElement2, pagesElement3); |
| 100 | + AnnotateFileRequest requestsElement = |
| 101 | + AnnotateFileRequest.newBuilder() |
| 102 | + .setInputConfig(inputConfig) |
| 103 | + .addAllFeatures(features) |
| 104 | + .addAllPages(pages) |
| 105 | + .build(); |
| 106 | + List<AnnotateFileRequest> requests = Arrays.asList(requestsElement); |
| 107 | + BatchAnnotateFilesRequest request = |
| 108 | + BatchAnnotateFilesRequest.newBuilder().addAllRequests(requests).build(); |
| 109 | + BatchAnnotateFilesResponse response = imageAnnotatorClient.batchAnnotateFiles(request); |
| 110 | + for (AnnotateImageResponse imageResponse : |
| 111 | + response.getResponsesList().get(0).getResponsesList()) { |
| 112 | + System.out.printf("Full text: %s\n", imageResponse.getFullTextAnnotation().getText()); |
| 113 | + for (Page page : imageResponse.getFullTextAnnotation().getPagesList()) { |
| 114 | + for (Block block : page.getBlocksList()) { |
| 115 | + System.out.printf("\nBlock confidence: %s\n", block.getConfidence()); |
| 116 | + for (Paragraph par : block.getParagraphsList()) { |
| 117 | + System.out.printf("\tParagraph confidence: %s\n", par.getConfidence()); |
| 118 | + for (Word word : par.getWordsList()) { |
| 119 | + System.out.printf("\t\tWord confidence: %s\n", word.getConfidence()); |
| 120 | + for (Symbol symbol : word.getSymbolsList()) { |
| 121 | + System.out.printf( |
| 122 | + "\t\t\tSymbol: %s, (confidence: %s)\n", |
| 123 | + symbol.getText(), symbol.getConfidence()); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + } catch (Exception exception) { |
| 131 | + System.err.println("Failed to create the client due to: " + exception); |
| 132 | + } |
| 133 | + } |
| 134 | + // [END vision_batch_annotate_files] |
| 135 | + |
| 136 | + public static void main(String[] args) throws Exception { |
| 137 | + Options options = new Options(); |
| 138 | + options.addOption(Option.builder("").required(false).hasArg(true).longOpt("file_path").build()); |
| 139 | + |
| 140 | + CommandLine cl = (new DefaultParser()).parse(options, args); |
| 141 | + String filePath = cl.getOptionValue("file_path", "resources/kafka.pdf"); |
| 142 | + |
| 143 | + sampleBatchAnnotateFiles(filePath); |
| 144 | + } |
| 145 | +} |
0 commit comments