|
| 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 | +import argparse |
| 25 | +import sys |
| 26 | +import time |
| 27 | + |
| 28 | +from google.cloud.gapic.videointelligence.v1beta1 import enums |
| 29 | +from google.cloud.gapic.videointelligence.v1beta1 import ( |
| 30 | + video_intelligence_service_client) |
| 31 | + |
| 32 | + |
| 33 | +def analyze_faces(path): |
| 34 | + """ Detects faces given a GCS path. """ |
| 35 | + video_client = (video_intelligence_service_client. |
| 36 | + VideoIntelligenceServiceClient()) |
| 37 | + features = [enums.Feature.FACE_DETECTION] |
| 38 | + operation = video_client.annotate_video(path, features) |
| 39 | + print('\nProcessing video for label annotations:') |
| 40 | + |
| 41 | + while not operation.done(): |
| 42 | + sys.stdout.write('.') |
| 43 | + sys.stdout.flush() |
| 44 | + time.sleep(1) |
| 45 | + |
| 46 | + print('\nFinished processing.') |
| 47 | + |
| 48 | + # first result is retrieved because a single video was processed |
| 49 | + face_annotations = (operation.result().annotation_results[0]. |
| 50 | + face_annotations) |
| 51 | + |
| 52 | + for face_id, face in enumerate(face_annotations): |
| 53 | + print('Thumbnail size: {}'.format(len(face.thumbnail))) |
| 54 | + |
| 55 | + for segment_id, segment in enumerate(face.segments): |
| 56 | + print('Track {}: {} to {}'.format( |
| 57 | + segment_id, |
| 58 | + segment.start_time_offset, |
| 59 | + segment.end_time_offset)) |
| 60 | + |
| 61 | + |
| 62 | +def analyze_labels(path): |
| 63 | + """ Detects labels given a GCS path. """ |
| 64 | + video_client = (video_intelligence_service_client. |
| 65 | + VideoIntelligenceServiceClient()) |
| 66 | + features = [enums.Feature.LABEL_DETECTION] |
| 67 | + operation = video_client.annotate_video(path, features) |
| 68 | + print('\nProcessing video for label annotations:') |
| 69 | + |
| 70 | + while not operation.done(): |
| 71 | + sys.stdout.write('.') |
| 72 | + sys.stdout.flush() |
| 73 | + time.sleep(1) |
| 74 | + |
| 75 | + print('\nFinished processing.') |
| 76 | + |
| 77 | + # first result is retrieved because a single video was processed |
| 78 | + results = operation.result().annotation_results[0] |
| 79 | + |
| 80 | + for i, label in enumerate(results.label_annotations): |
| 81 | + print('Label description: {}'.format(label.description)) |
| 82 | + print('Locations:') |
| 83 | + |
| 84 | + for l, location in enumerate(label.locations): |
| 85 | + print('\t{}: {} to {}'.format( |
| 86 | + l, |
| 87 | + location.segment.start_time_offset, |
| 88 | + location.segment.end_time_offset)) |
| 89 | + |
| 90 | + |
| 91 | +def analyze_shots(path): |
| 92 | + """ Detects camera shot changes. """ |
| 93 | + video_client = (video_intelligence_service_client. |
| 94 | + VideoIntelligenceServiceClient()) |
| 95 | + features = [enums.Feature.SHOT_CHANGE_DETECTION] |
| 96 | + operation = video_client.annotate_video(path, features) |
| 97 | + print('\nProcessing video for shot change annotations:') |
| 98 | + |
| 99 | + while not operation.done(): |
| 100 | + sys.stdout.write('.') |
| 101 | + sys.stdout.flush() |
| 102 | + time.sleep(1) |
| 103 | + |
| 104 | + print('\nFinished processing.') |
| 105 | + |
| 106 | + # first result is retrieved because a single video was processed |
| 107 | + shots = operation.result().annotation_results[0] |
| 108 | + |
| 109 | + for note, shot in enumerate(shots.shot_annotations): |
| 110 | + print('Scene {}: {} to {}'.format( |
| 111 | + note, |
| 112 | + shot.start_time_offset, |
| 113 | + shot.end_time_offset)) |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == '__main__': |
| 117 | + parser = argparse.ArgumentParser( |
| 118 | + description=__doc__, |
| 119 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 120 | + subparsers = parser.add_subparsers(dest='command') |
| 121 | + analyze_faces_parser = subparsers.add_parser( |
| 122 | + 'faces', help=analyze_faces.__doc__) |
| 123 | + analyze_faces_parser.add_argument('path') |
| 124 | + analyze_labels_parser = subparsers.add_parser( |
| 125 | + 'labels', help=analyze_labels.__doc__) |
| 126 | + analyze_labels_parser.add_argument('path') |
| 127 | + analyze_shots_parser = subparsers.add_parser( |
| 128 | + 'shots', help=analyze_shots.__doc__) |
| 129 | + analyze_shots_parser.add_argument('path') |
| 130 | + |
| 131 | + args = parser.parse_args() |
| 132 | + |
| 133 | + if args.command == 'faces': |
| 134 | + analyze_faces(args.path) |
| 135 | + if args.command == 'labels': |
| 136 | + analyze_labels(args.path) |
| 137 | + if args.command == 'shots': |
| 138 | + analyze_shots(args.path) |
0 commit comments