From d772359ac04b5d06de35b1b3ce10c9b91b4418f4 Mon Sep 17 00:00:00 2001 From: Gus Class Date: Thu, 25 May 2017 13:16:58 -0700 Subject: [PATCH 1/3] Adds quickstart --- video/cloud-client/quickstart/README.rst | 173 ++++++++++++++++++ video/cloud-client/quickstart/README.rst.in | 20 ++ video/cloud-client/quickstart/quickstart.py | 67 +++++++ .../quickstart/quickstart_test.py | 26 +++ .../cloud-client/quickstart/requirements.txt | 1 + 5 files changed, 287 insertions(+) create mode 100644 video/cloud-client/quickstart/README.rst create mode 100644 video/cloud-client/quickstart/README.rst.in create mode 100644 video/cloud-client/quickstart/quickstart.py create mode 100644 video/cloud-client/quickstart/quickstart_test.py create mode 100644 video/cloud-client/quickstart/requirements.txt diff --git a/video/cloud-client/quickstart/README.rst b/video/cloud-client/quickstart/README.rst new file mode 100644 index 00000000000..ca74c0e5ef2 --- /dev/null +++ b/video/cloud-client/quickstart/README.rst @@ -0,0 +1,173 @@ +.. This file is automatically generated. Do not edit this file directly. + +Google Cloud Video Intelligence API Python Samples +=============================================================================== + +This directory contains samples for Google Cloud Video Intelligence API. `Google Cloud Video Intelligence API`_ allows developers to easily integrate feature detection in video. + + + + +.. _Google Cloud Video Intelligence API: https://cloud.google.com/video-intelligence/docs + +Setup +------------------------------------------------------------------------------- + + +Authentication +++++++++++++++ + +Authentication is typically done through `Application Default Credentials`_, +which means you do not have to change the code to authenticate as long as +your environment has credentials. You have a few options for setting up +authentication: + +#. When running locally, use the `Google Cloud SDK`_ + + .. code-block:: bash + + gcloud auth application-default login + + +#. When running on App Engine or Compute Engine, credentials are already + set-up. However, you may need to configure your Compute Engine instance + with `additional scopes`_. + +#. You can create a `Service Account key file`_. This file can be used to + authenticate to Google Cloud Platform services from any environment. To use + the file, set the ``GOOGLE_APPLICATION_CREDENTIALS`` environment variable to + the path to the key file, for example: + + .. code-block:: bash + + export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json + +.. _Application Default Credentials: https://cloud.google.com/docs/authentication#getting_credentials_for_server-centric_flow +.. _additional scopes: https://cloud.google.com/compute/docs/authentication#using +.. _Service Account key file: https://developers.google.com/identity/protocols/OAuth2ServiceAccount#creatinganaccount + +Install Dependencies +++++++++++++++++++++ + +#. Install `pip`_ and `virtualenv`_ if you do not already have them. + +#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+. + + .. code-block:: bash + + $ virtualenv env + $ source env/bin/activate + +#. Install the dependencies needed to run the samples. + + .. code-block:: bash + + $ pip install -r requirements.txt + +.. _pip: https://pip.pypa.io/ +.. _virtualenv: https://virtualenv.pypa.io/ + +Samples +------------------------------------------------------------------------------- + +quickstart ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + + +To run this sample: + +.. code-block:: bash + + $ python quickstart.py + + + Processing video for label annotations: + . + Finished processing. + Label description: Animal + Locations: + 0: Entire video + 1: 0.0 to 14.840051 + + + Label description: Cat + Locations: + 0: Entire video + 1: 0.0 to 14.840051 + + + Label description: Domestic long-haired cat + Locations: + 0: Entire video + 1: 0.0 to 14.840051 + + + Label description: Felidae + Locations: + 0: Entire video + 1: 0.0 to 14.840051 + + + Label description: Felinae + Locations: + 0: Entire video + 1: 0.0 to 14.840051 + + + Label description: Kitten + Locations: + 0: Entire video + 1: 0.0 to 14.840051 + + + Label description: Maine Coon + Locations: + 0: Entire video + 1: 0.0 to 14.840051 + + + Label description: Nature + Locations: + 0: Entire video + 1: 0.0 to 14.840051 + + + Label description: Pet + Locations: + 0: Entire video + 1: 0.0 to 14.840051 + + + Label description: Tabby cat + Locations: + 0: Entire video + 1: 0.0 to 14.840051 + + + Label description: Whiskers + Locations: + 0: Entire video + 1: 0.0 to 14.840051 + + + + + + +The client library +------------------------------------------------------------------------------- + +This sample uses the `Google Cloud Client Library for Python`_. +You can read the documentation for more details on API usage and use GitHub +to `browse the source`_ and `report issues`_. + +.. _Google Cloud Client Library for Python: + https://googlecloudplatform.github.io/google-cloud-python/ +.. _browse the source: + https://github.com/GoogleCloudPlatform/google-cloud-python +.. _report issues: + https://github.com/GoogleCloudPlatform/google-cloud-python/issues + + +.. _Google Cloud SDK: https://cloud.google.com/sdk/ \ No newline at end of file diff --git a/video/cloud-client/quickstart/README.rst.in b/video/cloud-client/quickstart/README.rst.in new file mode 100644 index 00000000000..617e3fb4a5a --- /dev/null +++ b/video/cloud-client/quickstart/README.rst.in @@ -0,0 +1,20 @@ +# This file is used to generate README.rst + +product: + name: Google Cloud Video Intelligence API + short_name: Cloud Video Intelligence API + url: https://cloud.google.com/video-intelligence/docs + description: > + `Google Cloud Video Intelligence API`_ allows developers to easily + integrate feature detection in video. + +setup: +- auth +- install_deps + +samples: +- name: quickstart + file: quickstart.py + show_help: True + +cloud_client_library: true diff --git a/video/cloud-client/quickstart/quickstart.py b/video/cloud-client/quickstart/quickstart.py new file mode 100644 index 00000000000..08ff887e384 --- /dev/null +++ b/video/cloud-client/quickstart/quickstart.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python + +# Copyright 2017 Google Inc. All Rights Reserved. +# +# 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. + +"""This application demonstrates label detection using the Google Cloud API. + +Usage: + python quickstart.py +""" + +# [START videointelligence_quickstart] +import sys +import time + +from google.cloud.gapic.videointelligence.v1beta1 import enums +from google.cloud.gapic.videointelligence.v1beta1 import ( + video_intelligence_service_client) + + +def run_quickstart(): + video_client = (video_intelligence_service_client. + VideoIntelligenceServiceClient()) + features = [enums.Feature.LABEL_DETECTION] + operation = video_client.annotate_video('gs://demomaker/cat.mp4', features) + print('\nProcessing video for label annotations:') + + while not operation.done(): + sys.stdout.write('.') + sys.stdout.flush() + time.sleep(15) + + print('\nFinished processing.') + + # first result is retrieved because a single video was processed + results = operation.result().annotation_results[0] + + for i, label in enumerate(results.label_annotations): + print('Label description: {}'.format(label.description)) + print('Locations:') + + for l, location in enumerate(label.locations): + positions = 'Entire video' + if (location.segment.start_time_offset != -1 or + location.segment.end_time_offset != -1): + positions = '{} to {}'.format( + location.segment.start_time_offset / 1000000.0, + location.segment.end_time_offset / 1000000.0) + print('\t{}: {}'.format(l, positions)) + + print('\n') + + +if __name__ == '__main__': + run_quickstart() +# [END videointelligence_quickstart] diff --git a/video/cloud-client/quickstart/quickstart_test.py b/video/cloud-client/quickstart/quickstart_test.py new file mode 100644 index 00000000000..9712f410762 --- /dev/null +++ b/video/cloud-client/quickstart/quickstart_test.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python + +# Copyright 2017 Google, Inc +# +# 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 pytest + +import quickstart + + +@pytest.mark.slow +def test_quickstart(capsys): + quickstart.run_quickstart() + out, _ = capsys.readouterr() + assert 'Whiskers' in out diff --git a/video/cloud-client/quickstart/requirements.txt b/video/cloud-client/quickstart/requirements.txt new file mode 100644 index 00000000000..b2aa05b6722 --- /dev/null +++ b/video/cloud-client/quickstart/requirements.txt @@ -0,0 +1 @@ +google-cloud-videointelligence==0.25.0 From 06bfe26c487942dac7c04480f0761ea10e31c48a Mon Sep 17 00:00:00 2001 From: Gus Class Date: Thu, 25 May 2017 14:35:46 -0700 Subject: [PATCH 2/3] Address review comments --- video/cloud-client/quickstart/quickstart.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/video/cloud-client/quickstart/quickstart.py b/video/cloud-client/quickstart/quickstart.py index 08ff887e384..bfb5bca9116 100644 --- a/video/cloud-client/quickstart/quickstart.py +++ b/video/cloud-client/quickstart/quickstart.py @@ -20,16 +20,16 @@ python quickstart.py """ -# [START videointelligence_quickstart] -import sys -import time -from google.cloud.gapic.videointelligence.v1beta1 import enums -from google.cloud.gapic.videointelligence.v1beta1 import ( - video_intelligence_service_client) +def run_quickstart(): + # [START videointelligence_quickstart] + import sys + import time + from google.cloud.gapic.videointelligence.v1beta1 import enums + from google.cloud.gapic.videointelligence.v1beta1 import ( + video_intelligence_service_client) -def run_quickstart(): video_client = (video_intelligence_service_client. VideoIntelligenceServiceClient()) features = [enums.Feature.LABEL_DETECTION] @@ -46,7 +46,7 @@ def run_quickstart(): # first result is retrieved because a single video was processed results = operation.result().annotation_results[0] - for i, label in enumerate(results.label_annotations): + for label in results.label_annotations: print('Label description: {}'.format(label.description)) print('Locations:') @@ -60,8 +60,8 @@ def run_quickstart(): print('\t{}: {}'.format(l, positions)) print('\n') + # [END videointelligence_quickstart] if __name__ == '__main__': run_quickstart() -# [END videointelligence_quickstart] From 6418d65dfae8c24934c8bf902008a01c3a1a5166 Mon Sep 17 00:00:00 2001 From: Gus Class Date: Thu, 25 May 2017 14:37:15 -0700 Subject: [PATCH 3/3] Fixes docs --- video/cloud-client/quickstart/README.rst | 71 --------------------- video/cloud-client/quickstart/README.rst.in | 1 - 2 files changed, 72 deletions(-) diff --git a/video/cloud-client/quickstart/README.rst b/video/cloud-client/quickstart/README.rst index ca74c0e5ef2..2064dafe034 100644 --- a/video/cloud-client/quickstart/README.rst +++ b/video/cloud-client/quickstart/README.rst @@ -81,77 +81,6 @@ To run this sample: $ python quickstart.py - - Processing video for label annotations: - . - Finished processing. - Label description: Animal - Locations: - 0: Entire video - 1: 0.0 to 14.840051 - - - Label description: Cat - Locations: - 0: Entire video - 1: 0.0 to 14.840051 - - - Label description: Domestic long-haired cat - Locations: - 0: Entire video - 1: 0.0 to 14.840051 - - - Label description: Felidae - Locations: - 0: Entire video - 1: 0.0 to 14.840051 - - - Label description: Felinae - Locations: - 0: Entire video - 1: 0.0 to 14.840051 - - - Label description: Kitten - Locations: - 0: Entire video - 1: 0.0 to 14.840051 - - - Label description: Maine Coon - Locations: - 0: Entire video - 1: 0.0 to 14.840051 - - - Label description: Nature - Locations: - 0: Entire video - 1: 0.0 to 14.840051 - - - Label description: Pet - Locations: - 0: Entire video - 1: 0.0 to 14.840051 - - - Label description: Tabby cat - Locations: - 0: Entire video - 1: 0.0 to 14.840051 - - - Label description: Whiskers - Locations: - 0: Entire video - 1: 0.0 to 14.840051 - - - diff --git a/video/cloud-client/quickstart/README.rst.in b/video/cloud-client/quickstart/README.rst.in index 617e3fb4a5a..59014b4d5da 100644 --- a/video/cloud-client/quickstart/README.rst.in +++ b/video/cloud-client/quickstart/README.rst.in @@ -15,6 +15,5 @@ setup: samples: - name: quickstart file: quickstart.py - show_help: True cloud_client_library: true