|
| 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", "automl_vision_batch_predict") |
| 17 | +// sample-metadata: |
| 18 | +// title: AutoML Batch Predict (AutoML Vision) |
| 19 | +// description: AutoML Batch Predict using AutoML Vision |
| 20 | +// usage: gradle run -PmainClass=com.google.cloud.examples.automl.v1beta1.AutomlVisionBatchPredict [--args='[--input_uri "gs://[BUCKET-NAME]/path/to/file-with-image-urls.csv"] [--output_uri "gs://[BUCKET-NAME]/directory-for-output-files/"] [--project "[Google Cloud Project ID]"] [--model_id "[Model ID]"]'] |
| 21 | + |
| 22 | +package com.google.cloud.examples.automl.v1beta1; |
| 23 | + |
| 24 | +import com.google.api.gax.longrunning.OperationFuture; |
| 25 | +import com.google.cloud.automl.v1beta1.BatchPredictInputConfig; |
| 26 | +import com.google.cloud.automl.v1beta1.BatchPredictOutputConfig; |
| 27 | +import com.google.cloud.automl.v1beta1.BatchPredictRequest; |
| 28 | +import com.google.cloud.automl.v1beta1.BatchPredictResult; |
| 29 | +import com.google.cloud.automl.v1beta1.GcsDestination; |
| 30 | +import com.google.cloud.automl.v1beta1.GcsSource; |
| 31 | +import com.google.cloud.automl.v1beta1.ModelName; |
| 32 | +import com.google.cloud.automl.v1beta1.OperationMetadata; |
| 33 | +import com.google.cloud.automl.v1beta1.PredictionServiceClient; |
| 34 | +import java.util.Arrays; |
| 35 | +import java.util.HashMap; |
| 36 | +import java.util.List; |
| 37 | +import java.util.Map; |
| 38 | +import org.apache.commons.cli.CommandLine; |
| 39 | +import org.apache.commons.cli.DefaultParser; |
| 40 | +import org.apache.commons.cli.Option; |
| 41 | +import org.apache.commons.cli.Options; |
| 42 | + |
| 43 | +public class AutomlVisionBatchPredict { |
| 44 | + // [START automl_vision_batch_predict] |
| 45 | + /* |
| 46 | + * Please include the following imports to run this sample. |
| 47 | + * |
| 48 | + * import com.google.api.gax.longrunning.OperationFuture; |
| 49 | + * import com.google.cloud.automl.v1beta1.BatchPredictInputConfig; |
| 50 | + * import com.google.cloud.automl.v1beta1.BatchPredictOutputConfig; |
| 51 | + * import com.google.cloud.automl.v1beta1.BatchPredictRequest; |
| 52 | + * import com.google.cloud.automl.v1beta1.BatchPredictResult; |
| 53 | + * import com.google.cloud.automl.v1beta1.GcsDestination; |
| 54 | + * import com.google.cloud.automl.v1beta1.GcsSource; |
| 55 | + * import com.google.cloud.automl.v1beta1.ModelName; |
| 56 | + * import com.google.cloud.automl.v1beta1.OperationMetadata; |
| 57 | + * import com.google.cloud.automl.v1beta1.PredictionServiceClient; |
| 58 | + * import java.util.Arrays; |
| 59 | + * import java.util.HashMap; |
| 60 | + * import java.util.List; |
| 61 | + * import java.util.Map; |
| 62 | + */ |
| 63 | + |
| 64 | + /** |
| 65 | + * AutoML Batch Predict using AutoML Vision |
| 66 | + * |
| 67 | + * @param inputUri Google Cloud Storage URI to CSV file in your bucket that contains the paths to |
| 68 | + * the images to annotate, e.g. gs://[BUCKET-NAME]/path/to/images.csv Each line specifies a |
| 69 | + * separate path to an image in Google Cloud Storage. |
| 70 | + * @param outputUri Identifies where to store the output of your prediction request in your Google |
| 71 | + * Cloud Storage bucket. You must have write permissions to the Google Cloud Storage bucket. |
| 72 | + * @param project Required. Your Google Cloud Project ID. |
| 73 | + * @param modelId Model ID, e.g. VOT1234567890123456789 |
| 74 | + */ |
| 75 | + public static void sampleBatchPredict( |
| 76 | + String inputUri, String outputUri, String project, String modelId) { |
| 77 | + try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create()) { |
| 78 | + // inputUri = "gs://[BUCKET-NAME]/path/to/file-with-image-urls.csv"; |
| 79 | + // outputUri = "gs://[BUCKET-NAME]/directory-for-output-files/"; |
| 80 | + // project = "[Google Cloud Project ID]"; |
| 81 | + // modelId = "[Model ID]"; |
| 82 | + ModelName name = ModelName.of(project, "us-central1", modelId); |
| 83 | + List<String> inputUris = Arrays.asList(inputUri); |
| 84 | + GcsSource gcsSource = GcsSource.newBuilder().addAllInputUris(inputUris).build(); |
| 85 | + BatchPredictInputConfig inputConfig = |
| 86 | + BatchPredictInputConfig.newBuilder().setGcsSource(gcsSource).build(); |
| 87 | + GcsDestination gcsDestination = |
| 88 | + GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build(); |
| 89 | + BatchPredictOutputConfig outputConfig = |
| 90 | + BatchPredictOutputConfig.newBuilder().setGcsDestination(gcsDestination).build(); |
| 91 | + |
| 92 | + // A value from 0.0 to 1.0. When the model detects objects on video frames, |
| 93 | + // it will only produce bounding boxes that have at least this confidence score. |
| 94 | + // The default is 0.5. |
| 95 | + String paramsItem = "0.0"; |
| 96 | + Map<String, String> params = new HashMap<>(); |
| 97 | + params.put("score_threshold", paramsItem); |
| 98 | + BatchPredictRequest request = |
| 99 | + BatchPredictRequest.newBuilder() |
| 100 | + .setName(name.toString()) |
| 101 | + .setInputConfig(inputConfig) |
| 102 | + .setOutputConfig(outputConfig) |
| 103 | + .putAllParams(params) |
| 104 | + .build(); |
| 105 | + OperationFuture<BatchPredictResult, OperationMetadata> future = |
| 106 | + predictionServiceClient.batchPredictAsync(request); |
| 107 | + |
| 108 | + System.out.println("Waiting for operation to complete..."); |
| 109 | + BatchPredictResult response = future.get(); |
| 110 | + System.out.println("Batch Prediction results saved to specified Cloud Storage bucket."); |
| 111 | + } catch (Exception exception) { |
| 112 | + System.err.println("Failed to create the client due to: " + exception); |
| 113 | + } |
| 114 | + } |
| 115 | + // [END automl_vision_batch_predict] |
| 116 | + |
| 117 | + public static void main(String[] args) throws Exception { |
| 118 | + Options options = new Options(); |
| 119 | + options.addOption(Option.builder("").required(false).hasArg(true).longOpt("input_uri").build()); |
| 120 | + options.addOption( |
| 121 | + Option.builder("").required(false).hasArg(true).longOpt("output_uri").build()); |
| 122 | + options.addOption(Option.builder("").required(false).hasArg(true).longOpt("project").build()); |
| 123 | + options.addOption(Option.builder("").required(false).hasArg(true).longOpt("model_id").build()); |
| 124 | + |
| 125 | + CommandLine cl = (new DefaultParser()).parse(options, args); |
| 126 | + String inputUri = |
| 127 | + cl.getOptionValue("input_uri", "gs://[BUCKET-NAME]/path/to/file-with-image-urls.csv"); |
| 128 | + String outputUri = |
| 129 | + cl.getOptionValue("output_uri", "gs://[BUCKET-NAME]/directory-for-output-files/"); |
| 130 | + String project = cl.getOptionValue("project", "[Google Cloud Project ID]"); |
| 131 | + String modelId = cl.getOptionValue("model_id", "[Model ID]"); |
| 132 | + |
| 133 | + sampleBatchPredict(inputUri, outputUri, project, modelId); |
| 134 | + } |
| 135 | +} |
0 commit comments