|
| 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 ("LongRunningStartThenCancel", "automl_video_object_tracking_create_model") |
| 17 | +// sample-metadata: |
| 18 | +// title: Create Model |
| 19 | +// description: Create a model |
| 20 | +// usage: gradle run -PmainClass=com.google.cloud.examples.automl.v1beta1.AutomlVideoObjectTrackingCreateModel [--args='[--display_name "My_Model_Name_123"] [--dataset_id "[Dataset ID]"] [--project "[Google Cloud Project 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.AutoMlClient; |
| 26 | +import com.google.cloud.automl.v1beta1.CreateModelRequest; |
| 27 | +import com.google.cloud.automl.v1beta1.LocationName; |
| 28 | +import com.google.cloud.automl.v1beta1.Model; |
| 29 | +import com.google.cloud.automl.v1beta1.OperationMetadata; |
| 30 | +import com.google.cloud.automl.v1beta1.VideoObjectTrackingModelMetadata; |
| 31 | +import org.apache.commons.cli.CommandLine; |
| 32 | +import org.apache.commons.cli.DefaultParser; |
| 33 | +import org.apache.commons.cli.Option; |
| 34 | +import org.apache.commons.cli.Options; |
| 35 | + |
| 36 | +public class AutomlVideoObjectTrackingCreateModel { |
| 37 | + // [START automl_video_object_tracking_create_model] |
| 38 | + /* |
| 39 | + * Please include the following imports to run this sample. |
| 40 | + * |
| 41 | + * import com.google.api.gax.longrunning.OperationFuture; |
| 42 | + * import com.google.cloud.automl.v1beta1.AutoMlClient; |
| 43 | + * import com.google.cloud.automl.v1beta1.CreateModelRequest; |
| 44 | + * import com.google.cloud.automl.v1beta1.LocationName; |
| 45 | + * import com.google.cloud.automl.v1beta1.Model; |
| 46 | + * import com.google.cloud.automl.v1beta1.OperationMetadata; |
| 47 | + * import com.google.cloud.automl.v1beta1.VideoObjectTrackingModelMetadata; |
| 48 | + */ |
| 49 | + |
| 50 | + /** |
| 51 | + * Create a model |
| 52 | + * |
| 53 | + * @param displayName The name of the model to show in the interface. The name can be up to 32 |
| 54 | + * characters long and can consist only of ASCII Latin letters A-Z and a-z, underscores (_), |
| 55 | + * and ASCII digits 0-9. Must be unique within the scope of the provided GCP Project and |
| 56 | + * Location. |
| 57 | + * @param datasetId Required. The resource ID of the dataset used to create the model. The dataset |
| 58 | + * must come from the same ancestor project and location. |
| 59 | + * @param project Required. Your Google Cloud Project ID. |
| 60 | + */ |
| 61 | + public static void sampleCreateModel(String displayName, String datasetId, String project) { |
| 62 | + try (AutoMlClient autoMlClient = AutoMlClient.create()) { |
| 63 | + // displayName = "My_Model_Name_123"; |
| 64 | + // datasetId = "[Dataset ID]"; |
| 65 | + // project = "[Google Cloud Project ID]"; |
| 66 | + LocationName parent = LocationName.of(project, "us-central1"); |
| 67 | + |
| 68 | + // Initialized video_object_tracking_model_metadata field must be provided. |
| 69 | + // This specifies this Dataset is to be used for video object tracking. |
| 70 | + VideoObjectTrackingModelMetadata videoObjectTrackingModelMetadata = |
| 71 | + VideoObjectTrackingModelMetadata.newBuilder().build(); |
| 72 | + Model model = |
| 73 | + Model.newBuilder() |
| 74 | + .setDisplayName(displayName) |
| 75 | + .setDatasetId(datasetId) |
| 76 | + .setVideoObjectTrackingModelMetadata(videoObjectTrackingModelMetadata) |
| 77 | + .build(); |
| 78 | + CreateModelRequest request = |
| 79 | + CreateModelRequest.newBuilder().setParent(parent.toString()).setModel(model).build(); |
| 80 | + OperationFuture<Model, OperationMetadata> operation = |
| 81 | + autoMlClient.createModelOperationCallable().futureCall(request); |
| 82 | + |
| 83 | + // The long-running operation has started. |
| 84 | + |
| 85 | + // Store the operation name to poll for operation status: |
| 86 | + String operationName = operation.getName(); |
| 87 | + System.out.printf("Started long-running operation: %s", operationName); |
| 88 | + } catch (Exception exception) { |
| 89 | + System.err.println("Failed to create the client due to: " + exception); |
| 90 | + } |
| 91 | + } |
| 92 | + // [END automl_video_object_tracking_create_model] |
| 93 | + |
| 94 | + public static void main(String[] args) throws Exception { |
| 95 | + Options options = new Options(); |
| 96 | + options.addOption( |
| 97 | + Option.builder("").required(false).hasArg(true).longOpt("display_name").build()); |
| 98 | + options.addOption( |
| 99 | + Option.builder("").required(false).hasArg(true).longOpt("dataset_id").build()); |
| 100 | + options.addOption(Option.builder("").required(false).hasArg(true).longOpt("project").build()); |
| 101 | + |
| 102 | + CommandLine cl = (new DefaultParser()).parse(options, args); |
| 103 | + String displayName = cl.getOptionValue("display_name", "My_Model_Name_123"); |
| 104 | + String datasetId = cl.getOptionValue("dataset_id", "[Dataset ID]"); |
| 105 | + String project = cl.getOptionValue("project", "[Google Cloud Project ID]"); |
| 106 | + |
| 107 | + sampleCreateModel(displayName, datasetId, project); |
| 108 | + } |
| 109 | +} |
0 commit comments