Skip to content

Commit 0b461ae

Browse files
gguussJon Wayne Parrott
authored and
Jon Wayne Parrott
committed
Adds tutorials using Cloud Client [(#930)](GoogleCloudPlatform/python-docs-samples#930)
* Adds tutorials. * Removes unused enumerate
0 parents  commit 0b461ae

File tree

4 files changed

+142
-0
lines changed

4 files changed

+142
-0
lines changed

samples/labels/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Google Cloud Video Intelligence
2+
3+
Demonstrates label detection using the Google Cloud Video Intelligence API.
4+
5+
## Setup
6+
Please follow the [Set Up Your Project](https://cloud.google.com/video-intelligence/docs/getting-started#set_up_your_project)
7+
steps in the Quickstart doc to create a project and enable the Google Cloud
8+
Video Intelligence API. Following those steps, make sure that you
9+
[Set Up a Service Account](https://cloud.google.com/video-intelligence/docs/common/auth#set_up_a_service_account),
10+
and export the following environment variable:
11+
12+
```
13+
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your-project-credentials.json
14+
```
15+
16+
## Run the sample
17+
18+
Install [pip](https://pip.pypa.io/en/stable/installing) if not already installed.
19+
20+
Install the necessary libraries using pip:
21+
22+
```sh
23+
$ pip install -r requirements.txt
24+
```
25+
26+
Run the sample, for example:
27+
```
28+
python labels.py gs://cloud-ml-sandbox/video/chicago.mp4
29+
```

samples/labels/labels.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2017 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""This application demonstrates how to perform basic operations with the
18+
Google Cloud Video Intelligence API.
19+
20+
For more information, check out the documentation at
21+
https://cloud.google.com/videointelligence/docs.
22+
"""
23+
24+
# [START full_tutorial]
25+
# [START imports]
26+
import argparse
27+
import sys
28+
import time
29+
30+
from google.cloud.gapic.videointelligence.v1beta1 import enums
31+
from google.cloud.gapic.videointelligence.v1beta1 import (
32+
video_intelligence_service_client)
33+
# [END imports]
34+
35+
36+
def analyze_labels(path):
37+
""" Detects labels given a GCS path. """
38+
# [START construct_request]
39+
video_client = (video_intelligence_service_client.
40+
VideoIntelligenceServiceClient())
41+
features = [enums.Feature.LABEL_DETECTION]
42+
operation = video_client.annotate_video(path, features)
43+
# [END construct_request]
44+
print('\nProcessing video for label annotations:')
45+
46+
# [START check_operation]
47+
while not operation.done():
48+
sys.stdout.write('.')
49+
sys.stdout.flush()
50+
time.sleep(20)
51+
52+
print('\nFinished processing.')
53+
# [END check_operation]
54+
55+
# [START parse_response]
56+
results = operation.result().annotation_results[0]
57+
58+
for label in results.label_annotations:
59+
print('Label description: {}'.format(label.description))
60+
print('Locations:')
61+
62+
for l, location in enumerate(label.locations):
63+
print('\t{}: {} to {}'.format(
64+
l,
65+
location.segment.start_time_offset,
66+
location.segment.end_time_offset))
67+
# [END parse_response]
68+
69+
70+
if __name__ == '__main__':
71+
# [START running_app]
72+
parser = argparse.ArgumentParser(
73+
description=__doc__,
74+
formatter_class=argparse.RawDescriptionHelpFormatter)
75+
parser.add_argument('path', help='GCS file path for label detection.')
76+
args = parser.parse_args()
77+
78+
analyze_labels(args.path)
79+
# [END running_app]
80+
# [END full_tutorial]

samples/labels/labels_test.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2017 Google, Inc
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import os
18+
19+
import pytest
20+
21+
import labels
22+
23+
BUCKET = os.environ['CLOUD_STORAGE_BUCKET']
24+
LABELS_FILE_PATH = '/video/cat.mp4'
25+
26+
27+
@pytest.mark.slow
28+
def test_feline_video_labels(capsys):
29+
labels.analyze_labels(
30+
'gs://{}{}'.format(BUCKET, LABELS_FILE_PATH))
31+
out, _ = capsys.readouterr()
32+
assert 'Whiskers' in out

samples/labels/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://storage.googleapis.com/videointelligence-alpha/videointelligence-python.zip

0 commit comments

Comments
 (0)