Skip to content

Commit 03840af

Browse files
[video] Streaming AutoML Classification (#2313)
* Video Intelligence Beta - Streaming/Live Streaming support for AutoML custom models * add test skeleton * skeleton * more skeleton code * update sample: update video codec/test/model_id/etc. * lint * mask project id * Noah's and Rebecca's suggestions
1 parent 7270468 commit 03840af

File tree

4 files changed

+100
-3
lines changed

4 files changed

+100
-3
lines changed

video/cloud-client/analyze/beta_snippets.py

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
22

3-
# Copyright 2017 Google Inc. All Rights Reserved.
3+
# Copyright 2019 Google LLC. All Rights Reserved.
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
@@ -36,6 +36,9 @@
3636
3737
python beta_snippets.py streaming-annotation-storage resources/cat.mp4 \
3838
gs://mybucket/myfolder
39+
40+
python beta_snippets.py streaming-automl-classification resources/cat.mp4 \
41+
$PROJECT_ID $MODEL_ID
3942
"""
4043

4144
import argparse
@@ -629,6 +632,79 @@ def stream_generator():
629632
# [END video_streaming_annotation_to_storage_beta]
630633

631634

635+
def streaming_automl_classification(path, project_id, model_id):
636+
# [START video_streaming_automl_classification_beta]
637+
import io
638+
639+
from google.cloud import videointelligence_v1p3beta1 as videointelligence
640+
from google.cloud.videointelligence_v1p3beta1 import enums
641+
642+
# path = 'path_to_file'
643+
# project_id = 'gcp_project_id'
644+
# model_id = 'automl_classification_model_id'
645+
646+
client = videointelligence.StreamingVideoIntelligenceServiceClient()
647+
648+
model_path = 'projects/{}/locations/us-central1/models/{}'.format(
649+
project_id, model_id)
650+
651+
# Here we use classification as an example.
652+
automl_config = (videointelligence.types
653+
.StreamingAutomlClassificationConfig(
654+
model_name=model_path))
655+
656+
video_config = videointelligence.types.StreamingVideoConfig(
657+
feature=enums.StreamingFeature.STREAMING_AUTOML_CLASSIFICATION,
658+
automl_classification_config=automl_config)
659+
660+
# config_request should be the first in the stream of requests.
661+
config_request = videointelligence.types.StreamingAnnotateVideoRequest(
662+
video_config=video_config)
663+
664+
# Set the chunk size to 5MB (recommended less than 10MB).
665+
chunk_size = 5 * 1024 * 1024
666+
667+
# Load file content.
668+
# Note: Input videos must have supported video codecs. See
669+
# https://cloud.google.com/video-intelligence/docs/streaming/streaming#supported_video_codecs
670+
# for more details.
671+
stream = []
672+
with io.open(path, 'rb') as video_file:
673+
while True:
674+
data = video_file.read(chunk_size)
675+
if not data:
676+
break
677+
stream.append(data)
678+
679+
def stream_generator():
680+
yield config_request
681+
for chunk in stream:
682+
yield videointelligence.types.StreamingAnnotateVideoRequest(
683+
input_content=chunk)
684+
685+
requests = stream_generator()
686+
687+
# streaming_annotate_video returns a generator.
688+
# The default timeout is about 300 seconds.
689+
# To process longer videos it should be set to
690+
# larger than the length (in seconds) of the stream.
691+
responses = client.streaming_annotate_video(requests, timeout=600)
692+
693+
for response in responses:
694+
# Check for errors.
695+
if response.error.message:
696+
print(response.error.message)
697+
break
698+
699+
for label in response.annotation_results.label_annotations:
700+
for frame in label.frames:
701+
print("At {:3d}s segment, {:5.1%} {}".format(
702+
frame.time_offset.seconds,
703+
frame.confidence,
704+
label.entity.entity_id))
705+
# [END video_streaming_automl_classification_beta]
706+
707+
632708
if __name__ == '__main__':
633709
parser = argparse.ArgumentParser(
634710
description=__doc__,
@@ -678,6 +754,13 @@ def stream_generator():
678754
video_streaming_annotation_to_storage_parser.add_argument('path')
679755
video_streaming_annotation_to_storage_parser.add_argument('output_uri')
680756

757+
video_streaming_automl_classification_parser = subparsers.add_parser(
758+
'streaming-automl-classification',
759+
help=streaming_automl_classification.__doc__)
760+
video_streaming_automl_classification_parser.add_argument('path')
761+
video_streaming_automl_classification_parser.add_argument('project_id')
762+
video_streaming_automl_classification_parser.add_argument('model_id')
763+
681764
args = parser.parse_args()
682765

683766
if args.command == 'transcription':
@@ -700,3 +783,6 @@ def stream_generator():
700783
detect_explicit_content_streaming(args.path)
701784
elif args.command == 'streaming-annotation-storage':
702785
annotation_to_storage_streaming(args.path, args.output_uri)
786+
elif args.command == 'streaming-automl-classification':
787+
streaming_automl_classification(
788+
args.path, args.project_id, args.model_id)

video/cloud-client/analyze/beta_snippets_test.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
22

3-
# Copyright 2017 Google, Inc
3+
# Copyright 2019 Google, LLC
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
from six.moves.urllib.request import urlopen
1818
import time
19+
import os
1920
import uuid
2021

2122
import beta_snippets
@@ -160,3 +161,13 @@ def test_track_objects_gcs():
160161
assert text_exists
161162
assert object_annotations[0].frames[0].normalized_bounding_box.left >= 0.0
162163
assert object_annotations[0].frames[0].normalized_bounding_box.left <= 1.0
164+
165+
166+
@pytest.mark.slow
167+
def test_streaming_automl_classification(capsys, in_file):
168+
project_id = os.environ['GCLOUD_PROJECT']
169+
model_id = 'VCN6363999689846554624'
170+
beta_snippets.streaming_automl_classification(
171+
in_file, project_id, model_id)
172+
out, _ = capsys.readouterr()
173+
assert 'brush_hair' in out
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.11.0
22
google-cloud-storage==1.14.0
Binary file not shown.

0 commit comments

Comments
 (0)