|
| 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 detect labels from a video |
| 18 | +based on the image content with the Google Cloud Video Intelligence |
| 19 | +API. |
| 20 | +
|
| 21 | +For more information, check out the documentation at |
| 22 | +https://cloud.google.com/videointelligence/docs. |
| 23 | +
|
| 24 | +Usage Example: |
| 25 | +
|
| 26 | + python labels.py gs://cloud-ml-sandbox/video/chicago.mp4 |
| 27 | +
|
| 28 | +""" |
| 29 | + |
| 30 | +# [START video_label_tutorial] |
| 31 | +# [START video_label_tutorial_imports] |
| 32 | +import argparse |
| 33 | + |
| 34 | +from google.cloud import videointelligence |
| 35 | +# [END video_label_tutorial_imports] |
| 36 | + |
| 37 | + |
| 38 | +def analyze_labels(path): |
| 39 | + """ Detects labels given a GCS path. """ |
| 40 | + # [START video_label_tutorial_construct_request] |
| 41 | + video_client = videointelligence.VideoIntelligenceServiceClient() |
| 42 | + features = [videointelligence.enums.Feature.LABEL_DETECTION] |
| 43 | + operation = video_client.annotate_video(input_uri=path, features=features) |
| 44 | + # [END video_label_tutorial_construct_request] |
| 45 | + print('\nProcessing video for label annotations:') |
| 46 | + |
| 47 | + # [START video_label_tutorial_check_operation] |
| 48 | + result = operation.result(timeout=90) |
| 49 | + print('\nFinished processing.') |
| 50 | + # [END video_label_tutorial_check_operation] |
| 51 | + |
| 52 | + # [START video_label_tutorial_parse_response] |
| 53 | + segment_labels = result.annotation_results[0].segment_label_annotations |
| 54 | + for i, segment_label in enumerate(segment_labels): |
| 55 | + print('Video label description: {}'.format( |
| 56 | + segment_label.entity.description)) |
| 57 | + for category_entity in segment_label.category_entities: |
| 58 | + print('\tLabel category description: {}'.format( |
| 59 | + category_entity.description)) |
| 60 | + |
| 61 | + for i, segment in enumerate(segment_label.segments): |
| 62 | + start_time = (segment.segment.start_time_offset.seconds + |
| 63 | + segment.segment.start_time_offset.nanos / 1e9) |
| 64 | + end_time = (segment.segment.end_time_offset.seconds + |
| 65 | + segment.segment.end_time_offset.nanos / 1e9) |
| 66 | + positions = '{}s to {}s'.format(start_time, end_time) |
| 67 | + confidence = segment.confidence |
| 68 | + print('\tSegment {}: {}'.format(i, positions)) |
| 69 | + print('\tConfidence: {}'.format(confidence)) |
| 70 | + print('\n') |
| 71 | + # [END video_label_tutorial_parse_response] |
| 72 | + |
| 73 | + |
| 74 | +if __name__ == '__main__': |
| 75 | + # [START video_label_tutorial_run_application] |
| 76 | + parser = argparse.ArgumentParser( |
| 77 | + description=__doc__, |
| 78 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 79 | + parser.add_argument('path', help='GCS file path for label detection.') |
| 80 | + args = parser.parse_args() |
| 81 | + |
| 82 | + analyze_labels(args.path) |
| 83 | + # [END video_label_tutorial_run_application] |
| 84 | +# [END video_label_tutorial] |
0 commit comments