Skip to content

Commit b76e222

Browse files
committed
Add auto-generated Logo Recognition Samples
1 parent bbcfdd9 commit b76e222

File tree

3 files changed

+205
-1
lines changed

3 files changed

+205
-1
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
google-cloud-videointelligence==1.8.0
1+
google-cloud-videointelligence==1.10.0
22
google-cloud-storage==1.14.0
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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\tStart 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'\tEnd Time Offset : {}.{}'.format(segment_end_time_offset.seconds, segment_end_time_offset.nanos))
72+
print(u'\tConfidence : {}'.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\tLeft : {}'.format(normalized_bounding_box.left))
78+
print(u'\t\tTop : {}'.format(normalized_bounding_box.top))
79+
print(u'\t\tRight : {}'.format(normalized_bounding_box.right))
80+
print(u'\t\tBottom : {}'.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\tName : {}'.format(attribute.name))
84+
print(u'\t\t\tConfidence : {}'.format(attribute.confidence))
85+
print(u'\t\t\tValue : {}'.format(attribute.value))
86+
# Optional. Attributes in the track level.
87+
for track_attribute in track.attributes:
88+
print(u'\n\t\tName : {}'.format(track_attribute.name))
89+
print(u'\t\tConfidence : {}'.format(track_attribute.confidence))
90+
print(u'\t\tValue : {}'.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\tStart 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'\tEnd 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()
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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_gcs_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 file hosted in
25+
# GCS.
26+
# usage: python3 samples/v1p3beta1/video_detect_logo_gcs_beta.py
27+
import sys
28+
29+
# [START video_detect_logo_gcs_beta]
30+
31+
from google.cloud import videointelligence_v1p3beta1
32+
from google.cloud.videointelligence_v1p3beta1 import enums
33+
34+
def sample_annotate_video():
35+
36+
client = videointelligence_v1p3beta1.VideoIntelligenceServiceClient()
37+
38+
input_uri = 'gs://cloud-samples-data/video/googlework_short.mp4'
39+
features_element = enums.Feature.LOGO_RECOGNITION
40+
features = [features_element]
41+
42+
operation = client.annotate_video(input_uri=input_uri, features=features)
43+
44+
print(u'Waiting for operation to complete...')
45+
response = operation.result()
46+
47+
# Get the first response, since we sent only one video.
48+
annotation_result = response.annotation_results[0]
49+
# Annotations for list of logos detected, tracked and recognized in video.
50+
for logo_recognition_annotation in annotation_result.logo_recognition_annotations:
51+
entity = logo_recognition_annotation.entity
52+
# Opaque entity ID. Some IDs may be available in [Google Knowledge Graph Search API](https://developers.google.com/knowledge-graph/).
53+
print(u'Entity Id : {}'.format(entity.entity_id))
54+
# Textual description, e.g. `Google`.
55+
print(u'Description : {}'.format(entity.description))
56+
# All logo tracks where the recognized logo appears. Each track corresponds to one logo instance appearing in consecutive frames.
57+
for track in logo_recognition_annotation.tracks:
58+
# Video segment of a track.
59+
segment = track.segment
60+
segment_start_time_offset = segment.start_time_offset
61+
print(u'\n\tStart Time Offset : {}.{}'.format(segment_start_time_offset.seconds, segment_start_time_offset.nanos))
62+
segment_end_time_offset = segment.end_time_offset
63+
print(u'\tEnd Time Offset : {}.{}'.format(segment_end_time_offset.seconds, segment_end_time_offset.nanos))
64+
print(u'\tConfidence : {}'.format(track.confidence))
65+
# The object with timestamp and attributes per frame in the track.
66+
for timestamped_object in track.timestamped_objects:
67+
# Normalized Bounding box in a frame, where the object is located.
68+
normalized_bounding_box = timestamped_object.normalized_bounding_box
69+
print(u'\n\t\tLeft : {}'.format(normalized_bounding_box.left))
70+
print(u'\t\tTop : {}'.format(normalized_bounding_box.top))
71+
print(u'\t\tRight : {}'.format(normalized_bounding_box.right))
72+
print(u'\t\tBottom : {}'.format(normalized_bounding_box.bottom))
73+
# Optional. The attributes of the object in the bounding box.
74+
for attribute in timestamped_object.attributes:
75+
print(u'\n\t\t\tName : {}'.format(attribute.name))
76+
print(u'\t\t\tConfidence : {}'.format(attribute.confidence))
77+
print(u'\t\t\tValue : {}'.format(attribute.value))
78+
# Optional. Attributes in the track level.
79+
for track_attribute in track.attributes:
80+
print(u'\n\t\tName : {}'.format(track_attribute.name))
81+
print(u'\t\tConfidence : {}'.format(track_attribute.confidence))
82+
print(u'\t\tValue : {}'.format(track_attribute.value))
83+
# All video segments where the recognized logo appears. There might be multiple instances of the same logo class appearing in one VideoSegment.
84+
for logo_recognition_annotation_segment in logo_recognition_annotation.segments:
85+
logo_recognition_annotation_segment_start_time_offset = logo_recognition_annotation_segment.start_time_offset
86+
print(u'\n\tStart Time Offset : {}.{}'.format(logo_recognition_annotation_segment_start_time_offset.seconds, logo_recognition_annotation_segment_start_time_offset.nanos))
87+
logo_recognition_annotation_segment_end_time_offset = logo_recognition_annotation_segment.end_time_offset
88+
print(u'\tEnd Time Offset : {}.{}'.format(logo_recognition_annotation_segment_end_time_offset.seconds, logo_recognition_annotation_segment_end_time_offset.nanos))
89+
# [END video_detect_logo_gcs_beta]
90+
91+
def main():
92+
sample_annotate_video()
93+
94+
if __name__ == '__main__':
95+
main()

0 commit comments

Comments
 (0)