1+ # -*- coding: utf-8 -*-
2+ #
3+ # Copyright 2019 Google LLC
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+ # https://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+ # DO NOT EDIT! This is a generated sample ("LongRunningPromise", "video_detect_logo_beta")
18+
19+ # To install the latest published package dependency, execute the following:
20+ # pip install google-cloud-video-intelligence
21+
22+ # sample-metadata
23+ # title:
24+ # description: Performs asynchronous video annotation for logo recognition on a local file.
25+ # usage: python3 samples/v1p3beta1/video_detect_logo_beta.py [--local_file_path "resources/googlework_short.mp4"]
26+ import sys
27+
28+ # [START video_detect_logo_beta]
29+
30+ from google .cloud import videointelligence_v1p3beta1
31+ from google .cloud .videointelligence_v1p3beta1 import enums
32+ import io
33+
34+ def sample_annotate_video (local_file_path ):
35+ """
36+ Performs asynchronous video annotation for logo recognition on a local file.
37+
38+ Args:
39+ local_file_path Path to local video file, e.g. /path/video.mp4
40+ """
41+
42+ client = videointelligence_v1p3beta1 .VideoIntelligenceServiceClient ()
43+
44+ # local_file_path = 'resources/googlework_short.mp4'
45+ with io .open (local_file_path , 'rb' ) as f :
46+ input_content = f .read ()
47+ features_element = enums .Feature .LOGO_RECOGNITION
48+ features = [features_element ]
49+
50+ operation = client .annotate_video (input_content = input_content , features = features )
51+
52+ print (u'Waiting for operation to complete...' )
53+ response = operation .result ()
54+
55+ # Get the first response, since we sent only one video.
56+ annotation_result = response .annotation_results [0 ]
57+ # Annotations for list of logos detected, tracked and recognized in video.
58+ for logo_recognition_annotation in annotation_result .logo_recognition_annotations :
59+ entity = logo_recognition_annotation .entity
60+ # Opaque entity ID. Some IDs may be available in [Google Knowledge Graph Search API](https://developers.google.com/knowledge-graph/).
61+ print (u'Entity Id : {}' .format (entity .entity_id ))
62+ # Textual description, e.g. `Google`.
63+ print (u'Description : {}' .format (entity .description ))
64+ # All logo tracks where the recognized logo appears. Each track corresponds to one logo instance appearing in consecutive frames.
65+ for track in logo_recognition_annotation .tracks :
66+ # Video segment of a track.
67+ segment = track .segment
68+ segment_start_time_offset = segment .start_time_offset
69+ print (u'\n \t Start Time Offset : {}.{}' .format (segment_start_time_offset .seconds , segment_start_time_offset .nanos ))
70+ segment_end_time_offset = segment .end_time_offset
71+ print (u'\t End Time Offset : {}.{}' .format (segment_end_time_offset .seconds , segment_end_time_offset .nanos ))
72+ print (u'\t Confidence : {}' .format (track .confidence ))
73+ # The object with timestamp and attributes per frame in the track.
74+ for timestamped_object in track .timestamped_objects :
75+ # Normalized Bounding box in a frame, where the object is located.
76+ normalized_bounding_box = timestamped_object .normalized_bounding_box
77+ print (u'\n \t \t Left : {}' .format (normalized_bounding_box .left ))
78+ print (u'\t \t Top : {}' .format (normalized_bounding_box .top ))
79+ print (u'\t \t Right : {}' .format (normalized_bounding_box .right ))
80+ print (u'\t \t Bottom : {}' .format (normalized_bounding_box .bottom ))
81+ # Optional. The attributes of the object in the bounding box.
82+ for attribute in timestamped_object .attributes :
83+ print (u'\n \t \t \t Name : {}' .format (attribute .name ))
84+ print (u'\t \t \t Confidence : {}' .format (attribute .confidence ))
85+ print (u'\t \t \t Value : {}' .format (attribute .value ))
86+ # Optional. Attributes in the track level.
87+ for track_attribute in track .attributes :
88+ print (u'\n \t \t Name : {}' .format (track_attribute .name ))
89+ print (u'\t \t Confidence : {}' .format (track_attribute .confidence ))
90+ print (u'\t \t Value : {}' .format (track_attribute .value ))
91+ # All video segments where the recognized logo appears. There might be multiple instances of the same logo class appearing in one VideoSegment.
92+ for logo_recognition_annotation_segment in logo_recognition_annotation .segments :
93+ logo_recognition_annotation_segment_start_time_offset = logo_recognition_annotation_segment .start_time_offset
94+ print (u'\n \t Start Time Offset : {}.{}' .format (logo_recognition_annotation_segment_start_time_offset .seconds , logo_recognition_annotation_segment_start_time_offset .nanos ))
95+ logo_recognition_annotation_segment_end_time_offset = logo_recognition_annotation_segment .end_time_offset
96+ print (u'\t End Time Offset : {}.{}' .format (logo_recognition_annotation_segment_end_time_offset .seconds , logo_recognition_annotation_segment_end_time_offset .nanos ))
97+ # [END video_detect_logo_beta]
98+
99+ def main ():
100+ import argparse
101+
102+ parser = argparse .ArgumentParser ()
103+ parser .add_argument ('--local_file_path' , type = str , default = 'resources/googlework_short.mp4' )
104+ args = parser .parse_args ()
105+
106+ sample_annotate_video (args .local_file_path )
107+
108+ if __name__ == '__main__' :
109+ main ()
0 commit comments