From 1d7a1a3ee32a8cbaaa7966cb3fb469bde1475652 Mon Sep 17 00:00:00 2001 From: Gus Class Date: Mon, 15 May 2017 12:37:12 -0700 Subject: [PATCH 1/4] Adds new examples, replaces markdown with restructured text --- video/cloud-client/analyze/README.md | 32 ----- video/cloud-client/analyze/README.rst | 126 ++++++++++++++++++ video/cloud-client/analyze/README.rst.in | 20 +++ video/cloud-client/analyze/analyze.py | 138 +++++++++++++++++--- video/cloud-client/analyze/analyze_test.py | 12 +- video/cloud-client/faces/README.md | 29 ---- video/cloud-client/faces/README.rst | 120 +++++++++++++++++ video/cloud-client/faces/README.rst.in | 20 +++ video/cloud-client/faces/faces.py | 5 + video/cloud-client/labels/README.md | 29 ---- video/cloud-client/labels/README.rst | 120 +++++++++++++++++ video/cloud-client/labels/README.rst.in | 20 +++ video/cloud-client/labels/labels.py | 5 + video/cloud-client/shotchange/README.md | 29 ---- video/cloud-client/shotchange/README.rst | 120 +++++++++++++++++ video/cloud-client/shotchange/README.rst.in | 20 +++ video/cloud-client/shotchange/shotchange.py | 5 + 17 files changed, 709 insertions(+), 141 deletions(-) delete mode 100644 video/cloud-client/analyze/README.md create mode 100644 video/cloud-client/analyze/README.rst create mode 100644 video/cloud-client/analyze/README.rst.in delete mode 100644 video/cloud-client/faces/README.md create mode 100644 video/cloud-client/faces/README.rst create mode 100644 video/cloud-client/faces/README.rst.in delete mode 100644 video/cloud-client/labels/README.md create mode 100644 video/cloud-client/labels/README.rst create mode 100644 video/cloud-client/labels/README.rst.in delete mode 100644 video/cloud-client/shotchange/README.md create mode 100644 video/cloud-client/shotchange/README.rst create mode 100644 video/cloud-client/shotchange/README.rst.in diff --git a/video/cloud-client/analyze/README.md b/video/cloud-client/analyze/README.md deleted file mode 100644 index 52fdd058d6b2..000000000000 --- a/video/cloud-client/analyze/README.md +++ /dev/null @@ -1,32 +0,0 @@ -# Google Cloud Video Intelligence Sample - -Demonstrates face detection, label detection, and shot change detection using -the Google Cloud API.. - -## Setup -Please follow the [Set Up Your Project](https://cloud.google.com/video-intelligence/docs/getting-started#set_up_your_project) -steps in the Quickstart doc to create a project and enable the Google Cloud -Video Intelligence API. Following those steps, make sure that you -[Set Up a Service Account](https://cloud.google.com/video-intelligence/docs/common/auth#set_up_a_service_account), -and export the following environment variable: - -``` -export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your-project-credentials.json -``` - -## Run the sample - -Install [pip](https://pip.pypa.io/en/stable/installing) if not already installed. - -Install the necessary libraries using pip: - -```sh -$ pip install -r requirements.txt -``` - -Run the sample, for example: -``` -python analyze.py faces gs://demomaker/google_gmail.mp4 -python analyze.py labels gs://demomaker/cat.mp4 -python analyze.py shots gs://demomaker/gbikes_dinosaur.mp4 -``` diff --git a/video/cloud-client/analyze/README.rst b/video/cloud-client/analyze/README.rst new file mode 100644 index 000000000000..f8c44d28998d --- /dev/null +++ b/video/cloud-client/analyze/README.rst @@ -0,0 +1,126 @@ +.. 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 +------------------------------------------------------------------------------- + +analyze ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + + +To run this sample: + +.. code-block:: bash + + $ python analyze.py + + usage: analyze.py [-h] {faces,labels,labels_file,safe_search,shots} ... + + This application demonstrates face detection, label detection, safe search, + and shot change detection using the Google Cloud API. + + Usage Examples: + + python analyze.py faces gs://demomaker/volleyball_court.mp4 + python analyze.py labels gs://demomaker/cat.mp4 + python analyze.py labels_file resources/cat.mp4 + python analyze.py shots gs://demomaker/gbikes_dinosaur.mp4 + python analyze.py safe_search gs://demomaker/cat.mp4 + + positional arguments: + {faces,labels,labels_file,safe_search,shots} + faces Detects faces given a GCS path. + labels Detects labels given a GCS path. + labels_file Detects labels given a file path. + safe_search Detects safe search features the GCS path to a video. + shots Detects camera shot changes. + + optional arguments: + -h, --help show this help message and exit + + + + +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/ diff --git a/video/cloud-client/analyze/README.rst.in b/video/cloud-client/analyze/README.rst.in new file mode 100644 index 000000000000..4249d48aed52 --- /dev/null +++ b/video/cloud-client/analyze/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: analyze + file: analyze.py + show_help: True + +cloud_client_library: true diff --git a/video/cloud-client/analyze/analyze.py b/video/cloud-client/analyze/analyze.py index 06f182ed7062..3b9ab19097e6 100644 --- a/video/cloud-client/analyze/analyze.py +++ b/video/cloud-client/analyze/analyze.py @@ -14,14 +14,21 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""This application demonstrates how to perform basic operations with the -Google Cloud Video Intelligence API. +"""This application demonstrates face detection, label detection, safe search, +and shot change detection using the Google Cloud API. + +Usage Examples: + + python analyze.py faces gs://demomaker/volleyball_court.mp4 + python analyze.py labels gs://demomaker/cat.mp4 + python analyze.py labels_file resources/cat.mp4 + python analyze.py shots gs://demomaker/gbikes_dinosaur.mp4 + python analyze.py safe_search gs://demomaker/cat.mp4 -For more information, check out the documentation at -https://cloud.google.com/videointelligence/docs. """ import argparse +import base64 import sys import time @@ -30,18 +37,49 @@ video_intelligence_service_client) +def analyze_safe_search(path): + """ Detects safe search features the GCS path to a video. """ + video_client = (video_intelligence_service_client. + VideoIntelligenceServiceClient()) + features = [enums.Feature.SAFE_SEARCH_DETECTION] + operation = video_client.annotate_video(path, features) + print('\nProcessing video for safe search annotations:') + + while not operation.done(): + sys.stdout.write('.') + sys.stdout.flush() + time.sleep(1) + + print('\nFinished processing.') + + # first result is retrieved because a single video was processed + safe_annotations = (operation.result().annotation_results[0]. + safe_search_annotations) + + likely_string = ("Unknown", "Very unlikely", "Unlikely", "Possible", + "Likely", "Very likely") + + for note in safe_annotations: + print('Time: {}s').format(note.time_offset / 1000000.0) + print('\tadult: {}').format(likely_string[note.adult]) + print('\tspoof: {}').format(likely_string[note.spoof]) + print('\tmedical: {}').format(likely_string[note.medical]) + print('\tracy: {}').format(likely_string[note.racy]) + print('\tviolent: {}\n').format(likely_string[note.violent]) + + def analyze_faces(path): """ Detects faces given a GCS path. """ video_client = (video_intelligence_service_client. VideoIntelligenceServiceClient()) features = [enums.Feature.FACE_DETECTION] operation = video_client.annotate_video(path, features) - print('\nProcessing video for label annotations:') + print('\nProcessing video for face annotations:') while not operation.done(): sys.stdout.write('.') sys.stdout.flush() - time.sleep(20) + time.sleep(1) print('\nFinished processing.') @@ -53,10 +91,16 @@ def analyze_faces(path): print('Thumbnail size: {}'.format(len(face.thumbnail))) for segment_id, segment in enumerate(face.segments): - print('Track {}: {} to {}'.format( - segment_id, - segment.start_time_offset, - segment.end_time_offset)) + positions = 'Entire video' + if (segment.start_time_offset != -1 or + segment.end_time_offset != -1): + positions = '{}s to {}s'.format( + segment.start_time_offset / 1000000.0, + segment.end_time_offset / 1000000.0) + + print('\tTrack {}: {}'.format(segment_id, positions)) + + print('\n') def analyze_labels(path): @@ -70,22 +114,66 @@ def analyze_labels(path): while not operation.done(): sys.stdout.write('.') sys.stdout.flush() - time.sleep(20) + time.sleep(1) + + 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 = '{}s to {}s'.format( + location.segment.start_time_offset / 1000000.0, + location.segment.end_time_offset / 1000000.0) + print('\t{}: {}'.format(l, positions)) + + print('\n') + + +def analyze_labels_file(path): + """ Detects labels given a file path. """ + video_client = (video_intelligence_service_client. + VideoIntelligenceServiceClient()) + features = [enums.Feature.LABEL_DETECTION] + + with open(path, "rb") as movie: + content_base64 = base64.b64encode(movie.read()) + + operation = video_client.annotate_video('', features, + input_content=content_base64) + print('\nProcessing video for label annotations:') + + while not operation.done(): + sys.stdout.write('.') + sys.stdout.flush() + time.sleep(1) print('\nFinished processing.') # first result is retrieved because a single video was processed results = operation.result().annotation_results[0] - for label in results.label_annotations: + for i, label in enumerate(results.label_annotations): print('Label description: {}'.format(label.description)) print('Locations:') for l, location in enumerate(label.locations): - print('\t{}: {} to {}'.format( - l, - location.segment.start_time_offset, - location.segment.end_time_offset)) + 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') def analyze_shots(path): @@ -99,7 +187,7 @@ def analyze_shots(path): while not operation.done(): sys.stdout.write('.') sys.stdout.flush() - time.sleep(20) + time.sleep(1) print('\nFinished processing.') @@ -107,10 +195,10 @@ def analyze_shots(path): shots = operation.result().annotation_results[0] for note, shot in enumerate(shots.shot_annotations): - print('Scene {}: {} to {}'.format( + print('\tScene {}: {} to {}'.format( note, - shot.start_time_offset, - shot.end_time_offset)) + shot.start_time_offset / 1000000.0, + shot.end_time_offset / 1000000.0)) if __name__ == '__main__': @@ -124,6 +212,12 @@ def analyze_shots(path): analyze_labels_parser = subparsers.add_parser( 'labels', help=analyze_labels.__doc__) analyze_labels_parser.add_argument('path') + analyze_labels_file_parser = subparsers.add_parser( + 'labels_file', help=analyze_labels_file.__doc__) + analyze_labels_file_parser.add_argument('path') + analyze_safe_search_parser = subparsers.add_parser( + 'safe_search', help=analyze_safe_search.__doc__) + analyze_safe_search_parser.add_argument('path') analyze_shots_parser = subparsers.add_parser( 'shots', help=analyze_shots.__doc__) analyze_shots_parser.add_argument('path') @@ -134,5 +228,9 @@ def analyze_shots(path): analyze_faces(args.path) if args.command == 'labels': analyze_labels(args.path) + if args.command == 'labels_file': + analyze_labels_file(args.path) if args.command == 'shots': analyze_shots(args.path) + if args.command == 'safe_search': + analyze_safe_search(args.path) diff --git a/video/cloud-client/analyze/analyze_test.py b/video/cloud-client/analyze/analyze_test.py index f3178319b363..fecd8fff2366 100644 --- a/video/cloud-client/analyze/analyze_test.py +++ b/video/cloud-client/analyze/analyze_test.py @@ -16,13 +16,13 @@ import os -import pytest - import analyze +import pytest BUCKET = os.environ['CLOUD_STORAGE_BUCKET'] LABELS_FILE_PATH = '/video/cat.mp4' FACES_FILE_PATH = '/video/googlework.mp4' +SAFE_SEARCH_FILE_PATH = '/video/cat.mp4' SHOTS_FILE_PATH = '/video/gbikes_dinosaur.mp4' @@ -48,3 +48,11 @@ def test_dino_video_labels(capsys): 'gs://{}{}'.format(BUCKET, LABELS_FILE_PATH)) out, _ = capsys.readouterr() assert 'Whiskers' in out + + +@pytest.mark.slow +def test_cat_safe_search(capsys): + analyze.analyze_safe_search( + 'gs://{}{}'.format(BUCKET, SAFE_SEARCH_FILE_PATH)) + out, _ = capsys.readouterr() + assert 'medical' in out diff --git a/video/cloud-client/faces/README.md b/video/cloud-client/faces/README.md deleted file mode 100644 index de8d4f319466..000000000000 --- a/video/cloud-client/faces/README.md +++ /dev/null @@ -1,29 +0,0 @@ -# Google Cloud Video Intelligence - -Demonstrates face detection using the Google Cloud Video Intelligence API. - -## Setup -Please follow the [Set Up Your Project](https://cloud.google.com/video-intelligence/docs/getting-started#set_up_your_project) -steps in the Quickstart doc to create a project and enable the Google Cloud -Video Intelligence API. Following those steps, make sure that you -[Set Up a Service Account](https://cloud.google.com/video-intelligence/docs/common/auth#set_up_a_service_account), -and export the following environment variable: - -``` -export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your-project-credentials.json -``` - -## Run the sample - -Install [pip](https://pip.pypa.io/en/stable/installing) if not already installed. - -Install the necessary libraries using pip: - -```sh -$ pip install -r requirements.txt -``` - -Run the sample, for example: -``` -python faces.py gs://cloudmleap/video/googlework.mp4 -``` diff --git a/video/cloud-client/faces/README.rst b/video/cloud-client/faces/README.rst new file mode 100644 index 000000000000..fa870f1a61cd --- /dev/null +++ b/video/cloud-client/faces/README.rst @@ -0,0 +1,120 @@ +.. 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 +------------------------------------------------------------------------------- + +faces ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + + +To run this sample: + +.. code-block:: bash + + $ python faces.py + + usage: faces.py [-h] path + + This application demonstrates how to perform shot change detection with the + Google Cloud Video Intelligence API. + + For more information, check out the documentation at + https://cloud.google.com/videointelligence/docs. + + Usage Example: + + python faces.py gs://cloudmleap/video/googlework.mp4 + + positional arguments: + path GCS file path for face detection. + + optional arguments: + -h, --help show this help message and exit + + + + +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/faces/README.rst.in b/video/cloud-client/faces/README.rst.in new file mode 100644 index 000000000000..3ca6fd6eb540 --- /dev/null +++ b/video/cloud-client/faces/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: faces + file: faces.py + show_help: True + +cloud_client_library: true diff --git a/video/cloud-client/faces/faces.py b/video/cloud-client/faces/faces.py index 3b52e2270190..e4ca0d46253f 100644 --- a/video/cloud-client/faces/faces.py +++ b/video/cloud-client/faces/faces.py @@ -19,6 +19,11 @@ For more information, check out the documentation at https://cloud.google.com/videointelligence/docs. + +Usage Example: + + python faces.py gs://cloudmleap/video/googlework.mp4 + """ # [START full_tutorial] diff --git a/video/cloud-client/labels/README.md b/video/cloud-client/labels/README.md deleted file mode 100644 index 20d87e76bef9..000000000000 --- a/video/cloud-client/labels/README.md +++ /dev/null @@ -1,29 +0,0 @@ -# Google Cloud Video Intelligence - -Demonstrates label detection using the Google Cloud Video Intelligence API. - -## Setup -Please follow the [Set Up Your Project](https://cloud.google.com/video-intelligence/docs/getting-started#set_up_your_project) -steps in the Quickstart doc to create a project and enable the Google Cloud -Video Intelligence API. Following those steps, make sure that you -[Set Up a Service Account](https://cloud.google.com/video-intelligence/docs/common/auth#set_up_a_service_account), -and export the following environment variable: - -``` -export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your-project-credentials.json -``` - -## Run the sample - -Install [pip](https://pip.pypa.io/en/stable/installing) if not already installed. - -Install the necessary libraries using pip: - -```sh -$ pip install -r requirements.txt -``` - -Run the sample, for example: -``` -python labels.py gs://cloud-ml-sandbox/video/chicago.mp4 -``` diff --git a/video/cloud-client/labels/README.rst b/video/cloud-client/labels/README.rst new file mode 100644 index 000000000000..0f93579a6ab4 --- /dev/null +++ b/video/cloud-client/labels/README.rst @@ -0,0 +1,120 @@ +.. 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 +------------------------------------------------------------------------------- + +labels ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + + +To run this sample: + +.. code-block:: bash + + $ python labels.py + + usage: labels.py [-h] path + + This application demonstrates how to perform basic operations with the + Google Cloud Video Intelligence API. + + For more information, check out the documentation at + https://cloud.google.com/videointelligence/docs. + + Usage Example: + + python labels.py gs://cloud-ml-sandbox/video/chicago.mp4 + + positional arguments: + path GCS file path for label detection. + + optional arguments: + -h, --help show this help message and exit + + + + +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/labels/README.rst.in b/video/cloud-client/labels/README.rst.in new file mode 100644 index 000000000000..d5755799b935 --- /dev/null +++ b/video/cloud-client/labels/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: labels + file: labels.py + show_help: True + +cloud_client_library: true diff --git a/video/cloud-client/labels/labels.py b/video/cloud-client/labels/labels.py index c879be192a91..7e0f9a0e2d42 100644 --- a/video/cloud-client/labels/labels.py +++ b/video/cloud-client/labels/labels.py @@ -19,6 +19,11 @@ For more information, check out the documentation at https://cloud.google.com/videointelligence/docs. + +Usage Example: + + python labels.py gs://cloud-ml-sandbox/video/chicago.mp4 + """ # [START full_tutorial] diff --git a/video/cloud-client/shotchange/README.md b/video/cloud-client/shotchange/README.md deleted file mode 100644 index 4b338fa1f576..000000000000 --- a/video/cloud-client/shotchange/README.md +++ /dev/null @@ -1,29 +0,0 @@ -# Google Cloud Video Intelligence - -Demonstrates shot change detection using the Google Cloud Video Intelligence API. - -## Setup -Please follow the [Set Up Your Project](https://cloud.google.com/video-intelligence/docs/getting-started#set_up_your_project) -steps in the Quickstart doc to create a project and enable the Google Cloud -Video Intelligence API. Following those steps, make sure that you -[Set Up a Service Account](https://cloud.google.com/video-intelligence/docs/common/auth#set_up_a_service_account), -and export the following environment variable: - -``` -export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your-project-credentials.json -``` - -## Run the sample - -Install [pip](https://pip.pypa.io/en/stable/installing) if not already installed. - -Install the necessary libraries using pip: - -```sh -$ pip install -r requirements.txt -``` - -Run the sample, for example: -``` -python shotchange.py gs://cloudmleap/video/googlework.mp4 -``` diff --git a/video/cloud-client/shotchange/README.rst b/video/cloud-client/shotchange/README.rst new file mode 100644 index 000000000000..cf24f2f0245f --- /dev/null +++ b/video/cloud-client/shotchange/README.rst @@ -0,0 +1,120 @@ +.. 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 +------------------------------------------------------------------------------- + +Shot Change Detection ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + + +To run this sample: + +.. code-block:: bash + + $ python shotchange.py + + usage: shotchange.py [-h] path + + This application demonstrates how to perform basic operations with the + Google Cloud Video Intelligence API. + + For more information, check out the documentation at + https://cloud.google.com/videointelligence/docs. + + Example Usage: + + python shotchange.py gs://cloudmleap/video/googlework.mp4 + + positional arguments: + path GCS path for shot change detection. + + optional arguments: + -h, --help show this help message and exit + + + + +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/shotchange/README.rst.in b/video/cloud-client/shotchange/README.rst.in new file mode 100644 index 000000000000..9512ecdc2b86 --- /dev/null +++ b/video/cloud-client/shotchange/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: Shot Change Detection + file: shotchange.py + show_help: True + +cloud_client_library: true diff --git a/video/cloud-client/shotchange/shotchange.py b/video/cloud-client/shotchange/shotchange.py index a5ec65192d60..c1894d51eea9 100644 --- a/video/cloud-client/shotchange/shotchange.py +++ b/video/cloud-client/shotchange/shotchange.py @@ -19,6 +19,11 @@ For more information, check out the documentation at https://cloud.google.com/videointelligence/docs. + +Example Usage: + + python shotchange.py gs://cloudmleap/video/googlework.mp4 + """ # [START full_tutorial] From 4c586182c99faf51141f17a40a0b98d15e956525 Mon Sep 17 00:00:00 2001 From: Gus Class Date: Mon, 15 May 2017 14:58:32 -0700 Subject: [PATCH 2/4] Address review feedback --- video/cloud-client/analyze/analyze.py | 17 +++++++++-------- video/cloud-client/analyze/analyze_test.py | 2 ++ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/video/cloud-client/analyze/analyze.py b/video/cloud-client/analyze/analyze.py index 3b9ab19097e6..b6445d7253fc 100644 --- a/video/cloud-client/analyze/analyze.py +++ b/video/cloud-client/analyze/analyze.py @@ -29,6 +29,7 @@ import argparse import base64 +import io import sys import time @@ -48,7 +49,7 @@ def analyze_safe_search(path): while not operation.done(): sys.stdout.write('.') sys.stdout.flush() - time.sleep(1) + time.sleep(15) print('\nFinished processing.') @@ -79,7 +80,7 @@ def analyze_faces(path): while not operation.done(): sys.stdout.write('.') sys.stdout.flush() - time.sleep(1) + time.sleep(15) print('\nFinished processing.') @@ -114,7 +115,7 @@ def analyze_labels(path): while not operation.done(): sys.stdout.write('.') sys.stdout.flush() - time.sleep(1) + time.sleep(15) print('\nFinished processing.') @@ -143,17 +144,17 @@ def analyze_labels_file(path): VideoIntelligenceServiceClient()) features = [enums.Feature.LABEL_DETECTION] - with open(path, "rb") as movie: + with io.open(path, "rb") as movie: content_base64 = base64.b64encode(movie.read()) - operation = video_client.annotate_video('', features, - input_content=content_base64) + operation = video_client.annotate_video( + '', features, input_content=content_base64) print('\nProcessing video for label annotations:') while not operation.done(): sys.stdout.write('.') sys.stdout.flush() - time.sleep(1) + time.sleep(15) print('\nFinished processing.') @@ -187,7 +188,7 @@ def analyze_shots(path): while not operation.done(): sys.stdout.write('.') sys.stdout.flush() - time.sleep(1) + time.sleep(15) print('\nFinished processing.') diff --git a/video/cloud-client/analyze/analyze_test.py b/video/cloud-client/analyze/analyze_test.py index fecd8fff2366..c3b29b39404a 100644 --- a/video/cloud-client/analyze/analyze_test.py +++ b/video/cloud-client/analyze/analyze_test.py @@ -17,8 +17,10 @@ import os import analyze + import pytest + BUCKET = os.environ['CLOUD_STORAGE_BUCKET'] LABELS_FILE_PATH = '/video/cat.mp4' FACES_FILE_PATH = '/video/googlework.mp4' From f16a71f28935a6e7e1c96c06178be9128c9bc3cc Mon Sep 17 00:00:00 2001 From: Gus Class Date: Wed, 17 May 2017 21:40:19 -0700 Subject: [PATCH 3/4] Use videos from pubilc bucket, update to new client library. --- video/cloud-client/analyze/README.rst | 24 ++++++++++----------- video/cloud-client/analyze/analyze.py | 10 ++++----- video/cloud-client/analyze/requirements.txt | 2 +- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/video/cloud-client/analyze/README.rst b/video/cloud-client/analyze/README.rst index f8c44d28998d..2ecafe2429a4 100644 --- a/video/cloud-client/analyze/README.rst +++ b/video/cloud-client/analyze/README.rst @@ -8,7 +8,7 @@ This directory contains samples for Google Cloud Video Intelligence API. `Google -.. _Google Cloud Video Intelligence API: https://cloud.google.com/video-intelligence/docs +.. _Google Cloud Video Intelligence API: https://cloud.google.com/video-intelligence/docs Setup ------------------------------------------------------------------------------- @@ -82,18 +82,18 @@ To run this sample: $ python analyze.py usage: analyze.py [-h] {faces,labels,labels_file,safe_search,shots} ... - + This application demonstrates face detection, label detection, safe search, and shot change detection using the Google Cloud API. - + Usage Examples: - - python analyze.py faces gs://demomaker/volleyball_court.mp4 - python analyze.py labels gs://demomaker/cat.mp4 - python analyze.py labels_file resources/cat.mp4 - python analyze.py shots gs://demomaker/gbikes_dinosaur.mp4 - python analyze.py safe_search gs://demomaker/cat.mp4 - + + python analyze.py faces gs://cloudmleap/video/next/volleyball_court.mp4 + python analyze.py labels gs://cloudmleap/video/next/fox-snatched.mp4 + python analyze.py labels_file resources/fox-snatched.mp4 + python analyze.py shots gs://cloudmleap/video/next/gbikes_dinosaur.mp4 + python analyze.py safe_search gs://cloudmleap/video/next/animals.mp4 + positional arguments: {faces,labels,labels_file,safe_search,shots} faces Detects faces given a GCS path. @@ -101,7 +101,7 @@ To run this sample: labels_file Detects labels given a file path. safe_search Detects safe search features the GCS path to a video. shots Detects camera shot changes. - + optional arguments: -h, --help show this help message and exit @@ -123,4 +123,4 @@ to `browse the source`_ and `report issues`_. https://github.com/GoogleCloudPlatform/google-cloud-python/issues -.. _Google Cloud SDK: https://cloud.google.com/sdk/ +.. _Google Cloud SDK: https://cloud.google.com/sdk/ \ No newline at end of file diff --git a/video/cloud-client/analyze/analyze.py b/video/cloud-client/analyze/analyze.py index b6445d7253fc..813d6a4aa2a0 100644 --- a/video/cloud-client/analyze/analyze.py +++ b/video/cloud-client/analyze/analyze.py @@ -19,11 +19,11 @@ Usage Examples: - python analyze.py faces gs://demomaker/volleyball_court.mp4 - python analyze.py labels gs://demomaker/cat.mp4 - python analyze.py labels_file resources/cat.mp4 - python analyze.py shots gs://demomaker/gbikes_dinosaur.mp4 - python analyze.py safe_search gs://demomaker/cat.mp4 + python analyze.py faces gs://cloudmleap/video/next/volleyball_court.mp4 + python analyze.py labels gs://cloudmleap/video/next/fox-snatched.mp4 + python analyze.py labels_file resources/fox-snatched.mp4 + python analyze.py shots gs://cloudmleap/video/next/gbikes_dinosaur.mp4 + python analyze.py safe_search gs://cloudmleap/video/next/animals.mp4 """ diff --git a/video/cloud-client/analyze/requirements.txt b/video/cloud-client/analyze/requirements.txt index ba92ac973b77..b2aa05b67223 100644 --- a/video/cloud-client/analyze/requirements.txt +++ b/video/cloud-client/analyze/requirements.txt @@ -1 +1 @@ -https://storage.googleapis.com/videointelligence-alpha/videointelligence-python.zip +google-cloud-videointelligence==0.25.0 From 031c5a6413717a48fbc8f552e85b0f3226579ccc Mon Sep 17 00:00:00 2001 From: Gus Class Date: Thu, 18 May 2017 08:35:19 -0700 Subject: [PATCH 4/4] Style nit --- video/cloud-client/analyze/analyze_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/video/cloud-client/analyze/analyze_test.py b/video/cloud-client/analyze/analyze_test.py index c3b29b39404a..2aac755f0735 100644 --- a/video/cloud-client/analyze/analyze_test.py +++ b/video/cloud-client/analyze/analyze_test.py @@ -16,10 +16,10 @@ import os -import analyze - import pytest +import analyze + BUCKET = os.environ['CLOUD_STORAGE_BUCKET'] LABELS_FILE_PATH = '/video/cat.mp4'