Skip to content
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

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

Merged
merged 9 commits into from
Mar 10, 2020
38 changes: 38 additions & 0 deletions automl/beta/video_object_tracking_create_dataset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 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.

# [START automl_video_object_tracking_create_dataset_beta]
from google.cloud import automl_v1beta1 as automl


def create_dataset(
project_id="YOUR_PROJECT_ID", display_name="your_datasets_display_name"
):
"""Create a automl video object tracking dataset."""
client = automl.AutoMlClient()

# A resource that represents Google Cloud Platform location.
project_location = client.location_path(project_id, "us-central1")
metadata = automl.types.VideoObjectTrackingDatasetMetadata()
dataset = automl.types.Dataset(
display_name=display_name,
video_object_tracking_dataset_metadata=metadata,
)

# Create a dataset with the dataset metadata in the region.
created_dataset = client.create_dataset(project_location, dataset)
# Display the dataset information
print("Dataset name: {}".format(created_dataset.name))
print("Dataset id: {}".format(created_dataset.name.split("/")[-1]))
# [END automl_video_object_tracking_create_dataset_beta]
51 changes: 51 additions & 0 deletions automl/beta/video_object_tracking_create_dataset_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# 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.

import os
import uuid

from google.cloud import automl_v1beta1 as automl
import pytest

import video_object_tracking_create_dataset

PROJECT_ID = os.environ["AUTOML_PROJECT_ID"]
DATASET_ID = None


@pytest.fixture(scope="function", autouse=True)
def teardown():
yield
nnegrey marked this conversation as resolved.
Show resolved Hide resolved

# Delete the created dataset
client = automl.AutoMlClient()
dataset_full_id = client.dataset_path(
PROJECT_ID, "us-central1", DATASET_ID
)
response = client.delete_dataset(dataset_full_id)
response.result()


def test_video_classification_create_dataset(capsys):
# create dataset
dataset_name = "test_{}".format(uuid.uuid4()).replace("-", "")[:32]
video_object_tracking_create_dataset.create_dataset(
PROJECT_ID, dataset_name
)
out, _ = capsys.readouterr()
assert "Dataset id: " in out

# Get the dataset id for deletion
global DATASET_ID
DATASET_ID = out.splitlines()[1].split()[2]
42 changes: 42 additions & 0 deletions automl/beta/video_object_tracking_create_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# 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.

# [START automl_video_object_tracking_create_model_beta]
from google.cloud import automl_v1beta1 as automl


def create_model(
project_id="YOUR_PROJECT_ID",
dataset_id="YOUR_DATASET_ID",
display_name="your_models_display_name",
):
"""Create a automl video classification model."""
client = automl.AutoMlClient()

# A resource that represents Google Cloud Platform loacation.
project_location = client.location_path(project_id, "us-central1")
# Leave model unset to use the default base model provided by Google
metadata = automl.types.VideoObjectTrackingModelMetadata()
model = automl.types.Model(
display_name=display_name,
dataset_id=dataset_id,
video_object_tracking_model_metadata=metadata,
)

# Create a model with the model metadata in the region.
response = client.create_model(project_location, model)

print("Training operation name: {}".format(response.operation.name))
print("Training started...")
# [END automl_video_object_tracking_create_model_beta]
47 changes: 47 additions & 0 deletions automl/beta/video_object_tracking_create_model_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# 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.

import os
import uuid

from google.cloud import automl_v1beta1 as automl
import pytest

import video_object_tracking_create_model

PROJECT_ID = os.environ["GCLOUD_PROJECT"]
DATASET_ID = "VOT2823376535338090496"
OPERATION_ID = None


@pytest.fixture(scope="function", autouse=True)
def teardown():
yield

# Cancel the training operation
client = automl.AutoMlClient()
client.transport._operations_client.cancel_operation(OPERATION_ID)


def test_video_classification_create_model(capsys):
model_name = "test_{}".format(uuid.uuid4()).replace("-", "")[:32]
video_object_tracking_create_model.create_model(
PROJECT_ID, DATASET_ID, model_name
)
out, _ = capsys.readouterr()
assert "Training started" in out

# Cancel the operation
global OPERATION_ID
OPERATION_ID = out.split("Training operation name: ")[1].split("\n")[0]