Skip to content

Commit c6d960f

Browse files
authored
feat: make repo releasable, add parent/bom (#1)
* feat: make repo releasable, add parent/bom * deps: fix dependency declarations
0 parents  commit c6d960f

File tree

3 files changed

+403
-0
lines changed

3 files changed

+403
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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.api.gax.longrunning.OperationFuture;
29+
import com.google.cloud.vision.v1.AnnotateImageRequest;
30+
import com.google.cloud.vision.v1.AsyncBatchAnnotateImagesRequest;
31+
import com.google.cloud.vision.v1.AsyncBatchAnnotateImagesResponse;
32+
import com.google.cloud.vision.v1.Feature;
33+
import com.google.cloud.vision.v1.GcsDestination;
34+
import com.google.cloud.vision.v1.Image;
35+
import com.google.cloud.vision.v1.ImageAnnotatorClient;
36+
import com.google.cloud.vision.v1.ImageSource;
37+
import com.google.cloud.vision.v1.OperationMetadata;
38+
import com.google.cloud.vision.v1.OutputConfig;
39+
import java.util.Arrays;
40+
import java.util.List;
41+
import org.apache.commons.cli.CommandLine;
42+
import org.apache.commons.cli.DefaultParser;
43+
import org.apache.commons.cli.Option;
44+
import org.apache.commons.cli.Options;
45+
46+
public class VisionAsyncBatchAnnotateImages {
47+
// [START vision_async_batch_annotate_images]
48+
/*
49+
* Please include the following imports to run this sample.
50+
*
51+
* import com.google.api.gax.longrunning.OperationFuture;
52+
* import com.google.cloud.vision.v1.AnnotateImageRequest;
53+
* import com.google.cloud.vision.v1.AsyncBatchAnnotateImagesRequest;
54+
* import com.google.cloud.vision.v1.AsyncBatchAnnotateImagesResponse;
55+
* import com.google.cloud.vision.v1.Feature;
56+
* import com.google.cloud.vision.v1.GcsDestination;
57+
* import com.google.cloud.vision.v1.Image;
58+
* import com.google.cloud.vision.v1.ImageAnnotatorClient;
59+
* import com.google.cloud.vision.v1.ImageSource;
60+
* import com.google.cloud.vision.v1.OperationMetadata;
61+
* import com.google.cloud.vision.v1.OutputConfig;
62+
* import java.util.Arrays;
63+
* import java.util.List;
64+
*/
65+
66+
/** Perform async batch image annotation */
67+
public static void sampleAsyncBatchAnnotateImages(String inputImageUri, String outputUri) {
68+
try (ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create()) {
69+
// inputImageUri = "gs://cloud-samples-data/vision/label/wakeupcat.jpg";
70+
// outputUri = "gs://your-bucket/prefix/";
71+
ImageSource source = ImageSource.newBuilder().setImageUri(inputImageUri).build();
72+
Image image = Image.newBuilder().setSource(source).build();
73+
Feature.Type type = Feature.Type.LABEL_DETECTION;
74+
Feature featuresElement = Feature.newBuilder().setType(type).build();
75+
Feature.Type type2 = Feature.Type.IMAGE_PROPERTIES;
76+
Feature featuresElement2 = Feature.newBuilder().setType(type2).build();
77+
List<Feature> features = Arrays.asList(featuresElement, featuresElement2);
78+
AnnotateImageRequest requestsElement =
79+
AnnotateImageRequest.newBuilder().setImage(image).addAllFeatures(features).build();
80+
List<AnnotateImageRequest> requests = Arrays.asList(requestsElement);
81+
GcsDestination gcsDestination = GcsDestination.newBuilder().setUri(outputUri).build();
82+
83+
// The max number of responses to output in each JSON file
84+
int batchSize = 2;
85+
OutputConfig outputConfig =
86+
OutputConfig.newBuilder()
87+
.setGcsDestination(gcsDestination)
88+
.setBatchSize(batchSize)
89+
.build();
90+
AsyncBatchAnnotateImagesRequest request =
91+
AsyncBatchAnnotateImagesRequest.newBuilder()
92+
.addAllRequests(requests)
93+
.setOutputConfig(outputConfig)
94+
.build();
95+
OperationFuture<AsyncBatchAnnotateImagesResponse, OperationMetadata> future =
96+
imageAnnotatorClient.asyncBatchAnnotateImagesAsync(request);
97+
98+
System.out.println("Waiting for operation to complete...");
99+
AsyncBatchAnnotateImagesResponse response = future.get();
100+
// The output is written to GCS with the provided output_uri as prefix
101+
String gcsOutputUri = response.getOutputConfig().getGcsDestination().getUri();
102+
System.out.printf("Output written to GCS with prefix: %s\n", gcsOutputUri);
103+
} catch (Exception exception) {
104+
System.err.println("Failed to create the client due to: " + exception);
105+
}
106+
}
107+
// [END vision_async_batch_annotate_images]
108+
109+
public static void main(String[] args) throws Exception {
110+
Options options = new Options();
111+
options.addOption(
112+
Option.builder("").required(false).hasArg(true).longOpt("input_image_uri").build());
113+
options.addOption(
114+
Option.builder("").required(false).hasArg(true).longOpt("output_uri").build());
115+
116+
CommandLine cl = (new DefaultParser()).parse(options, args);
117+
String inputImageUri =
118+
cl.getOptionValue("input_image_uri", "gs://cloud-samples-data/vision/label/wakeupcat.jpg");
119+
String outputUri = cl.getOptionValue("output_uri", "gs://your-bucket/prefix/");
120+
121+
sampleAsyncBatchAnnotateImages(inputImageUri, outputUri);
122+
}
123+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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

Comments
 (0)