-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make repo releasable, add parent/bom (#1)
* feat: make repo releasable, add parent/bom * deps: fix dependency declarations
- Loading branch information
0 parents
commit c6d960f
Showing
3 changed files
with
403 additions
and
0 deletions.
There are no files selected for viewing
123 changes: 123 additions & 0 deletions
123
vision/src/main/java/com/google/cloud/examples/vision/v1/VisionAsyncBatchAnnotateImages.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* Copyright 2019 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
// DO NOT EDIT! This is a generated sample ("LongRunningRequestAsync", | ||
// "vision_async_batch_annotate_images") | ||
// sample-metadata: | ||
// title: Async Batch Image Annotation | ||
// description: Perform async batch image annotation | ||
// usage: gradle run | ||
// -PmainClass=com.google.cloud.examples.vision.v1.VisionAsyncBatchAnnotateImages | ||
// [--args='[--input_image_uri "gs://cloud-samples-data/vision/label/wakeupcat.jpg"] [--output_uri | ||
// "gs://your-bucket/prefix/"]'] | ||
|
||
package com.google.cloud.examples.vision.v1; | ||
|
||
import com.google.api.gax.longrunning.OperationFuture; | ||
import com.google.cloud.vision.v1.AnnotateImageRequest; | ||
import com.google.cloud.vision.v1.AsyncBatchAnnotateImagesRequest; | ||
import com.google.cloud.vision.v1.AsyncBatchAnnotateImagesResponse; | ||
import com.google.cloud.vision.v1.Feature; | ||
import com.google.cloud.vision.v1.GcsDestination; | ||
import com.google.cloud.vision.v1.Image; | ||
import com.google.cloud.vision.v1.ImageAnnotatorClient; | ||
import com.google.cloud.vision.v1.ImageSource; | ||
import com.google.cloud.vision.v1.OperationMetadata; | ||
import com.google.cloud.vision.v1.OutputConfig; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import org.apache.commons.cli.CommandLine; | ||
import org.apache.commons.cli.DefaultParser; | ||
import org.apache.commons.cli.Option; | ||
import org.apache.commons.cli.Options; | ||
|
||
public class VisionAsyncBatchAnnotateImages { | ||
// [START vision_async_batch_annotate_images] | ||
/* | ||
* Please include the following imports to run this sample. | ||
* | ||
* import com.google.api.gax.longrunning.OperationFuture; | ||
* import com.google.cloud.vision.v1.AnnotateImageRequest; | ||
* import com.google.cloud.vision.v1.AsyncBatchAnnotateImagesRequest; | ||
* import com.google.cloud.vision.v1.AsyncBatchAnnotateImagesResponse; | ||
* import com.google.cloud.vision.v1.Feature; | ||
* import com.google.cloud.vision.v1.GcsDestination; | ||
* import com.google.cloud.vision.v1.Image; | ||
* import com.google.cloud.vision.v1.ImageAnnotatorClient; | ||
* import com.google.cloud.vision.v1.ImageSource; | ||
* import com.google.cloud.vision.v1.OperationMetadata; | ||
* import com.google.cloud.vision.v1.OutputConfig; | ||
* import java.util.Arrays; | ||
* import java.util.List; | ||
*/ | ||
|
||
/** Perform async batch image annotation */ | ||
public static void sampleAsyncBatchAnnotateImages(String inputImageUri, String outputUri) { | ||
try (ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create()) { | ||
// inputImageUri = "gs://cloud-samples-data/vision/label/wakeupcat.jpg"; | ||
// outputUri = "gs://your-bucket/prefix/"; | ||
ImageSource source = ImageSource.newBuilder().setImageUri(inputImageUri).build(); | ||
Image image = Image.newBuilder().setSource(source).build(); | ||
Feature.Type type = Feature.Type.LABEL_DETECTION; | ||
Feature featuresElement = Feature.newBuilder().setType(type).build(); | ||
Feature.Type type2 = Feature.Type.IMAGE_PROPERTIES; | ||
Feature featuresElement2 = Feature.newBuilder().setType(type2).build(); | ||
List<Feature> features = Arrays.asList(featuresElement, featuresElement2); | ||
AnnotateImageRequest requestsElement = | ||
AnnotateImageRequest.newBuilder().setImage(image).addAllFeatures(features).build(); | ||
List<AnnotateImageRequest> requests = Arrays.asList(requestsElement); | ||
GcsDestination gcsDestination = GcsDestination.newBuilder().setUri(outputUri).build(); | ||
|
||
// The max number of responses to output in each JSON file | ||
int batchSize = 2; | ||
OutputConfig outputConfig = | ||
OutputConfig.newBuilder() | ||
.setGcsDestination(gcsDestination) | ||
.setBatchSize(batchSize) | ||
.build(); | ||
AsyncBatchAnnotateImagesRequest request = | ||
AsyncBatchAnnotateImagesRequest.newBuilder() | ||
.addAllRequests(requests) | ||
.setOutputConfig(outputConfig) | ||
.build(); | ||
OperationFuture<AsyncBatchAnnotateImagesResponse, OperationMetadata> future = | ||
imageAnnotatorClient.asyncBatchAnnotateImagesAsync(request); | ||
|
||
System.out.println("Waiting for operation to complete..."); | ||
AsyncBatchAnnotateImagesResponse response = future.get(); | ||
// The output is written to GCS with the provided output_uri as prefix | ||
String gcsOutputUri = response.getOutputConfig().getGcsDestination().getUri(); | ||
System.out.printf("Output written to GCS with prefix: %s\n", gcsOutputUri); | ||
} catch (Exception exception) { | ||
System.err.println("Failed to create the client due to: " + exception); | ||
} | ||
} | ||
// [END vision_async_batch_annotate_images] | ||
|
||
public static void main(String[] args) throws Exception { | ||
Options options = new Options(); | ||
options.addOption( | ||
Option.builder("").required(false).hasArg(true).longOpt("input_image_uri").build()); | ||
options.addOption( | ||
Option.builder("").required(false).hasArg(true).longOpt("output_uri").build()); | ||
|
||
CommandLine cl = (new DefaultParser()).parse(options, args); | ||
String inputImageUri = | ||
cl.getOptionValue("input_image_uri", "gs://cloud-samples-data/vision/label/wakeupcat.jpg"); | ||
String outputUri = cl.getOptionValue("output_uri", "gs://your-bucket/prefix/"); | ||
|
||
sampleAsyncBatchAnnotateImages(inputImageUri, outputUri); | ||
} | ||
} |
145 changes: 145 additions & 0 deletions
145
vision/src/main/java/com/google/cloud/examples/vision/v1/VisionBatchAnnotateFiles.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/* | ||
* Copyright 2019 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
// DO NOT EDIT! This is a generated sample ("Request", "vision_batch_annotate_files") | ||
// sample-metadata: | ||
// title: | ||
// description: Perform batch file annotation | ||
// usage: gradle run -PmainClass=com.google.cloud.examples.vision.v1.VisionBatchAnnotateFiles | ||
// [--args='[--file_path "resources/kafka.pdf"]'] | ||
|
||
package com.google.cloud.examples.vision.v1; | ||
|
||
import com.google.cloud.vision.v1.AnnotateFileRequest; | ||
import com.google.cloud.vision.v1.AnnotateImageResponse; | ||
import com.google.cloud.vision.v1.BatchAnnotateFilesRequest; | ||
import com.google.cloud.vision.v1.BatchAnnotateFilesResponse; | ||
import com.google.cloud.vision.v1.Block; | ||
import com.google.cloud.vision.v1.Feature; | ||
import com.google.cloud.vision.v1.ImageAnnotatorClient; | ||
import com.google.cloud.vision.v1.InputConfig; | ||
import com.google.cloud.vision.v1.Page; | ||
import com.google.cloud.vision.v1.Paragraph; | ||
import com.google.cloud.vision.v1.Symbol; | ||
import com.google.cloud.vision.v1.Word; | ||
import com.google.protobuf.ByteString; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import org.apache.commons.cli.CommandLine; | ||
import org.apache.commons.cli.DefaultParser; | ||
import org.apache.commons.cli.Option; | ||
import org.apache.commons.cli.Options; | ||
|
||
public class VisionBatchAnnotateFiles { | ||
// [START vision_batch_annotate_files] | ||
/* | ||
* Please include the following imports to run this sample. | ||
* | ||
* import com.google.cloud.vision.v1.AnnotateFileRequest; | ||
* import com.google.cloud.vision.v1.AnnotateImageResponse; | ||
* import com.google.cloud.vision.v1.BatchAnnotateFilesRequest; | ||
* import com.google.cloud.vision.v1.BatchAnnotateFilesResponse; | ||
* import com.google.cloud.vision.v1.Block; | ||
* import com.google.cloud.vision.v1.Feature; | ||
* import com.google.cloud.vision.v1.ImageAnnotatorClient; | ||
* import com.google.cloud.vision.v1.InputConfig; | ||
* import com.google.cloud.vision.v1.Page; | ||
* import com.google.cloud.vision.v1.Paragraph; | ||
* import com.google.cloud.vision.v1.Symbol; | ||
* import com.google.cloud.vision.v1.Word; | ||
* import com.google.protobuf.ByteString; | ||
* import java.nio.file.Files; | ||
* import java.nio.file.Path; | ||
* import java.nio.file.Paths; | ||
* import java.util.Arrays; | ||
* import java.util.List; | ||
*/ | ||
|
||
/** | ||
* Perform batch file annotation | ||
* | ||
* @param filePath Path to local pdf file, e.g. /path/document.pdf | ||
*/ | ||
public static void sampleBatchAnnotateFiles(String filePath) { | ||
try (ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create()) { | ||
// filePath = "resources/kafka.pdf"; | ||
|
||
// Supported mime_type: application/pdf, image/tiff, image/gif | ||
String mimeType = "application/pdf"; | ||
Path path = Paths.get(filePath); | ||
byte[] data = Files.readAllBytes(path); | ||
ByteString content = ByteString.copyFrom(data); | ||
InputConfig inputConfig = | ||
InputConfig.newBuilder().setMimeType(mimeType).setContent(content).build(); | ||
Feature.Type type = Feature.Type.DOCUMENT_TEXT_DETECTION; | ||
Feature featuresElement = Feature.newBuilder().setType(type).build(); | ||
List<Feature> features = Arrays.asList(featuresElement); | ||
|
||
// The service can process up to 5 pages per document file. Here we specify the first, second, | ||
// and | ||
// last page of the document to be processed. | ||
int pagesElement = 1; | ||
int pagesElement2 = 2; | ||
int pagesElement3 = -1; | ||
List<Integer> pages = Arrays.asList(pagesElement, pagesElement2, pagesElement3); | ||
AnnotateFileRequest requestsElement = | ||
AnnotateFileRequest.newBuilder() | ||
.setInputConfig(inputConfig) | ||
.addAllFeatures(features) | ||
.addAllPages(pages) | ||
.build(); | ||
List<AnnotateFileRequest> requests = Arrays.asList(requestsElement); | ||
BatchAnnotateFilesRequest request = | ||
BatchAnnotateFilesRequest.newBuilder().addAllRequests(requests).build(); | ||
BatchAnnotateFilesResponse response = imageAnnotatorClient.batchAnnotateFiles(request); | ||
for (AnnotateImageResponse imageResponse : | ||
response.getResponsesList().get(0).getResponsesList()) { | ||
System.out.printf("Full text: %s\n", imageResponse.getFullTextAnnotation().getText()); | ||
for (Page page : imageResponse.getFullTextAnnotation().getPagesList()) { | ||
for (Block block : page.getBlocksList()) { | ||
System.out.printf("\nBlock confidence: %s\n", block.getConfidence()); | ||
for (Paragraph par : block.getParagraphsList()) { | ||
System.out.printf("\tParagraph confidence: %s\n", par.getConfidence()); | ||
for (Word word : par.getWordsList()) { | ||
System.out.printf("\t\tWord confidence: %s\n", word.getConfidence()); | ||
for (Symbol symbol : word.getSymbolsList()) { | ||
System.out.printf( | ||
"\t\t\tSymbol: %s, (confidence: %s)\n", | ||
symbol.getText(), symbol.getConfidence()); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} catch (Exception exception) { | ||
System.err.println("Failed to create the client due to: " + exception); | ||
} | ||
} | ||
// [END vision_batch_annotate_files] | ||
|
||
public static void main(String[] args) throws Exception { | ||
Options options = new Options(); | ||
options.addOption(Option.builder("").required(false).hasArg(true).longOpt("file_path").build()); | ||
|
||
CommandLine cl = (new DefaultParser()).parse(options, args); | ||
String filePath = cl.getOptionValue("file_path", "resources/kafka.pdf"); | ||
|
||
sampleBatchAnnotateFiles(filePath); | ||
} | ||
} |
Oops, something went wrong.