Skip to content

Commit 6bcea63

Browse files
nirupa-kumarnnegrey
authored andcommitted
samples: Beta feature : Vision : PDF/TIFF/GIF document feature detection (#1349)
* Beta feature : Vision : PDF/TIFF/GIF document feature detection * Beta feature : Vision : PDF/TIFF/GIF document feature detection : Add AsyncBatchAnnotateImages sample * Update after review comments * Update after review comments
1 parent f8b76b9 commit 6bcea63

File tree

5 files changed

+411
-6
lines changed

5 files changed

+411
-6
lines changed

vision/snippets/resources/kafka.pdf

85.2 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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+
* 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_async_batch_annotate_images_beta]
20+
import com.google.api.core.ApiFuture;
21+
import com.google.api.gax.paging.Page;
22+
import com.google.cloud.storage.Blob;
23+
import com.google.cloud.storage.Bucket;
24+
import com.google.cloud.storage.Storage;
25+
import com.google.cloud.storage.Storage.BlobListOption;
26+
import com.google.cloud.storage.StorageOptions;
27+
import com.google.cloud.vision.v1p4beta1.AnnotateImageRequest;
28+
import com.google.cloud.vision.v1p4beta1.AsyncBatchAnnotateImagesRequest;
29+
import com.google.cloud.vision.v1p4beta1.BatchAnnotateImagesResponse.Builder;
30+
import com.google.cloud.vision.v1p4beta1.BatchAnnotateImagesResponse;
31+
import com.google.cloud.vision.v1p4beta1.Feature;
32+
import com.google.cloud.vision.v1p4beta1.Feature.Type;
33+
import com.google.cloud.vision.v1p4beta1.GcsDestination;
34+
import com.google.cloud.vision.v1p4beta1.Image;
35+
import com.google.cloud.vision.v1p4beta1.ImageAnnotatorClient;
36+
import com.google.cloud.vision.v1p4beta1.ImageSource;
37+
import com.google.cloud.vision.v1p4beta1.OutputConfig;
38+
39+
import com.google.longrunning.Operation;
40+
import com.google.protobuf.util.JsonFormat;
41+
import java.util.ArrayList;
42+
import java.util.List;
43+
import java.util.concurrent.TimeUnit;
44+
import java.util.regex.Matcher;
45+
import java.util.regex.Pattern;
46+
47+
public class AsyncBatchAnnotateImagesGcs {
48+
49+
// Performs asynchronous batch annotation of images on Google Cloud Storage
50+
public static void asyncBatchAnnotateImagesGcs(String gcsSourcePath, String gcsDestinationPath)
51+
throws Exception {
52+
// String gcsSourcePath = "gs://YOUR_BUCKET_ID/path_to_your_data";
53+
// String gcsDestinationPath = "gs://YOUR_BUCKET_ID/path_to_store_annotation";
54+
55+
try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
56+
List<AnnotateImageRequest> requests = new ArrayList<>();
57+
58+
ImageSource imgSource = ImageSource.newBuilder().setImageUri(gcsSourcePath).build();
59+
60+
Image image = Image.newBuilder().setSource(imgSource).build();
61+
62+
// Set the GCS destination path for where to save the results.
63+
GcsDestination gcsDestination =
64+
GcsDestination.newBuilder().setUri(gcsDestinationPath).build();
65+
66+
// Create the configuration for the output with the batch size.
67+
// The batch size sets how many pages should be grouped into each json output file.
68+
OutputConfig outputConfig =
69+
OutputConfig.newBuilder().setGcsDestination(gcsDestination).setBatchSize(2).build();
70+
71+
// Select the Features required by the vision API
72+
Feature features =
73+
Feature.newBuilder()
74+
.setType(Type.LABEL_DETECTION)
75+
.setType(Type.TEXT_DETECTION)
76+
.setType(Type.IMAGE_PROPERTIES)
77+
.build();
78+
79+
// Build the request
80+
AnnotateImageRequest annotateImageRequest =
81+
AnnotateImageRequest.newBuilder().setImage(image).addFeatures(features).build();
82+
83+
requests.add(annotateImageRequest);
84+
AsyncBatchAnnotateImagesRequest request =
85+
AsyncBatchAnnotateImagesRequest.newBuilder()
86+
.addAllRequests(requests)
87+
.setOutputConfig(outputConfig)
88+
.build();
89+
90+
ApiFuture<Operation> future = client.asyncBatchAnnotateImagesCallable().futureCall(request);
91+
// Wait for the request to finish. (The result is not used, since the API saves the result to
92+
// the specified location on GCS.)
93+
Operation response = future.get(180, TimeUnit.SECONDS);
94+
95+
System.out.println("Waiting for the operation to finish.");
96+
97+
// Once the request has completed and the output has been
98+
// written to GCS, we can list all the output files.
99+
Storage storage = StorageOptions.getDefaultInstance().getService();
100+
101+
// Get the destination location from the gcsDestinationPath
102+
Pattern pattern = Pattern.compile("gs://([^/]+)/(.+)");
103+
Matcher matcher = pattern.matcher(gcsDestinationPath);
104+
105+
if (matcher.find()) {
106+
String bucketName = matcher.group(1);
107+
String prefix = matcher.group(2);
108+
109+
// Get the list of objects with the given prefix from the GCS bucket
110+
Bucket bucket = storage.get(bucketName);
111+
Page<Blob> pageList = bucket.list(BlobListOption.prefix(prefix));
112+
113+
Blob firstOutputFile = null;
114+
115+
// List objects with the given prefix.
116+
System.out.println("Output files:");
117+
for (Blob blob : pageList.iterateAll()) {
118+
System.out.println(blob.getName());
119+
120+
// Process the first output file from GCS.
121+
// Since we specified batch size = 2, the first response contains
122+
// the first two image requests
123+
if (firstOutputFile == null) {
124+
firstOutputFile = blob;
125+
}
126+
}
127+
128+
// Get the contents of the file and convert the JSON contents to an
129+
// BatchAnnotateImagesResponse
130+
// object. If the Blob is small read all its content in one request
131+
// (Note: the file is a .json file)
132+
// Storage guide: https://cloud.google.com/storage/docs/downloading-objects
133+
String jsonContents = new String(firstOutputFile.getContent());
134+
Builder builder = BatchAnnotateImagesResponse.newBuilder();
135+
JsonFormat.parser().merge(jsonContents, builder);
136+
137+
// Build the AnnotateFileResponse object
138+
BatchAnnotateImagesResponse batchAnnotateImagesResponse = builder.build();
139+
140+
// Here we print the response for the first image
141+
// The response contains more information:
142+
// annotation/pages/blocks/paragraphs/words/symbols/colors
143+
// including confidence score and bounding boxes
144+
System.out.format("\nResponse: %s\n", batchAnnotateImagesResponse.getResponses(0));
145+
146+
} else {
147+
System.out.println("No MATCH");
148+
}
149+
} catch (Exception e) {
150+
System.out.println("Error during asyncBatchAnnotateImagesGcs: \n" + e.toString());
151+
}
152+
}
153+
}
154+
155+
// [END vision_async_batch_annotate_images_beta]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
* 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_beta]
20+
import com.google.api.core.ApiFuture;
21+
import com.google.cloud.vision.v1p4beta1.AnnotateFileRequest;
22+
import com.google.cloud.vision.v1p4beta1.AnnotateFileResponse;
23+
import com.google.cloud.vision.v1p4beta1.BatchAnnotateFilesRequest;
24+
import com.google.cloud.vision.v1p4beta1.BatchAnnotateFilesResponse;
25+
import com.google.cloud.vision.v1p4beta1.Block;
26+
import com.google.cloud.vision.v1p4beta1.Feature;
27+
import com.google.cloud.vision.v1p4beta1.Feature.Type;
28+
import com.google.cloud.vision.v1p4beta1.ImageAnnotatorClient;
29+
import com.google.cloud.vision.v1p4beta1.InputConfig;
30+
import com.google.cloud.vision.v1p4beta1.Page;
31+
import com.google.cloud.vision.v1p4beta1.Paragraph;
32+
import com.google.cloud.vision.v1p4beta1.Symbol;
33+
import com.google.cloud.vision.v1p4beta1.TextAnnotation;
34+
import com.google.cloud.vision.v1p4beta1.Word;
35+
import com.google.protobuf.ByteString;
36+
37+
import java.io.FileInputStream;
38+
import java.util.ArrayList;
39+
import java.util.Arrays;
40+
import java.util.List;
41+
42+
public class DetectBatchAnnotateFiles {
43+
44+
// Performs document feature detection on a local PDF/TIFF/GIF file.
45+
public static void detectBatchAnnotateFiles(String filePath) {
46+
// String filePath = "path/to/your_file";
47+
48+
try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
49+
// Annotate the first two pages and the last one (max 5 pages)
50+
// First page starts at 1, and not 0. Last page is -1.
51+
List<Integer> pages = Arrays.asList(1, 2, -1);
52+
ByteString pdfBytes = ByteString.readFrom(new FileInputStream(filePath));
53+
Feature feat = Feature.newBuilder().setType(Type.DOCUMENT_TEXT_DETECTION).build();
54+
// Other supported mime types : 'image/tiff' or 'image/gif'
55+
InputConfig inputConfig =
56+
InputConfig.newBuilder().setMimeType("application/pdf").setContent(pdfBytes).build();
57+
AnnotateFileRequest request =
58+
AnnotateFileRequest.newBuilder()
59+
.addFeatures(feat)
60+
.setInputConfig(inputConfig)
61+
.addAllPages(pages)
62+
.build();
63+
List<AnnotateFileRequest> requests = new ArrayList<>();
64+
requests.add(request);
65+
66+
BatchAnnotateFilesRequest batchAnnotateFilesRequest =
67+
BatchAnnotateFilesRequest.newBuilder().addAllRequests(requests).build();
68+
ApiFuture<BatchAnnotateFilesResponse> future =
69+
client.batchAnnotateFilesCallable().futureCall(batchAnnotateFilesRequest);
70+
BatchAnnotateFilesResponse response = future.get();
71+
72+
// Getting the first response
73+
AnnotateFileResponse annotateFileResponse = response.getResponses(0);
74+
75+
// For full list of available annotations, see http://g.co/cloud/vision/docs
76+
TextAnnotation textAnnotation = annotateFileResponse.getResponses(0).getFullTextAnnotation();
77+
for (Page page : textAnnotation.getPagesList()) {
78+
String pageText = "";
79+
for (Block block : page.getBlocksList()) {
80+
String blockText = "";
81+
for (Paragraph para : block.getParagraphsList()) {
82+
String paraText = "";
83+
for (Word word : para.getWordsList()) {
84+
String wordText = "";
85+
for (Symbol symbol : word.getSymbolsList()) {
86+
wordText = wordText + symbol.getText();
87+
System.out.format(
88+
"Symbol text: %s (Confidence: %f)\n", symbol.getText(), symbol.getConfidence());
89+
}
90+
System.out.format(
91+
"Word text: %s (Confidence: %f)\n\n", wordText, word.getConfidence());
92+
paraText = String.format("%s %s", paraText, wordText);
93+
}
94+
// Output Example using Paragraph:
95+
System.out.println("\nParagraph: \n" + paraText);
96+
System.out.format("Paragraph Confidence: %f\n", para.getConfidence());
97+
blockText = blockText + paraText;
98+
}
99+
pageText = pageText + blockText;
100+
}
101+
}
102+
System.out.println("\nComplete annotation:");
103+
System.out.println(textAnnotation.getText());
104+
105+
} catch (Exception e) {
106+
System.out.println("Error during detectPdfText: \n" + e.toString());
107+
}
108+
}
109+
}
110+
// [END vision_batch_annotate_files_beta]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
* 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_gcs_beta]
20+
import com.google.api.core.ApiFuture;
21+
import com.google.cloud.vision.v1p4beta1.AnnotateFileRequest;
22+
import com.google.cloud.vision.v1p4beta1.AnnotateFileResponse;
23+
import com.google.cloud.vision.v1p4beta1.BatchAnnotateFilesRequest;
24+
import com.google.cloud.vision.v1p4beta1.BatchAnnotateFilesResponse;
25+
import com.google.cloud.vision.v1p4beta1.Block;
26+
import com.google.cloud.vision.v1p4beta1.Feature;
27+
import com.google.cloud.vision.v1p4beta1.Feature.Type;
28+
import com.google.cloud.vision.v1p4beta1.GcsSource;
29+
import com.google.cloud.vision.v1p4beta1.ImageAnnotatorClient;
30+
import com.google.cloud.vision.v1p4beta1.InputConfig;
31+
import com.google.cloud.vision.v1p4beta1.Page;
32+
import com.google.cloud.vision.v1p4beta1.Paragraph;
33+
import com.google.cloud.vision.v1p4beta1.Symbol;
34+
import com.google.cloud.vision.v1p4beta1.TextAnnotation;
35+
import com.google.cloud.vision.v1p4beta1.Word;
36+
37+
import java.util.ArrayList;
38+
import java.util.Arrays;
39+
import java.util.List;
40+
41+
public class DetectBatchAnnotateFilesGcs {
42+
43+
// Performs document feature detection on a remote PDF/TIFF/GIF file on Google Cloud Storage.
44+
public static void detectBatchAnnotateFilesGcs(String gcsPath) {
45+
// String gcsPath = "gs://Your_BUCKET_ID/path_to_your_data";
46+
47+
try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
48+
// Annotate the first two pages and the last one (max 5 pages)
49+
// First page starts at 1, and not 0. Last page is -1.
50+
List<Integer> pages = Arrays.asList(1, 2, -1);
51+
GcsSource gcsSource = GcsSource.newBuilder().setUri(gcsPath).build();
52+
Feature feat = Feature.newBuilder().setType(Type.DOCUMENT_TEXT_DETECTION).build();
53+
//Other supported mime types : 'image/tiff' or 'image/gif'
54+
InputConfig inputConfig =
55+
InputConfig.newBuilder().setMimeType("application/pdf").setGcsSource(gcsSource).build();
56+
AnnotateFileRequest request =
57+
AnnotateFileRequest.newBuilder()
58+
.addFeatures(feat)
59+
.setInputConfig(inputConfig)
60+
.addAllPages(pages)
61+
.build();
62+
List<AnnotateFileRequest> requests = new ArrayList<>();
63+
requests.add(request);
64+
65+
BatchAnnotateFilesRequest batchAnnotateFilesRequest =
66+
BatchAnnotateFilesRequest.newBuilder().addAllRequests(requests).build();
67+
ApiFuture<BatchAnnotateFilesResponse> future =
68+
client.batchAnnotateFilesCallable().futureCall(batchAnnotateFilesRequest);
69+
BatchAnnotateFilesResponse response = future.get();
70+
71+
// Getting the first response
72+
AnnotateFileResponse annotateFileResponse = response.getResponses(0);
73+
74+
// For full list of available annotations, see http://g.co/cloud/vision/docs
75+
TextAnnotation textAnnotation = annotateFileResponse.getResponses(0).getFullTextAnnotation();
76+
for (Page page : textAnnotation.getPagesList()) {
77+
String pageText = "";
78+
for (Block block : page.getBlocksList()) {
79+
String blockText = "";
80+
for (Paragraph para : block.getParagraphsList()) {
81+
String paraText = "";
82+
for (Word word : para.getWordsList()) {
83+
String wordText = "";
84+
for (Symbol symbol : word.getSymbolsList()) {
85+
wordText = wordText + symbol.getText();
86+
System.out.format(
87+
"Symbol text: %s (Confidence: %f)\n", symbol.getText(), symbol.getConfidence());
88+
}
89+
System.out.format(
90+
"Word text: %s (Confidence: %f)\n\n", wordText, word.getConfidence());
91+
paraText = String.format("%s %s", paraText, wordText);
92+
}
93+
// Output Example using Paragraph:
94+
System.out.println("\nParagraph: \n" + paraText);
95+
System.out.format("Paragraph Confidence: %f\n", para.getConfidence());
96+
blockText = blockText + paraText;
97+
}
98+
pageText = pageText + blockText;
99+
}
100+
}
101+
System.out.println("\nComplete annotation:");
102+
System.out.println(textAnnotation.getText());
103+
104+
} catch (Exception e) {
105+
System.out.println("Error during detectPdfText: \n" + e.toString());
106+
}
107+
}
108+
}
109+
// [END vision_batch_annotate_files_gcs_beta]

0 commit comments

Comments
 (0)