From 29b006045f6ce5a9e762f09fbe00be1257bfa4fc Mon Sep 17 00:00:00 2001 From: Gus Class Date: Fri, 5 May 2017 10:58:17 -0700 Subject: [PATCH] Adds tutorials using Cloud Client (#930) * Adds tutorials. * Removes unused enumerate --- video/cloud-client/{ => analyze}/README.md | 0 video/cloud-client/{ => analyze}/analyze.py | 2 +- .../{ => analyze}/analyze_test.py | 0 .../{ => analyze}/requirements.txt | 0 video/cloud-client/labels/README.md | 29 +++++++ video/cloud-client/labels/labels.py | 80 +++++++++++++++++++ video/cloud-client/labels/labels_test.py | 32 ++++++++ video/cloud-client/labels/requirements.txt | 1 + video/cloud-client/shotchange/README.md | 29 +++++++ .../cloud-client/shotchange/requirements.txt | 1 + video/cloud-client/shotchange/shotchange.py | 76 ++++++++++++++++++ .../shotchange/shotchange_test.py | 32 ++++++++ 12 files changed, 281 insertions(+), 1 deletion(-) rename video/cloud-client/{ => analyze}/README.md (100%) rename video/cloud-client/{ => analyze}/analyze.py (98%) rename video/cloud-client/{ => analyze}/analyze_test.py (100%) rename video/cloud-client/{ => analyze}/requirements.txt (100%) create mode 100644 video/cloud-client/labels/README.md create mode 100644 video/cloud-client/labels/labels.py create mode 100644 video/cloud-client/labels/labels_test.py create mode 100644 video/cloud-client/labels/requirements.txt create mode 100644 video/cloud-client/shotchange/README.md create mode 100644 video/cloud-client/shotchange/requirements.txt create mode 100644 video/cloud-client/shotchange/shotchange.py create mode 100644 video/cloud-client/shotchange/shotchange_test.py diff --git a/video/cloud-client/README.md b/video/cloud-client/analyze/README.md similarity index 100% rename from video/cloud-client/README.md rename to video/cloud-client/analyze/README.md diff --git a/video/cloud-client/analyze.py b/video/cloud-client/analyze/analyze.py similarity index 98% rename from video/cloud-client/analyze.py rename to video/cloud-client/analyze/analyze.py index f682ea17a930..06f182ed7062 100644 --- a/video/cloud-client/analyze.py +++ b/video/cloud-client/analyze/analyze.py @@ -77,7 +77,7 @@ def analyze_labels(path): # 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:') diff --git a/video/cloud-client/analyze_test.py b/video/cloud-client/analyze/analyze_test.py similarity index 100% rename from video/cloud-client/analyze_test.py rename to video/cloud-client/analyze/analyze_test.py diff --git a/video/cloud-client/requirements.txt b/video/cloud-client/analyze/requirements.txt similarity index 100% rename from video/cloud-client/requirements.txt rename to video/cloud-client/analyze/requirements.txt diff --git a/video/cloud-client/labels/README.md b/video/cloud-client/labels/README.md new file mode 100644 index 000000000000..20d87e76bef9 --- /dev/null +++ b/video/cloud-client/labels/README.md @@ -0,0 +1,29 @@ +# 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/labels.py b/video/cloud-client/labels/labels.py new file mode 100644 index 000000000000..c879be192a91 --- /dev/null +++ b/video/cloud-client/labels/labels.py @@ -0,0 +1,80 @@ +#!/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 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. +""" + +# [START full_tutorial] +# [START imports] +import argparse +import sys +import time + +from google.cloud.gapic.videointelligence.v1beta1 import enums +from google.cloud.gapic.videointelligence.v1beta1 import ( + video_intelligence_service_client) +# [END imports] + + +def analyze_labels(path): + """ Detects labels given a GCS path. """ + # [START construct_request] + video_client = (video_intelligence_service_client. + VideoIntelligenceServiceClient()) + features = [enums.Feature.LABEL_DETECTION] + operation = video_client.annotate_video(path, features) + # [END construct_request] + print('\nProcessing video for label annotations:') + + # [START check_operation] + while not operation.done(): + sys.stdout.write('.') + sys.stdout.flush() + time.sleep(20) + + print('\nFinished processing.') + # [END check_operation] + + # [START parse_response] + results = operation.result().annotation_results[0] + + for label in 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)) + # [END parse_response] + + +if __name__ == '__main__': + # [START running_app] + parser = argparse.ArgumentParser( + description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter) + parser.add_argument('path', help='GCS file path for label detection.') + args = parser.parse_args() + + analyze_labels(args.path) + # [END running_app] +# [END full_tutorial] diff --git a/video/cloud-client/labels/labels_test.py b/video/cloud-client/labels/labels_test.py new file mode 100644 index 000000000000..cd571b087f1e --- /dev/null +++ b/video/cloud-client/labels/labels_test.py @@ -0,0 +1,32 @@ +#!/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 os + +import pytest + +import labels + +BUCKET = os.environ['CLOUD_STORAGE_BUCKET'] +LABELS_FILE_PATH = '/video/cat.mp4' + + +@pytest.mark.slow +def test_feline_video_labels(capsys): + labels.analyze_labels( + 'gs://{}{}'.format(BUCKET, LABELS_FILE_PATH)) + out, _ = capsys.readouterr() + assert 'Whiskers' in out diff --git a/video/cloud-client/labels/requirements.txt b/video/cloud-client/labels/requirements.txt new file mode 100644 index 000000000000..ba92ac973b77 --- /dev/null +++ b/video/cloud-client/labels/requirements.txt @@ -0,0 +1 @@ +https://storage.googleapis.com/videointelligence-alpha/videointelligence-python.zip diff --git a/video/cloud-client/shotchange/README.md b/video/cloud-client/shotchange/README.md new file mode 100644 index 000000000000..1b510a0fd7de --- /dev/null +++ b/video/cloud-client/shotchange/README.md @@ -0,0 +1,29 @@ +# 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 shotchange.py gs://cloudmleap/video/googlework.mp4 +``` diff --git a/video/cloud-client/shotchange/requirements.txt b/video/cloud-client/shotchange/requirements.txt new file mode 100644 index 000000000000..ba92ac973b77 --- /dev/null +++ b/video/cloud-client/shotchange/requirements.txt @@ -0,0 +1 @@ +https://storage.googleapis.com/videointelligence-alpha/videointelligence-python.zip diff --git a/video/cloud-client/shotchange/shotchange.py b/video/cloud-client/shotchange/shotchange.py new file mode 100644 index 000000000000..d6ecc88c0b00 --- /dev/null +++ b/video/cloud-client/shotchange/shotchange.py @@ -0,0 +1,76 @@ +#!/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 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. +""" + +# [START full_tutorial] +# [START imports] +import argparse +import sys +import time + +from google.cloud.gapic.videointelligence.v1beta1 import enums +from google.cloud.gapic.videointelligence.v1beta1 import ( + video_intelligence_service_client) +# [END imports] + + +def analyze_shots(path): + """ Detects camera shot changes. """ + # [START construct_request] + video_client = (video_intelligence_service_client. + VideoIntelligenceServiceClient()) + features = [enums.Feature.SHOT_CHANGE_DETECTION] + operation = video_client.annotate_video(path, features) + # [END construct_request] + print('\nProcessing video for shot change annotations:') + + # [START check_operation] + while not operation.done(): + sys.stdout.write('.') + sys.stdout.flush() + time.sleep(20) + + print('\nFinished processing.') + # [END check_operation] + + # [START parse_response] + shots = operation.result().annotation_results[0] + + for note, shot in enumerate(shots.shot_annotations): + print('Scene {}: {} to {}'.format( + note, + shot.start_time_offset, + shot.end_time_offset)) + # [END parse_response] + + +if __name__ == '__main__': + # [START running_app] + parser = argparse.ArgumentParser( + description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter) + parser.add_argument('path', help='GCS file path for label detection.') + args = parser.parse_args() + + analyze_shots(args.path) + # [END running_app] +# [END full_tutorial] diff --git a/video/cloud-client/shotchange/shotchange_test.py b/video/cloud-client/shotchange/shotchange_test.py new file mode 100644 index 000000000000..2c637036fcfa --- /dev/null +++ b/video/cloud-client/shotchange/shotchange_test.py @@ -0,0 +1,32 @@ +#!/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 os + +import pytest + +import shotchange + +BUCKET = os.environ['CLOUD_STORAGE_BUCKET'] +SHOTS_FILE_PATH = '/video/gbikes_dinosaur.mp4' + + +@pytest.mark.slow +def test_shots_dino(capsys): + shotchange.analyze_shots( + 'gs://{}{}'.format(BUCKET, SHOTS_FILE_PATH)) + out, _ = capsys.readouterr() + assert 'Scene 1:' in out