|
| 1 | +# Copyright 2020 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import io |
| 16 | + |
| 17 | +# [START video_detect_logo_beta] |
| 18 | + |
| 19 | +from google.cloud import videointelligence_v1p3beta1 |
| 20 | +from google.cloud.videointelligence_v1p3beta1 import enums |
| 21 | + |
| 22 | + |
| 23 | +def sample_annotate_video(local_file_path="resources/googlework_tiny.mp4"): |
| 24 | + """Performs asynchronous video annotation for logo recognition on a local file.""" |
| 25 | + |
| 26 | + client = videointelligence_v1p3beta1.VideoIntelligenceServiceClient() |
| 27 | + |
| 28 | + with io.open(local_file_path, "rb") as f: |
| 29 | + input_content = f.read() |
| 30 | + features_element = enums.Feature.LOGO_RECOGNITION |
| 31 | + features = [features_element] |
| 32 | + |
| 33 | + operation = client.annotate_video(input_content=input_content, features=features) |
| 34 | + |
| 35 | + print(u"Waiting for operation to complete...") |
| 36 | + response = operation.result() |
| 37 | + |
| 38 | + # Get the first response, since we sent only one video. |
| 39 | + annotation_result = response.annotation_results[0] |
| 40 | + # Annotations for list of logos detected, tracked and recognized in video. |
| 41 | + for logo_recognition_annotation in annotation_result.logo_recognition_annotations: |
| 42 | + entity = logo_recognition_annotation.entity |
| 43 | + # Opaque entity ID. Some IDs may be available in [Google Knowledge Graph |
| 44 | + # Search API](https://developers.google.com/knowledge-graph/). |
| 45 | + print(u"Entity Id : {}".format(entity.entity_id)) |
| 46 | + # Textual description, e.g. `Google`. |
| 47 | + print(u"Description : {}".format(entity.description)) |
| 48 | + # All logo tracks where the recognized logo appears. Each track corresponds |
| 49 | + # to one logo instance appearing in consecutive frames. |
| 50 | + for track in logo_recognition_annotation.tracks: |
| 51 | + # Video segment of a track. |
| 52 | + segment = track.segment |
| 53 | + segment_start_time_offset = segment.start_time_offset |
| 54 | + print( |
| 55 | + u"\n\tStart Time Offset : {}.{}".format( |
| 56 | + segment_start_time_offset.seconds, segment_start_time_offset.nanos |
| 57 | + ) |
| 58 | + ) |
| 59 | + segment_end_time_offset = segment.end_time_offset |
| 60 | + print( |
| 61 | + u"\tEnd Time Offset : {}.{}".format( |
| 62 | + segment_end_time_offset.seconds, segment_end_time_offset.nanos |
| 63 | + ) |
| 64 | + ) |
| 65 | + print(u"\tConfidence : {}".format(track.confidence)) |
| 66 | + # The object with timestamp and attributes per frame in the track. |
| 67 | + for timestamped_object in track.timestamped_objects: |
| 68 | + # Normalized Bounding box in a frame, where the object is located. |
| 69 | + normalized_bounding_box = timestamped_object.normalized_bounding_box |
| 70 | + print(u"\n\t\tLeft : {}".format(normalized_bounding_box.left)) |
| 71 | + print(u"\t\tTop : {}".format(normalized_bounding_box.top)) |
| 72 | + print(u"\t\tRight : {}".format(normalized_bounding_box.right)) |
| 73 | + print(u"\t\tBottom : {}".format(normalized_bounding_box.bottom)) |
| 74 | + # Optional. The attributes of the object in the bounding box. |
| 75 | + for attribute in timestamped_object.attributes: |
| 76 | + print(u"\n\t\t\tName : {}".format(attribute.name)) |
| 77 | + print(u"\t\t\tConfidence : {}".format(attribute.confidence)) |
| 78 | + print(u"\t\t\tValue : {}".format(attribute.value)) |
| 79 | + # Optional. Attributes in the track level. |
| 80 | + for track_attribute in track.attributes: |
| 81 | + print(u"\n\t\tName : {}".format(track_attribute.name)) |
| 82 | + print(u"\t\tConfidence : {}".format(track_attribute.confidence)) |
| 83 | + print(u"\t\tValue : {}".format(track_attribute.value)) |
| 84 | + # All video segments where the recognized logo appears. There might be |
| 85 | + # multiple instances of the same logo class appearing in one VideoSegment. |
| 86 | + for logo_recognition_annotation_segment in logo_recognition_annotation.segments: |
| 87 | + logo_recognition_annotation_segment_start_time_offset = ( |
| 88 | + logo_recognition_annotation_segment.start_time_offset |
| 89 | + ) |
| 90 | + print( |
| 91 | + u"\n\tStart Time Offset : {}.{}".format( |
| 92 | + logo_recognition_annotation_segment_start_time_offset.seconds, |
| 93 | + logo_recognition_annotation_segment_start_time_offset.nanos, |
| 94 | + ) |
| 95 | + ) |
| 96 | + logo_recognition_annotation_segment_end_time_offset = ( |
| 97 | + logo_recognition_annotation_segment.end_time_offset |
| 98 | + ) |
| 99 | + print( |
| 100 | + u"\tEnd Time Offset : {}.{}".format( |
| 101 | + logo_recognition_annotation_segment_end_time_offset.seconds, |
| 102 | + logo_recognition_annotation_segment_end_time_offset.nanos, |
| 103 | + ) |
| 104 | + ) |
| 105 | + |
| 106 | + |
| 107 | +# [END video_detect_logo_beta] |
0 commit comments