forked from googleapis/python-automl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
automl: move video object tracking samples out of branch [(#3043)](Go…
…ogleCloudPlatform/python-docs-samples#3043) * automl: move video object tracking samples out of branch * fix uuid and create test * fix project * use global var for testing * Update video_object_tracking_create_model_test.py
- Loading branch information
Showing
4 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
# 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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |