Skip to content

Commit bead166

Browse files
nnegreyleahecole
andauthored
automl: move video classification samples out of branch (#3042)
* 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 <[email protected]>
1 parent 883d0ee commit bead166

4 files changed

+35
-32
lines changed

automl/beta/video_classification_create_dataset.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
# limitations under the License.
1414

1515

16-
def create_dataset(project_id, display_name):
17-
"""Create a dataset."""
18-
# [START automl_video_classification_create_dataset_beta]
19-
from google.cloud import automl_v1beta1 as automl
16+
# [START automl_video_classification_create_dataset_beta]
17+
from google.cloud import automl_v1beta1 as automl
2018

21-
# TODO(developer): Uncomment and set the following variables
22-
# project_id = "YOUR_PROJECT_ID"
23-
# display_name = "your_datasets_display_name"
19+
20+
def create_dataset(
21+
project_id="YOUR_PROJECT_ID", display_name="your_datasets_display_name"
22+
):
23+
"""Create a automl video classification dataset."""
2424

2525
client = automl.AutoMlClient()
2626

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

3838
# Display the dataset information
3939
print("Dataset name: {}".format(created_dataset.name))
40+
4041
# To get the dataset id, you have to parse it out of the `name` field.
4142
# As dataset Ids are required for other methods.
4243
# Name Form:
4344
# `projects/{project_id}/locations/{location_id}/datasets/{dataset_id}`
4445
print("Dataset id: {}".format(created_dataset.name.split("/")[-1]))
45-
# [END automl_video_classification_create_dataset_beta]
46+
# [END automl_video_classification_create_dataset_beta]

automl/beta/video_classification_create_dataset_test.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import datetime
1615
import os
16+
import uuid
1717

1818
from google.cloud import automl_v1beta1 as automl
1919
import pytest
@@ -22,7 +22,7 @@
2222

2323

2424
PROJECT_ID = os.environ["AUTOML_PROJECT_ID"]
25-
pytest.DATASET_ID = None
25+
DATASET_ID = None
2626

2727

2828
@pytest.fixture(scope="function", autouse=True)
@@ -32,20 +32,21 @@ def teardown():
3232
# Delete the created dataset
3333
client = automl.AutoMlClient()
3434
dataset_full_id = client.dataset_path(
35-
PROJECT_ID, "us-central1", pytest.DATASET_ID
35+
PROJECT_ID, "us-central1", DATASET_ID
3636
)
3737
response = client.delete_dataset(dataset_full_id)
3838
response.result()
3939

4040

4141
def test_video_classification_create_dataset(capsys):
4242
# create dataset
43-
dataset_name = "test_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S")
43+
dataset_name = "test_{}".format(uuid.uuid4()).replace("-", "")[:32]
4444
video_classification_create_dataset.create_dataset(
4545
PROJECT_ID, dataset_name
4646
)
4747
out, _ = capsys.readouterr()
4848
assert "Dataset id: " in out
4949

50-
# Get the the created dataset id for deletion
51-
pytest.DATASET_ID = out.splitlines()[1].split()[2]
50+
# Get the dataset id for deletion
51+
global DATASET_ID
52+
DATASET_ID = out.splitlines()[1].split()[2]

automl/beta/video_classification_create_model.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
# [START automl_video_classification_create_model_beta]
16+
from google.cloud import automl_v1beta1 as automl
1517

16-
def create_model(project_id, dataset_id, display_name):
17-
"""Create a model."""
18-
# [START automl_video_classification_create_model_beta]
19-
from google.cloud import automl_v1beta1 as automl
20-
21-
# TODO(developer): Uncomment and set the following variables
22-
# project_id = "YOUR_PROJECT_ID"
23-
# dataset_id = "YOUR_DATASET_ID"
24-
# display_name = "your_models_display_name"
2518

19+
def create_model(
20+
project_id="YOUR_PROJECT_ID",
21+
dataset_id="YOUR_DATASET_ID",
22+
display_name="your_models_display_name",
23+
):
24+
"""Create a automl video classification model."""
2625
client = automl.AutoMlClient()
2726

2827
# A resource that represents Google Cloud Platform location.
2928
project_location = client.location_path(project_id, "us-central1")
29+
# Leave model unset to use the default base model provided by Google
3030
metadata = automl.types.VideoClassificationModelMetadata()
3131
model = automl.types.Model(
3232
display_name=display_name,
@@ -39,4 +39,4 @@ def create_model(project_id, dataset_id, display_name):
3939

4040
print("Training operation name: {}".format(response.operation.name))
4141
print("Training started...")
42-
# [END automl_video_classification_create_model_beta]
42+
# [END automl_video_classification_create_model_beta]

automl/beta/video_classification_create_model_test.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import os
16+
import uuid
1617

1718
from google.cloud import automl_v1beta1 as automl
1819
import pytest
@@ -21,26 +22,26 @@
2122

2223
PROJECT_ID = os.environ["GCLOUD_PROJECT"]
2324
DATASET_ID = "VCN510437278078730240"
24-
pytest.OPERATION_ID = None
25+
OPERATION_ID = None
2526

2627

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

31-
# Cancel the operation
32+
# Cancel the training operation
3233
client = automl.AutoMlClient()
33-
client.transport._operations_client.cancel_operation(pytest.OPERATION_ID)
34+
client.transport._operations_client.cancel_operation(OPERATION_ID)
3435

3536

3637
def test_video_classification_create_model(capsys):
38+
model_name = "test_{}".format(uuid.uuid4()).replace("-", "")[:32]
3739
video_classification_create_model.create_model(
38-
PROJECT_ID, DATASET_ID, "classification_test_create_model"
40+
PROJECT_ID, DATASET_ID, model_name
3941
)
4042
out, _ = capsys.readouterr()
4143
assert "Training started" in out
4244

43-
# Get the the operation id for cancellation
44-
pytest.OPERATION_ID = out.split("Training operation name: ")[1].split(
45-
"\n"
46-
)[0]
45+
# Cancel the operation
46+
global OPERATION_ID
47+
OPERATION_ID = out.split("Training operation name: ")[1].split("\n")[0]

0 commit comments

Comments
 (0)