|
| 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] |
0 commit comments