From a75e025e4b1c570f1c88384e107aaf2c8f8a0fd4 Mon Sep 17 00:00:00 2001 From: Yu-Han Liu Date: Tue, 19 Sep 2017 14:42:07 -0700 Subject: [PATCH] Video v1beta2 (#1088) * update analyze_safe_search * update analyze_shots * update explicit_content_detection and test * update fece detection * update label detection (path) * update label detection (file) * flake * safe search --> explicit content * update faces tutorial * update client library quickstart * update shotchange tutorial * update labels tutorial * correct spelling * correction start_time_offset * import order * rebased --- video/cloud-client/analyze/README.rst | 12 +- video/cloud-client/analyze/analyze.py | 242 ++++++++++++------ video/cloud-client/analyze/analyze_test.py | 24 +- video/cloud-client/faces/faces.py | 18 +- video/cloud-client/faces/faces_test.py | 3 +- video/cloud-client/labels/labels.py | 34 ++- video/cloud-client/labels/labels_test.py | 5 +- video/cloud-client/quickstart/quickstart.py | 35 +-- .../quickstart/quickstart_test.py | 2 +- video/cloud-client/shotchange/shotchange.py | 23 +- .../shotchange/shotchange_test.py | 2 +- 11 files changed, 249 insertions(+), 151 deletions(-) diff --git a/video/cloud-client/analyze/README.rst b/video/cloud-client/analyze/README.rst index f5ac39139e22..d68790fdf354 100644 --- a/video/cloud-client/analyze/README.rst +++ b/video/cloud-client/analyze/README.rst @@ -59,10 +59,10 @@ To run this sample: $ python analyze.py - usage: analyze.py [-h] {faces,labels,labels_file,safe_search,shots} ... + usage: analyze.py [-h] {faces,labels,labels_file,explicit_content,shots} ... - This application demonstrates face detection, label detection, safe search, - and shot change detection using the Google Cloud API. + This application demonstrates face detection, label detection, + explicit content, and shot change detection using the Google Cloud API. Usage Examples: @@ -70,14 +70,14 @@ To run this sample: python analyze.py labels gs://cloud-ml-sandbox/video/chicago.mp4 python analyze.py labels_file resources/cat.mp4 python analyze.py shots gs://demomaker/gbikes_dinosaur.mp4 - python analyze.py safe_search gs://demomaker/gbikes_dinosaur.mp4 + python analyze.py explicit_content gs://demomaker/gbikes_dinosaur.mp4 positional arguments: - {faces,labels,labels_file,safe_search,shots} + {faces,labels,labels_file,explicit_content,shots} faces Detects faces given a GCS path. labels Detects labels given a GCS path. labels_file Detects labels given a file path. - safe_search Detects safe search features the GCS path to a video. + explicit_content Detects explicit content from the GCS path to a video. shots Detects camera shot changes. optional arguments: diff --git a/video/cloud-client/analyze/analyze.py b/video/cloud-client/analyze/analyze.py index c85f9d78a280..738f436257f8 100644 --- a/video/cloud-client/analyze/analyze.py +++ b/video/cloud-client/analyze/analyze.py @@ -14,8 +14,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""This application demonstrates face detection, label detection, safe search, -and shot change detection using the Google Cloud API. +"""This application demonstrates face detection, label detection, +explicit content, and shot change detection using the Google Cloud API. Usage Examples: @@ -23,7 +23,7 @@ python analyze.py labels gs://cloud-ml-sandbox/video/chicago.mp4 python analyze.py labels_file resources/cat.mp4 python analyze.py shots gs://demomaker/gbikes_dinosaur.mp4 - python analyze.py safe_search gs://demomaker/gbikes_dinosaur.mp4 + python analyze.py explicit_content gs://demomaker/gbikes_dinosaur.mp4 """ @@ -33,18 +33,18 @@ import sys import time -from google.cloud.gapic.videointelligence.v1beta1 import enums -from google.cloud.gapic.videointelligence.v1beta1 import ( - video_intelligence_service_client) +from google.cloud import videointelligence_v1beta2 +from google.cloud.videointelligence_v1beta2 import enums +from google.cloud.videointelligence_v1beta2 import types -def analyze_safe_search(path): - """ Detects safe search features the GCS path to a video. """ - video_client = (video_intelligence_service_client. - VideoIntelligenceServiceClient()) - features = [enums.Feature.SAFE_SEARCH_DETECTION] +def analyze_explicit_content(path): + """ Detects explicit content from the GCS path to a video. """ + video_client = videointelligence_v1beta2.VideoIntelligenceServiceClient() + features = [enums.Feature.EXPLICIT_CONTENT_DETECTION] + operation = video_client.annotate_video(path, features) - print('\nProcessing video for safe search annotations:') + print('\nProcessing video for explicit content annotations:') while not operation.done(): sys.stdout.write('.') @@ -54,27 +54,29 @@ def analyze_safe_search(path): print('\nFinished processing.') # first result is retrieved because a single video was processed - safe_annotations = (operation.result().annotation_results[0]. - safe_search_annotations) + explicit_annotation = (operation.result().annotation_results[0]. + explicit_annotation) likely_string = ("Unknown", "Very unlikely", "Unlikely", "Possible", "Likely", "Very likely") - for note in safe_annotations: - print('Time: {}s'.format(note.time_offset / 1000000.0)) - print('\tadult: {}'.format(likely_string[note.adult])) - print('\tspoof: {}'.format(likely_string[note.spoof])) - print('\tmedical: {}'.format(likely_string[note.medical])) - print('\tracy: {}'.format(likely_string[note.racy])) - print('\tviolent: {}\n'.format(likely_string[note.violent])) + for frame in explicit_annotation.frames: + frame_time = frame.time_offset.seconds + frame.time_offset.nanos / 1e9 + print('Time: {}s'.format(frame_time)) + print('\tpornography: {}'.format( + likely_string[frame.pornography_likelihood])) def analyze_faces(path): """ Detects faces given a GCS path. """ - video_client = (video_intelligence_service_client. - VideoIntelligenceServiceClient()) + video_client = videointelligence_v1beta2.VideoIntelligenceServiceClient() features = [enums.Feature.FACE_DETECTION] - operation = video_client.annotate_video(path, features) + + config = types.FaceDetectionConfig(include_bounding_boxes=True) + context = types.VideoContext(face_detection_config=config) + + operation = video_client.annotate_video( + path, features, video_context=context) print('\nProcessing video for face annotations:') while not operation.done(): @@ -89,27 +91,43 @@ def analyze_faces(path): face_annotations) for face_id, face in enumerate(face_annotations): + print('Face {}'.format(face_id)) print('Thumbnail size: {}'.format(len(face.thumbnail))) for segment_id, segment in enumerate(face.segments): - positions = 'Entire video' - if (segment.start_time_offset != -1 or - segment.end_time_offset != -1): - positions = '{}s to {}s'.format( - segment.start_time_offset / 1000000.0, - segment.end_time_offset / 1000000.0) - - print('\tTrack {}: {}'.format(segment_id, positions)) - + start_time = (segment.segment.start_time_offset.seconds + + segment.segment.start_time_offset.nanos / 1e9) + end_time = (segment.segment.end_time_offset.seconds + + segment.segment.end_time_offset.nanos / 1e9) + positions = '{}s to {}s'.format(start_time, end_time) + print('\tSegment {}: {}'.format(segment_id, positions)) + + # There are typically many frames for each face, + # here we print information on only the first frame. + frame = face.frames[0] + time_offset = (frame.time_offset.seconds + + frame.time_offset.nanos / 1e9) + box = frame.normalized_bounding_boxes[0] + print('First frame time offset: {}s'.format(time_offset)) + print('First frame normalized bounding box:') + print('\tleft: {}'.format(box.left)) + print('\ttop: {}'.format(box.top)) + print('\tright: {}'.format(box.right)) + print('\tbottom: {}'.format(box.bottom)) print('\n') def analyze_labels(path): """ Detects labels given a GCS path. """ - video_client = (video_intelligence_service_client. - VideoIntelligenceServiceClient()) + video_client = videointelligence_v1beta2.VideoIntelligenceServiceClient() features = [enums.Feature.LABEL_DETECTION] - operation = video_client.annotate_video(path, features) + + config = types.LabelDetectionConfig( + label_detection_mode=enums.LabelDetectionMode.SHOT_AND_FRAME_MODE) + context = types.VideoContext(label_detection_config=config) + + operation = video_client.annotate_video( + path, features, video_context=context) print('\nProcessing video for label annotations:') while not operation.done(): @@ -122,26 +140,65 @@ def analyze_labels(path): # first result is retrieved because a single video was processed results = operation.result().annotation_results[0] - for i, label in enumerate(results.label_annotations): - print('Label description: {}'.format(label.description)) - print('Locations:') + # Process video/segment level label annotations + for i, segment_label in enumerate(results.segment_label_annotations): + print('Video label description: {}'.format( + segment_label.entity.description)) + for category_entity in segment_label.category_entities: + print('\tLabel category description: {}'.format( + category_entity.description)) + + for i, segment in enumerate(segment_label.segments): + start_time = (segment.segment.start_time_offset.seconds + + segment.segment.start_time_offset.nanos / 1e9) + end_time = (segment.segment.end_time_offset.seconds + + segment.segment.end_time_offset.nanos / 1e9) + positions = '{}s to {}s'.format(start_time, end_time) + confidence = segment.confidence + print('\tSegment {}: {}'.format(i, positions)) + print('\tConfidence: {}'.format(confidence)) + print('\n') - for l, location in enumerate(label.locations): - positions = 'Entire video' - if (location.segment.start_time_offset != -1 or - location.segment.end_time_offset != -1): - positions = '{}s to {}s'.format( - location.segment.start_time_offset / 1000000.0, - location.segment.end_time_offset / 1000000.0) - print('\t{}: {}'.format(l, positions)) + # Process shot level label annotations + for i, shot_label in enumerate(results.shot_label_annotations): + print('Shot label description: {}'.format( + shot_label.entity.description)) + for category_entity in shot_label.category_entities: + print('\tLabel category description: {}'.format( + category_entity.description)) + + for i, shot in enumerate(shot_label.segments): + start_time = (shot.segment.start_time_offset.seconds + + shot.segment.start_time_offset.nanos / 1e9) + end_time = (shot.segment.end_time_offset.seconds + + shot.segment.end_time_offset.nanos / 1e9) + positions = '{}s to {}s'.format(start_time, end_time) + confidence = shot.confidence + print('\tSegment {}: {}'.format(i, positions)) + print('\tConfidence: {}'.format(confidence)) + print('\n') + # Process frame level label annotations + for i, frame_label in enumerate(results.frame_label_annotations): + print('Frame label description: {}'.format( + frame_label.entity.description)) + for category_entity in frame_label.category_entities: + print('\tLabel category description: {}'.format( + category_entity.description)) + + # Each frame_label_annotation has many frames, + # here we print information only about the first frame. + frame = frame_label.frames[0] + time_offset = (frame.time_offset.seconds + + frame.time_offset.nanos / 1e9) + print('\tFirst frame time offset: {}s'.format(time_offset)) + print('\tFirst frame confidence: {}'.format(frame.confidence)) print('\n') def analyze_labels_file(path): """ Detects labels given a file path. """ - video_client = (video_intelligence_service_client. - VideoIntelligenceServiceClient()) + video_client = videointelligence_v1beta2.VideoIntelligenceServiceClient() features = [enums.Feature.LABEL_DETECTION] with io.open(path, "rb") as movie: @@ -161,26 +218,64 @@ def analyze_labels_file(path): # first result is retrieved because a single video was processed results = operation.result().annotation_results[0] - for i, label in enumerate(results.label_annotations): - print('Label description: {}'.format(label.description)) - print('Locations:') + # Process video/segment level label annotations + for i, segment_label in enumerate(results.segment_label_annotations): + print('Video label description: {}'.format( + segment_label.entity.description)) + for category_entity in segment_label.category_entities: + print('\tLabel category description: {}'.format( + category_entity.description)) + + for i, segment in enumerate(segment_label.segments): + start_time = (segment.segment.start_time_offset.seconds + + segment.segment.start_time_offset.nanos / 1e9) + end_time = (segment.segment.end_time_offset.seconds + + segment.segment.end_time_offset.nanos / 1e9) + positions = '{}s to {}s'.format(start_time, end_time) + confidence = segment.confidence + print('\tSegment {}: {}'.format(i, positions)) + print('\tConfidence: {}'.format(confidence)) + print('\n') - for l, location in enumerate(label.locations): - positions = 'Entire video' - if (location.segment.start_time_offset != -1 or - location.segment.end_time_offset != -1): - positions = '{} to {}'.format( - location.segment.start_time_offset / 1000000.0, - location.segment.end_time_offset / 1000000.0) - print('\t{}: {}'.format(l, positions)) + # Process shot level label annotations + for i, shot_label in enumerate(results.shot_label_annotations): + print('Shot label description: {}'.format( + shot_label.entity.description)) + for category_entity in shot_label.category_entities: + print('\tLabel category description: {}'.format( + category_entity.description)) + + for i, shot in enumerate(shot_label.segments): + start_time = (shot.segment.start_time_offset.seconds + + shot.segment.start_time_offset.nanos / 1e9) + end_time = (shot.segment.end_time_offset.seconds + + shot.segment.end_time_offset.nanos / 1e9) + positions = '{}s to {}s'.format(start_time, end_time) + confidence = shot.confidence + print('\tSegment {}: {}'.format(i, positions)) + print('\tConfidence: {}'.format(confidence)) + print('\n') + # Process frame level label annotations + for i, frame_label in enumerate(results.frame_label_annotations): + print('Frame label description: {}'.format( + frame_label.entity.description)) + for category_entity in frame_label.category_entities: + print('\tLabel category description: {}'.format( + category_entity.description)) + + # Each frame_label_annotation has many frames, + # here we print information only about the first frame. + frame = frame_label.frames[0] + time_offset = frame.time_offset.seconds + frame.time_offset.nanos / 1e9 + print('\tFirst frame time offset: {}s'.format(time_offset)) + print('\tFirst frame confidence: {}'.format(frame.confidence)) print('\n') def analyze_shots(path): """ Detects camera shot changes. """ - video_client = (video_intelligence_service_client. - VideoIntelligenceServiceClient()) + video_client = videointelligence_v1beta2.VideoIntelligenceServiceClient() features = [enums.Feature.SHOT_CHANGE_DETECTION] operation = video_client.annotate_video(path, features) print('\nProcessing video for shot change annotations:') @@ -193,13 +288,14 @@ def analyze_shots(path): print('\nFinished processing.') # first result is retrieved because a single video was processed - shots = operation.result().annotation_results[0] + shots = operation.result().annotation_results[0].shot_annotations - for note, shot in enumerate(shots.shot_annotations): - print('\tScene {}: {} to {}'.format( - note, - shot.start_time_offset / 1000000.0, - shot.end_time_offset / 1000000.0)) + for i, shot in enumerate(shots): + start_time = (shot.start_time_offset.seconds + + shot.start_time_offset.nanos / 1e9) + end_time = (shot.end_time_offset.seconds + + shot.end_time_offset.nanos / 1e9) + print('\tShot {}: {} to {}'.format(i, start_time, end_time)) if __name__ == '__main__': @@ -216,9 +312,9 @@ def analyze_shots(path): analyze_labels_file_parser = subparsers.add_parser( 'labels_file', help=analyze_labels_file.__doc__) analyze_labels_file_parser.add_argument('path') - analyze_safe_search_parser = subparsers.add_parser( - 'safe_search', help=analyze_safe_search.__doc__) - analyze_safe_search_parser.add_argument('path') + analyze_explicit_content_parser = subparsers.add_parser( + 'explicit_content', help=analyze_explicit_content.__doc__) + analyze_explicit_content_parser.add_argument('path') analyze_shots_parser = subparsers.add_parser( 'shots', help=analyze_shots.__doc__) analyze_shots_parser.add_argument('path') @@ -233,5 +329,5 @@ def analyze_shots(path): analyze_labels_file(args.path) if args.command == 'shots': analyze_shots(args.path) - if args.command == 'safe_search': - analyze_safe_search(args.path) + if args.command == 'explicit_content': + analyze_explicit_content(args.path) diff --git a/video/cloud-client/analyze/analyze_test.py b/video/cloud-client/analyze/analyze_test.py index 2aac755f0735..487b466dde83 100644 --- a/video/cloud-client/analyze/analyze_test.py +++ b/video/cloud-client/analyze/analyze_test.py @@ -15,29 +15,27 @@ # limitations under the License. import os - -import pytest - import analyze +import pytest BUCKET = os.environ['CLOUD_STORAGE_BUCKET'] LABELS_FILE_PATH = '/video/cat.mp4' FACES_FILE_PATH = '/video/googlework.mp4' -SAFE_SEARCH_FILE_PATH = '/video/cat.mp4' +EXPLICIT_CONTENT_FILE_PATH = '/video/cat.mp4' SHOTS_FILE_PATH = '/video/gbikes_dinosaur.mp4' @pytest.mark.slow -def test_cat_video_shots(capsys): +def test_analyze_shots(capsys): analyze.analyze_shots( 'gs://{}{}'.format(BUCKET, SHOTS_FILE_PATH)) out, _ = capsys.readouterr() - assert 'Scene 1:' in out + assert 'Shot 1:' in out @pytest.mark.slow -def test_work_video_faces(capsys): +def test_analyze_faces(capsys): analyze.analyze_faces( 'gs://{}{}'.format(BUCKET, FACES_FILE_PATH)) out, _ = capsys.readouterr() @@ -45,16 +43,16 @@ def test_work_video_faces(capsys): @pytest.mark.slow -def test_dino_video_labels(capsys): +def test_analyze_labels(capsys): analyze.analyze_labels( 'gs://{}{}'.format(BUCKET, LABELS_FILE_PATH)) out, _ = capsys.readouterr() - assert 'Whiskers' in out + assert 'label description: cat' in out @pytest.mark.slow -def test_cat_safe_search(capsys): - analyze.analyze_safe_search( - 'gs://{}{}'.format(BUCKET, SAFE_SEARCH_FILE_PATH)) +def test_analyze_explicit_content(capsys): + analyze.analyze_explicit_content( + 'gs://{}{}'.format(BUCKET, EXPLICIT_CONTENT_FILE_PATH)) out, _ = capsys.readouterr() - assert 'medical' in out + assert 'pornography' in out diff --git a/video/cloud-client/faces/faces.py b/video/cloud-client/faces/faces.py index b6221fa2e6fb..3bca1510f9f4 100644 --- a/video/cloud-client/faces/faces.py +++ b/video/cloud-client/faces/faces.py @@ -32,17 +32,15 @@ import sys import time -from google.cloud.gapic.videointelligence.v1beta1 import enums -from google.cloud.gapic.videointelligence.v1beta1 import ( - video_intelligence_service_client) +from google.cloud import videointelligence_v1beta2 +from google.cloud.videointelligence_v1beta2 import enums # [END imports] def analyze_faces(path): # [START construct_request] """ Detects faces given a GCS path. """ - video_client = (video_intelligence_service_client. - VideoIntelligenceServiceClient()) + video_client = videointelligence_v1beta2.VideoIntelligenceServiceClient() features = [enums.Feature.FACE_DETECTION] operation = video_client.annotate_video(path, features) # [END construct_request] @@ -66,10 +64,12 @@ def analyze_faces(path): print('Thumbnail size: {}'.format(len(face.thumbnail))) for segment_id, segment in enumerate(face.segments): - print('Track {}: {} to {}'.format( - segment_id, - segment.start_time_offset, - segment.end_time_offset)) + start_time = (segment.segment.start_time_offset.seconds + + segment.segment.start_time_offset.nanos / 1e9) + end_time = (segment.segment.end_time_offset.seconds + + segment.segment.end_time_offset.nanos / 1e9) + positions = '{}s to {}s'.format(start_time, end_time) + print('\tSegment {}: {}'.format(segment_id, positions)) # [END parse_response] diff --git a/video/cloud-client/faces/faces_test.py b/video/cloud-client/faces/faces_test.py index 5eb4075dedb3..9ca80920fa80 100644 --- a/video/cloud-client/faces/faces_test.py +++ b/video/cloud-client/faces/faces_test.py @@ -15,10 +15,9 @@ # limitations under the License. import os - +import faces import pytest -import faces BUCKET = os.environ['CLOUD_STORAGE_BUCKET'] FACES_FILE_PATH = '/video/googlework.mp4' diff --git a/video/cloud-client/labels/labels.py b/video/cloud-client/labels/labels.py index 7e0f9a0e2d42..5f45b8313747 100644 --- a/video/cloud-client/labels/labels.py +++ b/video/cloud-client/labels/labels.py @@ -32,17 +32,15 @@ import sys import time -from google.cloud.gapic.videointelligence.v1beta1 import enums -from google.cloud.gapic.videointelligence.v1beta1 import ( - video_intelligence_service_client) +from google.cloud import videointelligence_v1beta2 +from google.cloud.videointelligence_v1beta2 import enums # [END imports] def analyze_labels(path): """ Detects labels given a GCS path. """ # [START construct_request] - video_client = (video_intelligence_service_client. - VideoIntelligenceServiceClient()) + video_client = videointelligence_v1beta2.VideoIntelligenceServiceClient() features = [enums.Feature.LABEL_DETECTION] operation = video_client.annotate_video(path, features) # [END construct_request] @@ -60,15 +58,23 @@ def analyze_labels(path): # [START parse_response] results = operation.result().annotation_results[0] - for label in results.label_annotations: - print('Label description: {}'.format(label.description)) - print('Locations:') - - for l, location in enumerate(label.locations): - print('\t{}: {} to {}'.format( - l, - location.segment.start_time_offset, - location.segment.end_time_offset)) + for i, segment_label in enumerate(results.segment_label_annotations): + print('Video label description: {}'.format( + segment_label.entity.description)) + for category_entity in segment_label.category_entities: + print('\tLabel category description: {}'.format( + category_entity.description)) + + for i, segment in enumerate(segment_label.segments): + start_time = (segment.segment.start_time_offset.seconds + + segment.segment.start_time_offset.nanos / 1e9) + end_time = (segment.segment.end_time_offset.seconds + + segment.segment.end_time_offset.nanos / 1e9) + positions = '{}s to {}s'.format(start_time, end_time) + confidence = segment.confidence + print('\tSegment {}: {}'.format(i, positions)) + print('\tConfidence: {}'.format(confidence)) + print('\n') # [END parse_response] diff --git a/video/cloud-client/labels/labels_test.py b/video/cloud-client/labels/labels_test.py index cd571b087f1e..31a68358f6fa 100644 --- a/video/cloud-client/labels/labels_test.py +++ b/video/cloud-client/labels/labels_test.py @@ -15,10 +15,9 @@ # limitations under the License. import os - +import labels import pytest -import labels BUCKET = os.environ['CLOUD_STORAGE_BUCKET'] LABELS_FILE_PATH = '/video/cat.mp4' @@ -29,4 +28,4 @@ def test_feline_video_labels(capsys): labels.analyze_labels( 'gs://{}{}'.format(BUCKET, LABELS_FILE_PATH)) out, _ = capsys.readouterr() - assert 'Whiskers' in out + assert 'Video label description: cat' in out diff --git a/video/cloud-client/quickstart/quickstart.py b/video/cloud-client/quickstart/quickstart.py index bfb5bca91161..1f31d46657a9 100644 --- a/video/cloud-client/quickstart/quickstart.py +++ b/video/cloud-client/quickstart/quickstart.py @@ -26,12 +26,10 @@ def run_quickstart(): import sys import time - from google.cloud.gapic.videointelligence.v1beta1 import enums - from google.cloud.gapic.videointelligence.v1beta1 import ( - video_intelligence_service_client) + from google.cloud import videointelligence_v1beta2 + from google.cloud.videointelligence_v1beta2 import enums - video_client = (video_intelligence_service_client. - VideoIntelligenceServiceClient()) + video_client = videointelligence_v1beta2.VideoIntelligenceServiceClient() features = [enums.Feature.LABEL_DETECTION] operation = video_client.annotate_video('gs://demomaker/cat.mp4', features) print('\nProcessing video for label annotations:') @@ -46,19 +44,22 @@ def run_quickstart(): # first result is retrieved because a single video was processed results = operation.result().annotation_results[0] - for label in results.label_annotations: - print('Label description: {}'.format(label.description)) - print('Locations:') - - for l, location in enumerate(label.locations): - positions = 'Entire video' - if (location.segment.start_time_offset != -1 or - location.segment.end_time_offset != -1): - positions = '{} to {}'.format( - location.segment.start_time_offset / 1000000.0, - location.segment.end_time_offset / 1000000.0) - print('\t{}: {}'.format(l, positions)) + for i, segment_label in enumerate(results.segment_label_annotations): + print('Video label description: {}'.format( + segment_label.entity.description)) + for category_entity in segment_label.category_entities: + print('\tLabel category description: {}'.format( + category_entity.description)) + for i, segment in enumerate(segment_label.segments): + start_time = (segment.segment.start_time_offset.seconds + + segment.segment.start_time_offset.nanos / 1e9) + end_time = (segment.segment.end_time_offset.seconds + + segment.segment.end_time_offset.nanos / 1e9) + positions = '{}s to {}s'.format(start_time, end_time) + confidence = segment.confidence + print('\tSegment {}: {}'.format(i, positions)) + print('\tConfidence: {}'.format(confidence)) print('\n') # [END videointelligence_quickstart] diff --git a/video/cloud-client/quickstart/quickstart_test.py b/video/cloud-client/quickstart/quickstart_test.py index 9712f4107621..1d1534c46cfe 100644 --- a/video/cloud-client/quickstart/quickstart_test.py +++ b/video/cloud-client/quickstart/quickstart_test.py @@ -23,4 +23,4 @@ def test_quickstart(capsys): quickstart.run_quickstart() out, _ = capsys.readouterr() - assert 'Whiskers' in out + assert 'Video label description: cat' in out diff --git a/video/cloud-client/shotchange/shotchange.py b/video/cloud-client/shotchange/shotchange.py index 418b89fe2800..4db4ca3bc0ac 100644 --- a/video/cloud-client/shotchange/shotchange.py +++ b/video/cloud-client/shotchange/shotchange.py @@ -32,17 +32,15 @@ import sys import time -from google.cloud.gapic.videointelligence.v1beta1 import enums -from google.cloud.gapic.videointelligence.v1beta1 import ( - video_intelligence_service_client) +from google.cloud import videointelligence_v1beta2 +from google.cloud.videointelligence_v1beta2 import enums # [END imports] def analyze_shots(path): """ Detects camera shot changes. """ # [START construct_request] - video_client = (video_intelligence_service_client. - VideoIntelligenceServiceClient()) + video_client = videointelligence_v1beta2.VideoIntelligenceServiceClient() features = [enums.Feature.SHOT_CHANGE_DETECTION] operation = video_client.annotate_video(path, features) # [END construct_request] @@ -58,13 +56,14 @@ def analyze_shots(path): # [END check_operation] # [START parse_response] - shots = operation.result().annotation_results[0] - - for note, shot in enumerate(shots.shot_annotations): - print('Scene {}: {} to {}'.format( - note, - shot.start_time_offset, - shot.end_time_offset)) + shots = operation.result().annotation_results[0].shot_annotations + + for i, shot in enumerate(shots): + start_time = (shot.start_time_offset.seconds + + shot.start_time_offset.nanos / 1e9) + end_time = (shot.end_time_offset.seconds + + shot.end_time_offset.nanos / 1e9) + print('\tShot {}: {} to {}'.format(i, start_time, end_time)) # [END parse_response] diff --git a/video/cloud-client/shotchange/shotchange_test.py b/video/cloud-client/shotchange/shotchange_test.py index 2c637036fcfa..a004f56d5cd0 100644 --- a/video/cloud-client/shotchange/shotchange_test.py +++ b/video/cloud-client/shotchange/shotchange_test.py @@ -29,4 +29,4 @@ def test_shots_dino(capsys): shotchange.analyze_shots( 'gs://{}{}'.format(BUCKET, SHOTS_FILE_PATH)) out, _ = capsys.readouterr() - assert 'Scene 1:' in out + assert 'Shot 1:' in out