Skip to content

Commit 20bcf57

Browse files
committed
Add generated sample
1 parent d67bdd8 commit 20bcf57

File tree

3 files changed

+402
-0
lines changed

3 files changed

+402
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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 ("LongRunningRequestAsync",
17+
// "vision_async_batch_annotate_images")
18+
// sample-metadata:
19+
// title: Async Batch Image Annotation
20+
// description: Perform async batch image annotation
21+
// usage: gradle run
22+
// -PmainClass=com.google.cloud.examples.vision.v1.VisionAsyncBatchAnnotateImages
23+
// [--args='[--input_image_uri "gs://cloud-samples-data/vision/label/wakeupcat.jpg"] [--output_uri
24+
// "gs://your-bucket/prefix/"]']
25+
26+
package com.google.cloud.examples.vision.v1;
27+
28+
import com.google.cloud.vision.v1.AnnotateImageRequest;
29+
import com.google.cloud.vision.v1.AsyncBatchAnnotateImagesRequest;
30+
import com.google.cloud.vision.v1.AsyncBatchAnnotateImagesResponse;
31+
import com.google.cloud.vision.v1.Feature;
32+
import com.google.cloud.vision.v1.GcsDestination;
33+
import com.google.cloud.vision.v1.Image;
34+
import com.google.cloud.vision.v1.ImageAnnotatorClient;
35+
import com.google.cloud.vision.v1.ImageSource;
36+
import com.google.cloud.vision.v1.OutputConfig;
37+
import java.util.Arrays;
38+
import java.util.List;
39+
import org.apache.commons.cli.CommandLine;
40+
import org.apache.commons.cli.DefaultParser;
41+
import org.apache.commons.cli.Option;
42+
import org.apache.commons.cli.Options;
43+
44+
public class VisionAsyncBatchAnnotateImages {
45+
// [START vision_async_batch_annotate_images]
46+
/*
47+
* Please include the following imports to run this sample.
48+
*
49+
* import com.google.cloud.vision.v1.AnnotateImageRequest;
50+
* import com.google.cloud.vision.v1.AsyncBatchAnnotateImagesRequest;
51+
* import com.google.cloud.vision.v1.AsyncBatchAnnotateImagesResponse;
52+
* import com.google.cloud.vision.v1.Feature;
53+
* import com.google.cloud.vision.v1.GcsDestination;
54+
* import com.google.cloud.vision.v1.Image;
55+
* import com.google.cloud.vision.v1.ImageAnnotatorClient;
56+
* import com.google.cloud.vision.v1.ImageSource;
57+
* import com.google.cloud.vision.v1.OutputConfig;
58+
* import java.util.Arrays;
59+
* import java.util.List;
60+
*/
61+
62+
/** Perform async batch image annotation */
63+
public static void sampleAsyncBatchAnnotateImages(String inputImageUri, String outputUri) {
64+
// [START vision_async_batch_annotate_images_core]
65+
try (ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create()) {
66+
// inputImageUri = "gs://cloud-samples-data/vision/label/wakeupcat.jpg";
67+
// outputUri = "gs://your-bucket/prefix/";
68+
ImageSource source = ImageSource.newBuilder().setImageUri(inputImageUri).build();
69+
Image image = Image.newBuilder().setSource(source).build();
70+
Feature.Type type = Feature.Type.LABEL_DETECTION;
71+
Feature featuresElement = Feature.newBuilder().setType(type).build();
72+
Feature.Type type2 = Feature.Type.IMAGE_PROPERTIES;
73+
Feature featuresElement2 = Feature.newBuilder().setType(type2).build();
74+
List<Feature> features = Arrays.asList(featuresElement, featuresElement2);
75+
AnnotateImageRequest requestsElement =
76+
AnnotateImageRequest.newBuilder().setImage(image).addAllFeatures(features).build();
77+
List<AnnotateImageRequest> requests = Arrays.asList(requestsElement);
78+
GcsDestination gcsDestination = GcsDestination.newBuilder().setUri(outputUri).build();
79+
80+
// The max number of responses to output in each JSON file
81+
int batchSize = 2;
82+
OutputConfig outputConfig =
83+
OutputConfig.newBuilder()
84+
.setGcsDestination(gcsDestination)
85+
.setBatchSize(batchSize)
86+
.build();
87+
AsyncBatchAnnotateImagesRequest request =
88+
AsyncBatchAnnotateImagesRequest.newBuilder()
89+
.addAllRequests(requests)
90+
.setOutputConfig(outputConfig)
91+
.build();
92+
AsyncBatchAnnotateImagesResponse response =
93+
imageAnnotatorClient.asyncBatchAnnotateImagesAsync(request).get();
94+
// The output is written to GCS with the provided output_uri as prefix
95+
String gcsOutputUri = response.getOutputConfig().getGcsDestination().getUri();
96+
System.out.printf("Output written to GCS with prefix: %s\n", gcsOutputUri);
97+
} catch (Exception exception) {
98+
System.err.println("Failed to create the client due to: " + exception);
99+
}
100+
// [END vision_async_batch_annotate_images_core]
101+
}
102+
// [END vision_async_batch_annotate_images]
103+
104+
public static void main(String[] args) throws Exception {
105+
Options options = new Options();
106+
options.addOption(
107+
Option.builder("").required(false).hasArg(true).longOpt("input_image_uri").build());
108+
options.addOption(
109+
Option.builder("").required(false).hasArg(true).longOpt("output_uri").build());
110+
111+
CommandLine cl = (new DefaultParser()).parse(options, args);
112+
String inputImageUri =
113+
cl.getOptionValue("input_image_uri", "gs://cloud-samples-data/vision/label/wakeupcat.jpg");
114+
String outputUri = cl.getOptionValue("output_uri", "gs://your-bucket/prefix/");
115+
116+
sampleAsyncBatchAnnotateImages(inputImageUri, outputUri);
117+
}
118+
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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+
// [START vision_batch_annotate_files_core]
80+
try (ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create()) {
81+
// filePath = "resources/kafka.pdf";
82+
83+
// Supported mime_type: application/pdf, image/tiff, image/gif
84+
String mimeType = "application/pdf";
85+
Path path = Paths.get(filePath);
86+
byte[] data = Files.readAllBytes(path);
87+
ByteString content = ByteString.copyFrom(data);
88+
InputConfig inputConfig =
89+
InputConfig.newBuilder().setMimeType(mimeType).setContent(content).build();
90+
Feature.Type type = Feature.Type.DOCUMENT_TEXT_DETECTION;
91+
Feature featuresElement = Feature.newBuilder().setType(type).build();
92+
List<Feature> features = Arrays.asList(featuresElement);
93+
94+
// The service can process up to 5 pages per document file. Here we specify the first, second,
95+
// and
96+
// last page of the document to be processed.
97+
int pagesElement = 1;
98+
int pagesElement2 = 2;
99+
int pagesElement3 = -1;
100+
List<Integer> pages = Arrays.asList(pagesElement, pagesElement2, pagesElement3);
101+
AnnotateFileRequest requestsElement =
102+
AnnotateFileRequest.newBuilder()
103+
.setInputConfig(inputConfig)
104+
.addAllFeatures(features)
105+
.addAllPages(pages)
106+
.build();
107+
List<AnnotateFileRequest> requests = Arrays.asList(requestsElement);
108+
BatchAnnotateFilesRequest request =
109+
BatchAnnotateFilesRequest.newBuilder().addAllRequests(requests).build();
110+
BatchAnnotateFilesResponse response = imageAnnotatorClient.batchAnnotateFiles(request);
111+
for (AnnotateImageResponse imageResponse :
112+
response.getResponsesList().get(0).getResponsesList()) {
113+
System.out.printf("Full text: %s\n", imageResponse.getFullTextAnnotation().getText());
114+
for (Page page : imageResponse.getFullTextAnnotation().getPagesList()) {
115+
for (Block block : page.getBlocksList()) {
116+
System.out.printf("\nBlock confidence: %s\n", block.getConfidence());
117+
for (Paragraph par : block.getParagraphsList()) {
118+
System.out.printf("\tParagraph confidence: %s\n", par.getConfidence());
119+
for (Word word : par.getWordsList()) {
120+
System.out.printf("\t\tWord confidence: %s\n", word.getConfidence());
121+
for (Symbol symbol : word.getSymbolsList()) {
122+
System.out.printf(
123+
"\t\t\tSymbol: %s, (confidence: %s)\n",
124+
symbol.getText(), symbol.getConfidence());
125+
}
126+
}
127+
}
128+
}
129+
}
130+
}
131+
} catch (Exception exception) {
132+
System.err.println("Failed to create the client due to: " + exception);
133+
}
134+
// [END vision_batch_annotate_files_core]
135+
}
136+
// [END vision_batch_annotate_files]
137+
138+
public static void main(String[] args) throws Exception {
139+
Options options = new Options();
140+
options.addOption(Option.builder("").required(false).hasArg(true).longOpt("file_path").build());
141+
142+
CommandLine cl = (new DefaultParser()).parse(options, args);
143+
String filePath = cl.getOptionValue("file_path", "resources/kafka.pdf");
144+
145+
sampleBatchAnnotateFiles(filePath);
146+
}
147+
}

0 commit comments

Comments
 (0)