1+ /*
2+ * Copyright 2019 Google LLC
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ package com .example .video ;
18+
19+ // [START video_streaming_label_detection_beta]
20+ import com .google .api .gax .rpc .BidiStream ;
21+ import com .google .cloud .videointelligence .v1p3beta1 .LabelAnnotation ;
22+ import com .google .cloud .videointelligence .v1p3beta1 .LabelFrame ;
23+ import com .google .cloud .videointelligence .v1p3beta1 .StreamingAnnotateVideoRequest ;
24+ import com .google .cloud .videointelligence .v1p3beta1 .StreamingAnnotateVideoResponse ;
25+ import com .google .cloud .videointelligence .v1p3beta1 .StreamingFeature ;
26+ import com .google .cloud .videointelligence .v1p3beta1 .StreamingLabelDetectionConfig ;
27+ import com .google .cloud .videointelligence .v1p3beta1 .StreamingVideoAnnotationResults ;
28+ import com .google .cloud .videointelligence .v1p3beta1 .StreamingVideoConfig ;
29+ import com .google .cloud .videointelligence .v1p3beta1 .StreamingVideoIntelligenceServiceClient ;
30+ import com .google .protobuf .ByteString ;
31+ import java .io .IOException ;
32+ import java .nio .file .Files ;
33+ import java .nio .file .Path ;
34+ import java .nio .file .Paths ;
35+ import java .util .Arrays ;
36+
37+ class StreamingLabelDetection {
38+
39+ // Perform streaming video label detection
40+ static void streamingLabelDetection (String filePath ) {
41+ // String filePath = "path_to_your_video_file";
42+
43+ try (StreamingVideoIntelligenceServiceClient client =
44+ StreamingVideoIntelligenceServiceClient .create ()) {
45+
46+ Path path = Paths .get (filePath );
47+ byte [] data = Files .readAllBytes (path );
48+ // Set the chunk size to 5MB (recommended less than 10MB).
49+ int chunkSize = 5 * 1024 * 1024 ;
50+ int numChunks = (int ) Math .ceil ((double ) data .length / chunkSize );
51+
52+ StreamingLabelDetectionConfig labelConfig = StreamingLabelDetectionConfig .newBuilder ()
53+ .setStationaryCamera (false )
54+ .build ();
55+
56+ StreamingVideoConfig streamingVideoConfig = StreamingVideoConfig .newBuilder ()
57+ .setFeature (StreamingFeature .STREAMING_LABEL_DETECTION )
58+ .setLabelDetectionConfig (labelConfig )
59+ .build ();
60+
61+ BidiStream <StreamingAnnotateVideoRequest , StreamingAnnotateVideoResponse > call =
62+ client .streamingAnnotateVideoCallable ().call ();
63+
64+ // The first request must **only** contain the audio configuration:
65+ call .send (
66+ StreamingAnnotateVideoRequest .newBuilder ()
67+ .setVideoConfig (streamingVideoConfig )
68+ .build ());
69+
70+ // Subsequent requests must **only** contain the audio data.
71+ // Send the requests in chunks
72+ for (int i = 0 ; i < numChunks ; i ++) {
73+ call .send (
74+ StreamingAnnotateVideoRequest .newBuilder ()
75+ .setInputContent (ByteString .copyFrom (
76+ Arrays .copyOfRange (data , i * chunkSize , i * chunkSize + chunkSize )))
77+ .build ());
78+ }
79+
80+ // Tell the service you are done sending data
81+ call .closeSend ();
82+
83+ for (StreamingAnnotateVideoResponse response : call ) {
84+ StreamingVideoAnnotationResults annotationResults = response .getAnnotationResults ();
85+
86+ for (LabelAnnotation annotation : annotationResults .getLabelAnnotationsList ()) {
87+ String entity = annotation .getEntity ().getDescription ();
88+
89+ // There is only one frame per annotation
90+ LabelFrame labelFrame = annotation .getFrames (0 );
91+ double offset = labelFrame .getTimeOffset ().getSeconds ()
92+ + labelFrame .getTimeOffset ().getNanos () / 1e9 ;
93+ float confidence = labelFrame .getConfidence ();
94+
95+ System .out .format ("%fs: %s (%f)\n " , offset , entity , confidence );
96+ }
97+ }
98+ } catch (IOException e ) {
99+ e .printStackTrace ();
100+ }
101+ }
102+ }
103+ // [END video_streaming_label_detection_beta]
0 commit comments