-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
StreamingAutoMlActionRecognition.java
114 lines (94 loc) · 4.65 KB
/
StreamingAutoMlActionRecognition.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package beta.video;
// [START video_streaming_automl_action_recognition_beta]
import com.google.api.gax.rpc.BidiStream;
import com.google.cloud.videointelligence.v1p3beta1.LabelAnnotation;
import com.google.cloud.videointelligence.v1p3beta1.LabelFrame;
import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoRequest;
import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoResponse;
import com.google.cloud.videointelligence.v1p3beta1.StreamingAutomlActionRecognitionConfig;
import com.google.cloud.videointelligence.v1p3beta1.StreamingFeature;
import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoAnnotationResults;
import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoConfig;
import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoIntelligenceServiceClient;
import com.google.protobuf.ByteString;
import io.grpc.StatusRuntimeException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.concurrent.TimeoutException;
class StreamingAutoMlActionRecognition {
// Perform streaming video action recognition
static void streamingAutoMlActionRecognition(String filePath, String projectId, String modelId)
throws IOException, TimeoutException, StatusRuntimeException {
try (StreamingVideoIntelligenceServiceClient client =
StreamingVideoIntelligenceServiceClient.create()) {
Path path = Paths.get(filePath);
byte[] data = Files.readAllBytes(path);
// Set the chunk size to 5MB (recommended less than 10MB).
int chunkSize = 5 * 1024 * 1024;
int numChunks = (int) Math.ceil((double) data.length / chunkSize);
String modelPath =
String.format("projects/%s/locations/us-central1/models/%s", projectId, modelId);
System.out.println(modelPath);
StreamingAutomlActionRecognitionConfig streamingAutomlActionRecognitionConfig =
StreamingAutomlActionRecognitionConfig.newBuilder().setModelName(modelPath).build();
StreamingVideoConfig streamingVideoConfig =
StreamingVideoConfig.newBuilder()
.setFeature(StreamingFeature.STREAMING_AUTOML_ACTION_RECOGNITION)
.setAutomlActionRecognitionConfig(streamingAutomlActionRecognitionConfig)
.build();
BidiStream<StreamingAnnotateVideoRequest, StreamingAnnotateVideoResponse> call =
client.streamingAnnotateVideoCallable().call();
// The first request must **only** contain the video configuration:
call.send(
StreamingAnnotateVideoRequest.newBuilder().setVideoConfig(streamingVideoConfig).build());
// Subsequent requests must **only** contain the video data.
// Send the requests in chunks
for (int i = 0; i < numChunks; i++) {
call.send(
StreamingAnnotateVideoRequest.newBuilder()
.setInputContent(
ByteString.copyFrom(
Arrays.copyOfRange(data, i * chunkSize, i * chunkSize + chunkSize)))
.build());
}
// Tell the service you are done sending data
call.closeSend();
for (StreamingAnnotateVideoResponse response : call) {
if (response.hasError()) {
System.out.println(response.getError().getMessage());
break;
}
StreamingVideoAnnotationResults annotationResults = response.getAnnotationResults();
for (LabelAnnotation annotation : annotationResults.getLabelAnnotationsList()) {
String entity = annotation.getEntity().getDescription();
// There is only one frame per annotation
LabelFrame labelFrame = annotation.getFrames(0);
double offset =
labelFrame.getTimeOffset().getSeconds() + labelFrame.getTimeOffset().getNanos() / 1e9;
float confidence = labelFrame.getConfidence();
System.out.format("At %fs segment: %s (%f)\n", offset, entity, confidence);
}
}
System.out.println("Video streamed successfully.");
}
}
}
// [END video_streaming_automl_action_recognition_beta]