From 10aafedcddd599d0d2bd27da35e7beae1f2ec2bc Mon Sep 17 00:00:00 2001 From: Yu-Han Liu Date: Wed, 12 Jun 2019 13:59:48 -0700 Subject: [PATCH] update streaming samples --- video/cloud-client/analyze/beta_snippets.py | 37 ++++++++++++++------- 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/video/cloud-client/analyze/beta_snippets.py b/video/cloud-client/analyze/beta_snippets.py index ffd149343bf..2039b215c56 100644 --- a/video/cloud-client/analyze/beta_snippets.py +++ b/video/cloud-client/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: