Skip to content

Commit fc15397

Browse files
nnegreynirupa-kumar
authored andcommitted
Add beta samples for Video Streaming (GoogleCloudPlatform#1353)
* Add beta samples for Video Streaming * Update pom.xml * Update pom.xml
1 parent 9cec18a commit fc15397

11 files changed

+837
-1
lines changed

video/beta/pom.xml

+8-1
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,18 @@
3636
</properties>
3737

3838
<dependencies>
39+
<!-- [START video_java_dependencies_beta] -->
3940
<dependency>
4041
<groupId>com.google.cloud</groupId>
4142
<artifactId>google-cloud-video-intelligence</artifactId>
42-
<version>0.80.0-beta</version>
43+
<version>0.84.0-beta</version>
4344
</dependency>
45+
<dependency>
46+
<groupId>com.google.cloud</groupId>
47+
<artifactId>google-cloud-storage</artifactId>
48+
<version>1.65.0</version>
49+
</dependency>
50+
<!-- [END video_java_dependencies_beta] -->
4451

4552
<!-- Test dependencies -->
4653
<dependency>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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_annotation_to_storage_beta]
20+
import com.google.api.gax.rpc.BidiStream;
21+
import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoRequest;
22+
import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoResponse;
23+
import com.google.cloud.videointelligence.v1p3beta1.StreamingFeature;
24+
import com.google.cloud.videointelligence.v1p3beta1.StreamingLabelDetectionConfig;
25+
import com.google.cloud.videointelligence.v1p3beta1.StreamingStorageConfig;
26+
import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoConfig;
27+
import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoIntelligenceServiceClient;
28+
import com.google.protobuf.ByteString;
29+
import java.io.IOException;
30+
import java.nio.file.Files;
31+
import java.nio.file.Path;
32+
import java.nio.file.Paths;
33+
import java.util.Arrays;
34+
35+
public class StreamingAnnotationToStorage {
36+
37+
// Perform streaming video detection for explicit content
38+
static void streamingAnnotationToStorage(String filePath, String gcsUri) {
39+
// String filePath = "path_to_your_video_file";
40+
// String gcsUri = "gs://BUCKET_ID";
41+
42+
try (StreamingVideoIntelligenceServiceClient client =
43+
StreamingVideoIntelligenceServiceClient.create()) {
44+
45+
Path path = Paths.get(filePath);
46+
byte[] data = Files.readAllBytes(path);
47+
// Set the chunk size to 5MB (recommended less than 10MB).
48+
int chunkSize = 5 * 1024 * 1024;
49+
int numChunks = (int) Math.ceil((double) data.length / chunkSize);
50+
51+
StreamingStorageConfig streamingStorageConfig = StreamingStorageConfig.newBuilder()
52+
.setEnableStorageAnnotationResult(true)
53+
.setAnnotationResultStorageDirectory(gcsUri)
54+
.build();
55+
56+
StreamingLabelDetectionConfig labelConfig = StreamingLabelDetectionConfig.newBuilder()
57+
.setStationaryCamera(false)
58+
.build();
59+
60+
StreamingVideoConfig streamingVideoConfig = StreamingVideoConfig.newBuilder()
61+
.setFeature(StreamingFeature.STREAMING_LABEL_DETECTION)
62+
.setLabelDetectionConfig(labelConfig)
63+
.setStorageConfig(streamingStorageConfig)
64+
.build();
65+
66+
BidiStream<StreamingAnnotateVideoRequest, StreamingAnnotateVideoResponse> call =
67+
client.streamingAnnotateVideoCallable().call();
68+
69+
// The first request must **only** contain the audio configuration:
70+
call.send(
71+
StreamingAnnotateVideoRequest.newBuilder()
72+
.setVideoConfig(streamingVideoConfig)
73+
.build());
74+
75+
// Subsequent requests must **only** contain the audio data.
76+
// Send the requests in chunks
77+
for (int i = 0; i < numChunks; i++) {
78+
call.send(
79+
StreamingAnnotateVideoRequest.newBuilder()
80+
.setInputContent(ByteString.copyFrom(
81+
Arrays.copyOfRange(data, i * chunkSize, i * chunkSize + chunkSize)))
82+
.build());
83+
}
84+
85+
// Tell the service you are done sending data
86+
call.closeSend();
87+
88+
for (StreamingAnnotateVideoResponse response : call) {
89+
System.out.format("Storage Uri: %s\n", response.getAnnotationResultsUri());
90+
}
91+
} catch (IOException e) {
92+
e.printStackTrace();
93+
}
94+
}
95+
}
96+
// [END video_streaming_annotation_to_storage_beta]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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_explicit_content_detection_beta]
20+
import com.google.api.gax.rpc.BidiStream;
21+
import com.google.cloud.videointelligence.v1p3beta1.ExplicitContentFrame;
22+
import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoRequest;
23+
import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoResponse;
24+
import com.google.cloud.videointelligence.v1p3beta1.StreamingFeature;
25+
import com.google.cloud.videointelligence.v1p3beta1.StreamingLabelDetectionConfig;
26+
import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoAnnotationResults;
27+
import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoConfig;
28+
import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoIntelligenceServiceClient;
29+
import com.google.protobuf.ByteString;
30+
import java.io.IOException;
31+
import java.nio.file.Files;
32+
import java.nio.file.Path;
33+
import java.nio.file.Paths;
34+
import java.util.Arrays;
35+
36+
class StreamingExplicitContentDetection {
37+
38+
// Perform streaming video detection for explicit content
39+
static void streamingExplicitContentDetection(String filePath) {
40+
// String filePath = "path_to_your_video_file";
41+
42+
try (StreamingVideoIntelligenceServiceClient client =
43+
StreamingVideoIntelligenceServiceClient.create()) {
44+
45+
Path path = Paths.get(filePath);
46+
byte[] data = Files.readAllBytes(path);
47+
// Set the chunk size to 5MB (recommended less than 10MB).
48+
int chunkSize = 5 * 1024 * 1024;
49+
int numChunks = (int) Math.ceil((double) data.length / chunkSize);
50+
51+
StreamingLabelDetectionConfig labelConfig = StreamingLabelDetectionConfig.newBuilder()
52+
.setStationaryCamera(false)
53+
.build();
54+
55+
StreamingVideoConfig streamingVideoConfig = StreamingVideoConfig.newBuilder()
56+
.setFeature(StreamingFeature.STREAMING_EXPLICIT_CONTENT_DETECTION)
57+
.setLabelDetectionConfig(labelConfig)
58+
.build();
59+
60+
BidiStream<StreamingAnnotateVideoRequest, StreamingAnnotateVideoResponse> call =
61+
client.streamingAnnotateVideoCallable().call();
62+
63+
// The first request must **only** contain the audio configuration:
64+
call.send(
65+
StreamingAnnotateVideoRequest.newBuilder()
66+
.setVideoConfig(streamingVideoConfig)
67+
.build());
68+
69+
// Subsequent requests must **only** contain the audio data.
70+
// Send the requests in chunks
71+
for (int i = 0; i < numChunks; i++) {
72+
call.send(
73+
StreamingAnnotateVideoRequest.newBuilder()
74+
.setInputContent(ByteString.copyFrom(
75+
Arrays.copyOfRange(data, i * chunkSize, i * chunkSize + chunkSize)))
76+
.build());
77+
}
78+
79+
// Tell the service you are done sending data
80+
call.closeSend();
81+
82+
for (StreamingAnnotateVideoResponse response : call) {
83+
StreamingVideoAnnotationResults annotationResults = response.getAnnotationResults();
84+
85+
for (ExplicitContentFrame frame :
86+
annotationResults.getExplicitAnnotation().getFramesList()) {
87+
88+
double offset = frame.getTimeOffset().getSeconds()
89+
+ frame.getTimeOffset().getNanos() / 1e9;
90+
91+
System.out.format("Offset: %f\n", offset);
92+
System.out.format("\tPornography: %s", frame.getPornographyLikelihood());
93+
}
94+
}
95+
} catch (IOException e) {
96+
e.printStackTrace();
97+
}
98+
}
99+
}
100+
// [END video_streaming_explicit_content_detection_beta]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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

Comments
 (0)