|
| 1 | +/* |
| 2 | + * Copyright 2020 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 | + * http://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 | + |
| 17 | +package com.example.vision; |
| 18 | + |
| 19 | +// [START vision_batch_annotate_files] |
| 20 | +import com.google.cloud.vision.v1.AnnotateFileRequest; |
| 21 | +import com.google.cloud.vision.v1.AnnotateImageResponse; |
| 22 | +import com.google.cloud.vision.v1.BatchAnnotateFilesRequest; |
| 23 | +import com.google.cloud.vision.v1.BatchAnnotateFilesResponse; |
| 24 | +import com.google.cloud.vision.v1.Block; |
| 25 | +import com.google.cloud.vision.v1.Feature; |
| 26 | +import com.google.cloud.vision.v1.ImageAnnotatorClient; |
| 27 | +import com.google.cloud.vision.v1.InputConfig; |
| 28 | +import com.google.cloud.vision.v1.Page; |
| 29 | +import com.google.cloud.vision.v1.Paragraph; |
| 30 | +import com.google.cloud.vision.v1.Symbol; |
| 31 | +import com.google.cloud.vision.v1.Word; |
| 32 | +import com.google.protobuf.ByteString; |
| 33 | + |
| 34 | +import java.io.IOException; |
| 35 | +import java.nio.file.Files; |
| 36 | +import java.nio.file.Path; |
| 37 | +import java.nio.file.Paths; |
| 38 | + |
| 39 | +public class BatchAnnotateFiles { |
| 40 | + |
| 41 | + public static void batchAnnotateFiles() throws IOException { |
| 42 | + String filePath = "path/to/your/file.pdf"; |
| 43 | + batchAnnotateFiles(filePath); |
| 44 | + } |
| 45 | + |
| 46 | + public static void batchAnnotateFiles(String filePath) throws IOException { |
| 47 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 48 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 49 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 50 | + try (ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create()) { |
| 51 | + // You can send multiple files to be annotated, this sample demonstrates how to do this with |
| 52 | + // one file. If you want to use multiple files, you have to create a `AnnotateImageRequest` |
| 53 | + // object for each file that you want annotated. |
| 54 | + // First read the files contents |
| 55 | + Path path = Paths.get(filePath); |
| 56 | + byte[] data = Files.readAllBytes(path); |
| 57 | + ByteString content = ByteString.copyFrom(data); |
| 58 | + |
| 59 | + // Specify the input config with the file's contents and its type. |
| 60 | + // Supported mime_type: application/pdf, image/tiff, image/gif |
| 61 | + // https://cloud.google.com/vision/docs/reference/rpc/google.cloud.vision.v1#inputconfig |
| 62 | + InputConfig inputConfig = |
| 63 | + InputConfig.newBuilder().setMimeType("application/pdf").setContent(content).build(); |
| 64 | + |
| 65 | + // Set the type of annotation you want to perform on the file |
| 66 | + // https://cloud.google.com/vision/docs/reference/rpc/google.cloud.vision.v1#google.cloud.vision.v1.Feature.Type |
| 67 | + Feature feature = Feature.newBuilder().setType(Feature.Type.DOCUMENT_TEXT_DETECTION).build(); |
| 68 | + |
| 69 | + // Build the request object for that one file. Note: for additional file you have to create |
| 70 | + // additional `AnnotateFileRequest` objects and store them in a list to be used below. |
| 71 | + // Since we are sending a file of type `application/pdf`, we can use the `pages` field to |
| 72 | + // specify which pages to process. The service can process up to 5 pages per document file. |
| 73 | + // https://cloud.google.com/vision/docs/reference/rpc/google.cloud.vision.v1#google.cloud.vision.v1.AnnotateFileRequest |
| 74 | + AnnotateFileRequest fileRequest = |
| 75 | + AnnotateFileRequest.newBuilder() |
| 76 | + .setInputConfig(inputConfig) |
| 77 | + .addFeatures(feature) |
| 78 | + .addPages(1) // Process the first page |
| 79 | + .addPages(2) // Process the second page |
| 80 | + .addPages(-1) // Process the last page |
| 81 | + .build(); |
| 82 | + |
| 83 | + // Add each `AnnotateFileRequest` object to the batch request. |
| 84 | + BatchAnnotateFilesRequest request = |
| 85 | + BatchAnnotateFilesRequest.newBuilder().addRequests(fileRequest).build(); |
| 86 | + |
| 87 | + // Make the synchronous batch request. |
| 88 | + BatchAnnotateFilesResponse response = imageAnnotatorClient.batchAnnotateFiles(request); |
| 89 | + |
| 90 | + // Process the results, just get the first result, since only one file was sent in this |
| 91 | + // sample. |
| 92 | + for (AnnotateImageResponse imageResponse : |
| 93 | + response.getResponsesList().get(0).getResponsesList()) { |
| 94 | + System.out.printf("Full text: %s\n", imageResponse.getFullTextAnnotation().getText()); |
| 95 | + for (Page page : imageResponse.getFullTextAnnotation().getPagesList()) { |
| 96 | + for (Block block : page.getBlocksList()) { |
| 97 | + System.out.printf("\nBlock confidence: %s\n", block.getConfidence()); |
| 98 | + for (Paragraph par : block.getParagraphsList()) { |
| 99 | + System.out.printf("\tParagraph confidence: %s\n", par.getConfidence()); |
| 100 | + for (Word word : par.getWordsList()) { |
| 101 | + System.out.printf("\t\tWord confidence: %s\n", word.getConfidence()); |
| 102 | + for (Symbol symbol : word.getSymbolsList()) { |
| 103 | + System.out.printf( |
| 104 | + "\t\t\tSymbol: %s, (confidence: %s)\n", |
| 105 | + symbol.getText(), symbol.getConfidence()); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | +// [END vision_batch_annotate_files] |
0 commit comments