Skip to content

Commit

Permalink
automl: move video classification samples out of branch (#3042)
Browse files Browse the repository at this point in the history
* automl: move video classification samples out of branch

* fix uuid and create test

* fix project

* use global for testing

* Update video_classification_create_model.py

Co-authored-by: Leah E. Cole <6719667+leahecole@users.noreply.github.com>
  • Loading branch information
nnegrey and leahecole authored Apr 2, 2020
1 parent 883d0ee commit bead166
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 32 deletions.
17 changes: 9 additions & 8 deletions automl/beta/video_classification_create_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
# limitations under the License.


def create_dataset(project_id, display_name):
"""Create a dataset."""
# [START automl_video_classification_create_dataset_beta]
from google.cloud import automl_v1beta1 as automl
# [START automl_video_classification_create_dataset_beta]
from google.cloud import automl_v1beta1 as automl

# TODO(developer): Uncomment and set the following variables
# project_id = "YOUR_PROJECT_ID"
# display_name = "your_datasets_display_name"

def create_dataset(
project_id="YOUR_PROJECT_ID", display_name="your_datasets_display_name"
):
"""Create a automl video classification dataset."""

client = automl.AutoMlClient()

Expand All @@ -37,9 +37,10 @@ def create_dataset(project_id, display_name):

# Display the dataset information
print("Dataset name: {}".format(created_dataset.name))

# 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}`
print("Dataset id: {}".format(created_dataset.name.split("/")[-1]))
# [END automl_video_classification_create_dataset_beta]
# [END automl_video_classification_create_dataset_beta]
13 changes: 7 additions & 6 deletions automl/beta/video_classification_create_dataset_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import datetime
import os
import uuid

from google.cloud import automl_v1beta1 as automl
import pytest
Expand All @@ -22,7 +22,7 @@


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


@pytest.fixture(scope="function", autouse=True)
Expand All @@ -32,20 +32,21 @@ def teardown():
# Delete the created dataset
client = automl.AutoMlClient()
dataset_full_id = client.dataset_path(
PROJECT_ID, "us-central1", pytest.DATASET_ID
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_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S")
dataset_name = "test_{}".format(uuid.uuid4()).replace("-", "")[:32]
video_classification_create_dataset.create_dataset(
PROJECT_ID, dataset_name
)
out, _ = capsys.readouterr()
assert "Dataset id: " in out

# Get the the created dataset id for deletion
pytest.DATASET_ID = out.splitlines()[1].split()[2]
# Get the dataset id for deletion
global DATASET_ID
DATASET_ID = out.splitlines()[1].split()[2]
20 changes: 10 additions & 10 deletions automl/beta/video_classification_create_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@
# See the License for the specific language governing permissions and
# limitations under the License.

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

def create_model(project_id, dataset_id, display_name):
"""Create a model."""
# [START automl_video_classification_create_model_beta]
from google.cloud import automl_v1beta1 as automl

# TODO(developer): Uncomment and set the following variables
# project_id = "YOUR_PROJECT_ID"
# dataset_id = "YOUR_DATASET_ID"
# display_name = "your_models_display_name"

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 location.
project_location = client.location_path(project_id, "us-central1")
# Leave model unset to use the default base model provided by Google
metadata = automl.types.VideoClassificationModelMetadata()
model = automl.types.Model(
display_name=display_name,
Expand All @@ -39,4 +39,4 @@ def create_model(project_id, dataset_id, display_name):

print("Training operation name: {}".format(response.operation.name))
print("Training started...")
# [END automl_video_classification_create_model_beta]
# [END automl_video_classification_create_model_beta]
17 changes: 9 additions & 8 deletions automl/beta/video_classification_create_model_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import os
import uuid

from google.cloud import automl_v1beta1 as automl
import pytest
Expand All @@ -21,26 +22,26 @@

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


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

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


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

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

0 comments on commit bead166

Please sign in to comment.