@@ -339,17 +339,23 @@ def stream_generator():
339
339
print (response .error .message )
340
340
break
341
341
342
- # Get the time offset of the response.
343
- frame = response .annotation_results .label_annotations [0 ].frames [0 ]
344
- time_offset = frame .time_offset .seconds + frame .time_offset .nanos / 1e9
345
- print ('{}s:' .format (time_offset ))
342
+ label_annotations = response .annotation_results .label_annotations
343
+
344
+ # label_annotations could be empty
345
+ if not label_annotations :
346
+ continue
347
+
348
+ for annotation in label_annotations :
349
+ # Each annotation has one frame, which has a timeoffset.
350
+ frame = annotation .frames [0 ]
351
+ time_offset = frame .time_offset .seconds + \
352
+ frame .time_offset .nanos / 1e9
346
353
347
- for annotation in response .annotation_results .label_annotations :
348
354
description = annotation .entity .description
349
- # Every annotation has only one frame
350
355
confidence = annotation .frames [0 ].confidence
351
356
# description is in Unicode
352
- print (u'\t {} (confidence: {})' .format (description , confidence ))
357
+ print (u'{}s: {} (confidence: {})' .format (
358
+ time_offset , description , confidence ))
353
359
# [END video_streaming_label_detection_beta]
354
360
355
361
@@ -463,19 +469,26 @@ def stream_generator():
463
469
print (response .error .message )
464
470
break
465
471
466
- # Get the time offset of the response.
467
- frame = response .annotation_results .object_annotations [0 ].frames [0 ]
468
- time_offset = frame .time_offset .seconds + frame .time_offset .nanos / 1e9
469
- print ('{}s:' .format (time_offset ))
472
+ object_annotations = response .annotation_results .object_annotations
473
+
474
+ # object_annotations could be empty
475
+ if not object_annotations :
476
+ continue
477
+
478
+ for annotation in object_annotations :
479
+ # Each annotation has one frame, which has a timeoffset.
480
+ frame = annotation .frames [0 ]
481
+ time_offset = frame .time_offset .seconds + \
482
+ frame .time_offset .nanos / 1e9
470
483
471
- for annotation in response .annotation_results .object_annotations :
472
484
description = annotation .entity .description
473
485
confidence = annotation .confidence
474
486
475
487
# track_id tracks the same object in the video.
476
488
track_id = annotation .track_id
477
489
478
490
# description is in Unicode
491
+ print ('{}s' .format (time_offset ))
479
492
print (u'\t Entity description: {}' .format (description ))
480
493
print ('\t Track Id: {}' .format (track_id ))
481
494
if annotation .entity .entity_id :
0 commit comments