From b686c70fe6630f735d4c58dae835b3d66e986101 Mon Sep 17 00:00:00 2001 From: Yu-Han Liu Date: Wed, 12 Jun 2019 14:11:43 -0700 Subject: [PATCH] update streaming samples [(#2215)](https://github.com/GoogleCloudPlatform/python-docs-samples/issues/2215) --- .../samples/analyze/beta_snippets.py | 37 +++++++++++++------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/packages/google-cloud-videointelligence/samples/analyze/beta_snippets.py b/packages/google-cloud-videointelligence/samples/analyze/beta_snippets.py index ffd149343bf9..2039b215c567 100644 --- a/packages/google-cloud-videointelligence/samples/analyze/beta_snippets.py +++ b/packages/google-cloud-videointelligence/samples/analyze/beta_snippets.py @@ -339,17 +339,23 @@ def stream_generator(): print(response.error.message) break - # Get the time offset of the response. - frame = response.annotation_results.label_annotations[0].frames[0] - time_offset = frame.time_offset.seconds + frame.time_offset.nanos / 1e9 - print('{}s:'.format(time_offset)) + label_annotations = response.annotation_results.label_annotations + + # label_annotations could be empty + if not label_annotations: + continue + + for annotation in label_annotations: + # Each annotation has one frame, which has a timeoffset. + frame = annotation.frames[0] + time_offset = frame.time_offset.seconds + \ + frame.time_offset.nanos / 1e9 - for annotation in response.annotation_results.label_annotations: description = annotation.entity.description - # Every annotation has only one frame confidence = annotation.frames[0].confidence # description is in Unicode - print(u'\t{} (confidence: {})'.format(description, confidence)) + print(u'{}s: {} (confidence: {})'.format( + time_offset, description, confidence)) # [END video_streaming_label_detection_beta] @@ -463,12 +469,18 @@ def stream_generator(): print(response.error.message) break - # Get the time offset of the response. - frame = response.annotation_results.object_annotations[0].frames[0] - time_offset = frame.time_offset.seconds + frame.time_offset.nanos / 1e9 - print('{}s:'.format(time_offset)) + object_annotations = response.annotation_results.object_annotations + + # object_annotations could be empty + if not object_annotations: + continue + + for annotation in object_annotations: + # Each annotation has one frame, which has a timeoffset. + frame = annotation.frames[0] + time_offset = frame.time_offset.seconds + \ + frame.time_offset.nanos / 1e9 - for annotation in response.annotation_results.object_annotations: description = annotation.entity.description confidence = annotation.confidence @@ -476,6 +488,7 @@ def stream_generator(): track_id = annotation.track_id # description is in Unicode + print('{}s'.format(time_offset)) print(u'\tEntity description: {}'.format(description)) print('\tTrack Id: {}'.format(track_id)) if annotation.entity.entity_id: