diff --git a/automl/beta/video_object_tracking_create_dataset.py b/automl/beta/video_object_tracking_create_dataset.py new file mode 100644 index 00000000000..2a651d0d406 --- /dev/null +++ b/automl/beta/video_object_tracking_create_dataset.py @@ -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] diff --git a/automl/beta/video_object_tracking_create_dataset_test.py b/automl/beta/video_object_tracking_create_dataset_test.py new file mode 100644 index 00000000000..96957f71013 --- /dev/null +++ b/automl/beta/video_object_tracking_create_dataset_test.py @@ -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 + + # 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] diff --git a/automl/beta/video_object_tracking_create_model.py b/automl/beta/video_object_tracking_create_model.py new file mode 100644 index 00000000000..5ff8be9873e --- /dev/null +++ b/automl/beta/video_object_tracking_create_model.py @@ -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] diff --git a/automl/beta/video_object_tracking_create_model_test.py b/automl/beta/video_object_tracking_create_model_test.py new file mode 100644 index 00000000000..edfea3e4d13 --- /dev/null +++ b/automl/beta/video_object_tracking_create_model_test.py @@ -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]