Skip to content

Commit 78fb4c6

Browse files
author
Rebecca Taylor
committed
Add Java Video OT Create Model (and fix Create Dataset)
1 parent d9175da commit 78fb4c6

File tree

2 files changed

+113
-5
lines changed

2 files changed

+113
-5
lines changed

automl/beta/src/main/java/com/google/cloud/examples/automl/v1beta1/AutomlVideoObjectTrackingCreateDataset.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ public static void sampleCreateDataset(String displayName, String project) {
7878
.build();
7979
Dataset response = autoMlClient.createDataset(request);
8080
System.out.println("Created Dataset.");
81-
Dataset dataset = response;
8281
// Print out the full name of the created dataset.
8382
//
8483
// This will have the format:
@@ -87,15 +86,15 @@ public static void sampleCreateDataset(String displayName, String project) {
8786
// The Dataset ID is the generated identifer in this path, e.g. VOT1234567890123456789
8887
// You will need this ID to perform operations on the dataset as well as to create a model.
8988
//
90-
System.out.printf("Name: %s\n", dataset.getName());
89+
System.out.printf("Name: %s\n", response.getName());
9190
// Print out the Display Name (the text you provided during creation)
92-
System.out.printf("Display Name: %s\n", dataset.getDisplayName());
91+
System.out.printf("Display Name: %s\n", response.getDisplayName());
9392
// Print out the user-provided description (may be blank)
94-
System.out.printf("Description: %s\n", dataset.getDescription());
93+
System.out.printf("Description: %s\n", response.getDescription());
9594
// The number of examples in the dataset, if any.
9695
// Added by importing data via importData
9796
//
98-
System.out.printf("Example count: %s\n", dataset.getExampleCount());
97+
System.out.printf("Example count: %s\n", response.getExampleCount());
9998
} catch (Exception exception) {
10099
System.err.println("Failed to create the client due to: " + exception);
101100
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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

Comments
 (0)