Skip to content

automl: move video object tracking samples out of branch #2301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.example.automl;

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

// [END automl_video_object_tracking_get_model_evaluation_beta]
System.out.format(
"Classification Model Evaluation Metrics: %s\n",
modelEvaluation.getClassificationEvaluationMetrics());
// [END automl_video_classification_get_model_evaluation_beta]

// [START automl_video_object_tracking_get_model_evaluation_beta]
System.out.format(
"Video Object Tracking Evaluation Metrics: %s\n",
modelEvaluation.getVideoObjectTrackingEvaluationMetrics());
// [START automl_video_classification_get_model_evaluation_beta]
}
}
}
// [END automl_video_classification_get_model_evaluation_beta]
// [END automl_video_object_tracking_get_model_evaluation_beta]
10 changes: 10 additions & 0 deletions automl/beta/src/main/java/com/example/automl/ListDatasets.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.example.automl;

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

// [END automl_video_object_tracking_list_datasets_beta]
// [END automl_tables_list_datasets_beta]
System.out.format(
"Video classification dataset metadata: %s\n",
dataset.getVideoClassificationDatasetMetadata());
// [END automl_video_classification_list_datasets_beta]

// [START automl_video_object_tracking_list_datasets_beta]
System.out.format(
"Video object tracking dataset metadata: %s\n",
dataset.getVideoObjectTrackingDatasetMetadata());
// [END automl_video_object_tracking_list_datasets_beta]

// [START automl_tables_list_datasets_beta]
System.out.format("Tables dataset metadata: %s\n", dataset.getTablesDatasetMetadata());

// [START automl_video_classification_list_datasets_beta]
// [START automl_video_object_tracking_list_datasets_beta]
}
}
}
}
// [END automl_video_classification_list_datasets_beta]
// [END automl_video_object_tracking_list_datasets_beta]
// [END automl_tables_list_datasets_beta]
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2020 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
*
* http://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.
*/

package com.example.automl;

// [START automl_video_object_tracking_create_dataset_beta]
import com.google.cloud.automl.v1beta1.AutoMlClient;
import com.google.cloud.automl.v1beta1.Dataset;
import com.google.cloud.automl.v1beta1.LocationName;
import com.google.cloud.automl.v1beta1.VideoObjectTrackingDatasetMetadata;

import java.io.IOException;

class VideoObjectTrackingCreateDataset {

public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "YOUR_PROJECT_ID";
String displayName = "YOUR_DATASET_NAME";
createDataset(projectId, displayName);
}

// Create a dataset
static void createDataset(String projectId, String displayName) throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (AutoMlClient client = AutoMlClient.create()) {
// A resource that represents Google Cloud Platform location.
LocationName projectLocation = LocationName.of(projectId, "us-central1");
VideoObjectTrackingDatasetMetadata metadata =
VideoObjectTrackingDatasetMetadata.newBuilder().build();
Dataset dataset =
Dataset.newBuilder()
.setDisplayName(displayName)
.setVideoObjectTrackingDatasetMetadata(metadata)
.build();

Dataset createdDataset = client.createDataset(projectLocation, dataset);

// Display the dataset information.
System.out.format("Dataset name: %s\n", createdDataset.getName());
// To get the dataset id, you have to parse it out of the `name` field. As dataset Ids are
// required for other methods.
// Name Form: `projects/{project_id}/locations/{location_id}/datasets/{dataset_id}`
String[] names = createdDataset.getName().split("/");
String datasetId = names[names.length - 1];
System.out.format("Dataset id: %s\n", datasetId);
}
}
}
// [END automl_video_object_tracking_create_dataset_beta]
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2020 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
*
* http://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.
*/

package com.example.automl;

// [START automl_video_object_tracking_create_model_beta]
import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.automl.v1beta1.AutoMlClient;
import com.google.cloud.automl.v1beta1.LocationName;
import com.google.cloud.automl.v1beta1.Model;
import com.google.cloud.automl.v1beta1.OperationMetadata;
import com.google.cloud.automl.v1beta1.VideoObjectTrackingModelMetadata;

import java.io.IOException;
import java.util.concurrent.ExecutionException;

class VideoObjectTrackingCreateModel {

public static void main(String[] args)
throws IOException, ExecutionException, InterruptedException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "YOUR_PROJECT_ID";
String datasetId = "YOUR_DATASET_ID";
String displayName = "YOUR_DATASET_NAME";
createModel(projectId, datasetId, displayName);
}

// Create a model
static void createModel(String projectId, String datasetId, String displayName)
throws IOException, ExecutionException, InterruptedException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (AutoMlClient client = AutoMlClient.create()) {
// A resource that represents Google Cloud Platform location.
LocationName projectLocation = LocationName.of(projectId, "us-central1");
// Set model metadata.
VideoObjectTrackingModelMetadata metadata =
VideoObjectTrackingModelMetadata.newBuilder().build();
Model model =
Model.newBuilder()
.setDisplayName(displayName)
.setDatasetId(datasetId)
.setVideoObjectTrackingModelMetadata(metadata)
.build();

// Create a model with the model metadata in the region.
OperationFuture<Model, OperationMetadata> future =
client.createModelAsync(projectLocation, model);
// OperationFuture.get() will block until the model is created, which may take several hours.
// You can use OperationFuture.getInitialFuture to get a future representing the initial
// response to the request, which contains information while the operation is in progress.
System.out.format("Training operation name: %s\n", future.getInitialFuture().get().getName());
System.out.println("Training started...");
}
}
}
// [END automl_video_object_tracking_create_model_beta]
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright 2020 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
*
* http://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.
*/

package com.example.automl;

import static com.google.common.truth.Truth.assertThat;
import static junit.framework.TestCase.assertNotNull;

import com.google.cloud.automl.v1beta1.AutoMlClient;
import com.google.cloud.automl.v1beta1.DatasetName;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.UUID;
import java.util.concurrent.ExecutionException;

import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
@SuppressWarnings("checkstyle:abbreviationaswordinname")
public class VideoObjectTrackingCreateDatasetTest {

private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID");
private ByteArrayOutputStream bout;
private PrintStream out;
private String datasetId;

private static void requireEnvVar(String varName) {
assertNotNull(
System.getenv(varName),
"Environment variable '%s' is required to perform these tests.".format(varName));
}

@BeforeClass
public static void checkRequirements() {
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
requireEnvVar("AUTOML_PROJECT_ID");
}

@Before
public void setUp() {
bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
System.setOut(out);
}

@After
public void tearDown() throws InterruptedException, ExecutionException, IOException {
// Delete the created dataset
try (AutoMlClient client = AutoMlClient.create()) {
// Get the full path of the dataset.
DatasetName datasetFullId = DatasetName.of(PROJECT_ID, "us-central1", datasetId);
client.deleteDatasetAsync(datasetFullId).get();
}
System.setOut(null);
}

@Test
public void testCreateDataset() throws IOException, ExecutionException, InterruptedException {
// Create a random dataset name with a length of 32 characters (max allowed by AutoML)
// To prevent name collisions when running tests in multiple java versions at once.
// AutoML doesn't allow "-", but accepts "_"
String datasetName =
String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26));
VideoObjectTrackingCreateDataset.createDataset(PROJECT_ID, datasetName);

String got = bout.toString();
assertThat(got).contains("Dataset id:");
datasetId = got.split("Dataset id: ")[1].split("\n")[0];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright 2020 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
*
* http://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.
*/

package com.example.automl;

import static com.google.common.truth.Truth.assertThat;
import static junit.framework.TestCase.assertNotNull;

import com.google.cloud.automl.v1.AutoMlClient;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;

import java.util.UUID;
import java.util.concurrent.ExecutionException;

import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
@SuppressWarnings("checkstyle:abbreviationaswordinname")
public class VideoObjectTrackingCreateModelTest {

private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
private static final String DATASET_ID = "VOT1317239119331459072";
private ByteArrayOutputStream bout;
private PrintStream out;
private String operationId;

private static void requireEnvVar(String varName) {
assertNotNull(
System.getenv(varName),
"Environment variable '%s' is required to perform these tests.".format(varName));
}

@BeforeClass
public static void checkRequirements() {
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
requireEnvVar("GOOGLE_CLOUD_PROJECT");
}

@Before
public void setUp() {
bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
System.setOut(out);
}

@After
public void tearDown() throws IOException {
// Cancel the operation
try (AutoMlClient client = AutoMlClient.create()) {
client.getOperationsClient().cancelOperation(operationId);
}

System.setOut(null);
}

@Test
public void testVisionClassificationCreateModel()
throws IOException, ExecutionException, InterruptedException {
// Create a random dataset name with a length of 32 characters (max allowed by AutoML)
// To prevent name collisions when running tests in multiple java versions at once.
// AutoML doesn't allow "-", but accepts "_"
String modelName =
String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26));
VideoObjectTrackingCreateModel.createModel(PROJECT_ID, DATASET_ID, modelName);

String got = bout.toString();
assertThat(got).contains("Training started");

operationId = got.split("Training operation name: ")[1].split("\n")[0];
}
}