Skip to content

Commit 15978a4

Browse files
authored
automl: move video object tracking samples out of branch (#2301)
1 parent e6158c7 commit 15978a4

File tree

6 files changed

+338
-0
lines changed

6 files changed

+338
-0
lines changed

automl/beta/src/main/java/com/example/automl/GetModelEvaluation.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.example.automl;
1818

1919
// [START automl_video_classification_get_model_evaluation_beta]
20+
// [START automl_video_object_tracking_get_model_evaluation_beta]
2021
import com.google.cloud.automl.v1beta1.AutoMlClient;
2122
import com.google.cloud.automl.v1beta1.ModelEvaluation;
2223
import com.google.cloud.automl.v1beta1.ModelEvaluationName;
@@ -55,10 +56,19 @@ static void getModelEvaluation(String projectId, String modelId, String modelEva
5556
System.out.format(
5657
"Evalution Example Count: %d\n", modelEvaluation.getEvaluatedExampleCount());
5758

59+
// [END automl_video_object_tracking_get_model_evaluation_beta]
5860
System.out.format(
5961
"Classification Model Evaluation Metrics: %s\n",
6062
modelEvaluation.getClassificationEvaluationMetrics());
63+
// [END automl_video_classification_get_model_evaluation_beta]
64+
65+
// [START automl_video_object_tracking_get_model_evaluation_beta]
66+
System.out.format(
67+
"Video Object Tracking Evaluation Metrics: %s\n",
68+
modelEvaluation.getVideoObjectTrackingEvaluationMetrics());
69+
// [START automl_video_classification_get_model_evaluation_beta]
6170
}
6271
}
6372
}
6473
// [END automl_video_classification_get_model_evaluation_beta]
74+
// [END automl_video_object_tracking_get_model_evaluation_beta]

automl/beta/src/main/java/com/example/automl/ListDatasets.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.example.automl;
1818

1919
// [START automl_video_classification_list_datasets_beta]
20+
// [START automl_video_object_tracking_list_datasets_beta]
2021
// [START automl_tables_list_datasets_beta]
2122
import com.google.cloud.automl.v1beta1.AutoMlClient;
2223
import com.google.cloud.automl.v1beta1.Dataset;
@@ -60,19 +61,28 @@ static void listDatasets(String projectId) throws IOException {
6061
System.out.format("\tseconds: %s\n", dataset.getCreateTime().getSeconds());
6162
System.out.format("\tnanos: %s\n", dataset.getCreateTime().getNanos());
6263

64+
// [END automl_video_object_tracking_list_datasets_beta]
6365
// [END automl_tables_list_datasets_beta]
6466
System.out.format(
6567
"Video classification dataset metadata: %s\n",
6668
dataset.getVideoClassificationDatasetMetadata());
6769
// [END automl_video_classification_list_datasets_beta]
6870

71+
// [START automl_video_object_tracking_list_datasets_beta]
72+
System.out.format(
73+
"Video object tracking dataset metadata: %s\n",
74+
dataset.getVideoObjectTrackingDatasetMetadata());
75+
// [END automl_video_object_tracking_list_datasets_beta]
76+
6977
// [START automl_tables_list_datasets_beta]
7078
System.out.format("Tables dataset metadata: %s\n", dataset.getTablesDatasetMetadata());
7179

7280
// [START automl_video_classification_list_datasets_beta]
81+
// [START automl_video_object_tracking_list_datasets_beta]
7382
}
7483
}
7584
}
7685
}
7786
// [END automl_video_classification_list_datasets_beta]
87+
// [END automl_video_object_tracking_list_datasets_beta]
7888
// [END automl_tables_list_datasets_beta]
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2020 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+
* http://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+
17+
package com.example.automl;
18+
19+
// [START automl_video_object_tracking_create_dataset_beta]
20+
import com.google.cloud.automl.v1beta1.AutoMlClient;
21+
import com.google.cloud.automl.v1beta1.Dataset;
22+
import com.google.cloud.automl.v1beta1.LocationName;
23+
import com.google.cloud.automl.v1beta1.VideoObjectTrackingDatasetMetadata;
24+
25+
import java.io.IOException;
26+
27+
class VideoObjectTrackingCreateDataset {
28+
29+
public static void main(String[] args) throws IOException {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String projectId = "YOUR_PROJECT_ID";
32+
String displayName = "YOUR_DATASET_NAME";
33+
createDataset(projectId, displayName);
34+
}
35+
36+
// Create a dataset
37+
static void createDataset(String projectId, String displayName) throws IOException {
38+
// Initialize client that will be used to send requests. This client only needs to be created
39+
// once, and can be reused for multiple requests. After completing all of your requests, call
40+
// the "close" method on the client to safely clean up any remaining background resources.
41+
try (AutoMlClient client = AutoMlClient.create()) {
42+
// A resource that represents Google Cloud Platform location.
43+
LocationName projectLocation = LocationName.of(projectId, "us-central1");
44+
VideoObjectTrackingDatasetMetadata metadata =
45+
VideoObjectTrackingDatasetMetadata.newBuilder().build();
46+
Dataset dataset =
47+
Dataset.newBuilder()
48+
.setDisplayName(displayName)
49+
.setVideoObjectTrackingDatasetMetadata(metadata)
50+
.build();
51+
52+
Dataset createdDataset = client.createDataset(projectLocation, dataset);
53+
54+
// Display the dataset information.
55+
System.out.format("Dataset name: %s\n", createdDataset.getName());
56+
// To get the dataset id, you have to parse it out of the `name` field. As dataset Ids are
57+
// required for other methods.
58+
// Name Form: `projects/{project_id}/locations/{location_id}/datasets/{dataset_id}`
59+
String[] names = createdDataset.getName().split("/");
60+
String datasetId = names[names.length - 1];
61+
System.out.format("Dataset id: %s\n", datasetId);
62+
}
63+
}
64+
}
65+
// [END automl_video_object_tracking_create_dataset_beta]
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2020 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+
* http://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+
17+
package com.example.automl;
18+
19+
// [START automl_video_object_tracking_create_model_beta]
20+
import com.google.api.gax.longrunning.OperationFuture;
21+
import com.google.cloud.automl.v1beta1.AutoMlClient;
22+
import com.google.cloud.automl.v1beta1.LocationName;
23+
import com.google.cloud.automl.v1beta1.Model;
24+
import com.google.cloud.automl.v1beta1.OperationMetadata;
25+
import com.google.cloud.automl.v1beta1.VideoObjectTrackingModelMetadata;
26+
27+
import java.io.IOException;
28+
import java.util.concurrent.ExecutionException;
29+
30+
class VideoObjectTrackingCreateModel {
31+
32+
public static void main(String[] args)
33+
throws IOException, ExecutionException, InterruptedException {
34+
// TODO(developer): Replace these variables before running the sample.
35+
String projectId = "YOUR_PROJECT_ID";
36+
String datasetId = "YOUR_DATASET_ID";
37+
String displayName = "YOUR_DATASET_NAME";
38+
createModel(projectId, datasetId, displayName);
39+
}
40+
41+
// Create a model
42+
static void createModel(String projectId, String datasetId, String displayName)
43+
throws IOException, ExecutionException, InterruptedException {
44+
// Initialize client that will be used to send requests. This client only needs to be created
45+
// once, and can be reused for multiple requests. After completing all of your requests, call
46+
// the "close" method on the client to safely clean up any remaining background resources.
47+
try (AutoMlClient client = AutoMlClient.create()) {
48+
// A resource that represents Google Cloud Platform location.
49+
LocationName projectLocation = LocationName.of(projectId, "us-central1");
50+
// Set model metadata.
51+
VideoObjectTrackingModelMetadata metadata =
52+
VideoObjectTrackingModelMetadata.newBuilder().build();
53+
Model model =
54+
Model.newBuilder()
55+
.setDisplayName(displayName)
56+
.setDatasetId(datasetId)
57+
.setVideoObjectTrackingModelMetadata(metadata)
58+
.build();
59+
60+
// Create a model with the model metadata in the region.
61+
OperationFuture<Model, OperationMetadata> future =
62+
client.createModelAsync(projectLocation, model);
63+
// OperationFuture.get() will block until the model is created, which may take several hours.
64+
// You can use OperationFuture.getInitialFuture to get a future representing the initial
65+
// response to the request, which contains information while the operation is in progress.
66+
System.out.format("Training operation name: %s\n", future.getInitialFuture().get().getName());
67+
System.out.println("Training started...");
68+
}
69+
}
70+
}
71+
// [END automl_video_object_tracking_create_model_beta]
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2020 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+
* http://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+
17+
package com.example.automl;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static junit.framework.TestCase.assertNotNull;
21+
22+
import com.google.cloud.automl.v1beta1.AutoMlClient;
23+
import com.google.cloud.automl.v1beta1.DatasetName;
24+
25+
import java.io.ByteArrayOutputStream;
26+
import java.io.IOException;
27+
import java.io.PrintStream;
28+
import java.util.UUID;
29+
import java.util.concurrent.ExecutionException;
30+
31+
import org.junit.After;
32+
import org.junit.Before;
33+
import org.junit.BeforeClass;
34+
import org.junit.Test;
35+
import org.junit.runner.RunWith;
36+
import org.junit.runners.JUnit4;
37+
38+
@RunWith(JUnit4.class)
39+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
40+
public class VideoObjectTrackingCreateDatasetTest {
41+
42+
private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID");
43+
private ByteArrayOutputStream bout;
44+
private PrintStream out;
45+
private String datasetId;
46+
47+
private static void requireEnvVar(String varName) {
48+
assertNotNull(
49+
System.getenv(varName),
50+
"Environment variable '%s' is required to perform these tests.".format(varName));
51+
}
52+
53+
@BeforeClass
54+
public static void checkRequirements() {
55+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
56+
requireEnvVar("AUTOML_PROJECT_ID");
57+
}
58+
59+
@Before
60+
public void setUp() {
61+
bout = new ByteArrayOutputStream();
62+
out = new PrintStream(bout);
63+
System.setOut(out);
64+
}
65+
66+
@After
67+
public void tearDown() throws InterruptedException, ExecutionException, IOException {
68+
// Delete the created dataset
69+
try (AutoMlClient client = AutoMlClient.create()) {
70+
// Get the full path of the dataset.
71+
DatasetName datasetFullId = DatasetName.of(PROJECT_ID, "us-central1", datasetId);
72+
client.deleteDatasetAsync(datasetFullId).get();
73+
}
74+
System.setOut(null);
75+
}
76+
77+
@Test
78+
public void testCreateDataset() throws IOException, ExecutionException, InterruptedException {
79+
// Create a random dataset name with a length of 32 characters (max allowed by AutoML)
80+
// To prevent name collisions when running tests in multiple java versions at once.
81+
// AutoML doesn't allow "-", but accepts "_"
82+
String datasetName =
83+
String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26));
84+
VideoObjectTrackingCreateDataset.createDataset(PROJECT_ID, datasetName);
85+
86+
String got = bout.toString();
87+
assertThat(got).contains("Dataset id:");
88+
datasetId = got.split("Dataset id: ")[1].split("\n")[0];
89+
}
90+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright 2020 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+
* http://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+
17+
package com.example.automl;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static junit.framework.TestCase.assertNotNull;
21+
22+
import com.google.cloud.automl.v1.AutoMlClient;
23+
24+
import java.io.ByteArrayOutputStream;
25+
import java.io.IOException;
26+
import java.io.PrintStream;
27+
28+
import java.util.UUID;
29+
import java.util.concurrent.ExecutionException;
30+
31+
import org.junit.After;
32+
import org.junit.Before;
33+
import org.junit.BeforeClass;
34+
import org.junit.Test;
35+
import org.junit.runner.RunWith;
36+
import org.junit.runners.JUnit4;
37+
38+
@RunWith(JUnit4.class)
39+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
40+
public class VideoObjectTrackingCreateModelTest {
41+
42+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
43+
private static final String DATASET_ID = "VOT1317239119331459072";
44+
private ByteArrayOutputStream bout;
45+
private PrintStream out;
46+
private String operationId;
47+
48+
private static void requireEnvVar(String varName) {
49+
assertNotNull(
50+
System.getenv(varName),
51+
"Environment variable '%s' is required to perform these tests.".format(varName));
52+
}
53+
54+
@BeforeClass
55+
public static void checkRequirements() {
56+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
57+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
58+
}
59+
60+
@Before
61+
public void setUp() {
62+
bout = new ByteArrayOutputStream();
63+
out = new PrintStream(bout);
64+
System.setOut(out);
65+
}
66+
67+
@After
68+
public void tearDown() throws IOException {
69+
// Cancel the operation
70+
try (AutoMlClient client = AutoMlClient.create()) {
71+
client.getOperationsClient().cancelOperation(operationId);
72+
}
73+
74+
System.setOut(null);
75+
}
76+
77+
@Test
78+
public void testVisionClassificationCreateModel()
79+
throws IOException, ExecutionException, InterruptedException {
80+
// Create a random dataset name with a length of 32 characters (max allowed by AutoML)
81+
// To prevent name collisions when running tests in multiple java versions at once.
82+
// AutoML doesn't allow "-", but accepts "_"
83+
String modelName =
84+
String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26));
85+
VideoObjectTrackingCreateModel.createModel(PROJECT_ID, DATASET_ID, modelName);
86+
87+
String got = bout.toString();
88+
assertThat(got).contains("Training started");
89+
90+
operationId = got.split("Training operation name: ")[1].split("\n")[0];
91+
}
92+
}

0 commit comments

Comments
 (0)