Skip to content

Commit d778c08

Browse files
authored
Feat: Add code sample for video streaming action recognition. (#667)
1 parent c197b28 commit d778c08

File tree

2 files changed

+194
-0
lines changed

2 files changed

+194
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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 beta.video;
18+
19+
// [START video_streaming_automl_action_recognition_beta]
20+
21+
import com.google.api.gax.rpc.BidiStream;
22+
import com.google.cloud.videointelligence.v1p3beta1.LabelAnnotation;
23+
import com.google.cloud.videointelligence.v1p3beta1.LabelFrame;
24+
import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoRequest;
25+
import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoResponse;
26+
import com.google.cloud.videointelligence.v1p3beta1.StreamingAutomlActionRecognitionConfig;
27+
import com.google.cloud.videointelligence.v1p3beta1.StreamingFeature;
28+
import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoAnnotationResults;
29+
import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoConfig;
30+
import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoIntelligenceServiceClient;
31+
import com.google.protobuf.ByteString;
32+
import io.grpc.StatusRuntimeException;
33+
import java.io.IOException;
34+
import java.nio.file.Files;
35+
import java.nio.file.Path;
36+
import java.nio.file.Paths;
37+
import java.util.Arrays;
38+
import java.util.concurrent.TimeoutException;
39+
40+
class StreamingAutoMlActionRecognition {
41+
42+
// Perform streaming video action recognition
43+
static void streamingAutoMlActionRecognition(String filePath, String projectId, String modelId)
44+
throws IOException, TimeoutException, StatusRuntimeException {
45+
46+
try (StreamingVideoIntelligenceServiceClient client =
47+
StreamingVideoIntelligenceServiceClient.create()) {
48+
49+
Path path = Paths.get(filePath);
50+
byte[] data = Files.readAllBytes(path);
51+
// Set the chunk size to 5MB (recommended less than 10MB).
52+
int chunkSize = 5 * 1024 * 1024;
53+
int numChunks = (int) Math.ceil((double) data.length / chunkSize);
54+
55+
String modelPath =
56+
String.format("projects/%s/locations/us-central1/models/%s", projectId, modelId);
57+
58+
System.out.println(modelPath);
59+
60+
StreamingAutomlActionRecognitionConfig streamingAutomlActionRecognitionConfig =
61+
StreamingAutomlActionRecognitionConfig.newBuilder().setModelName(modelPath).build();
62+
63+
StreamingVideoConfig streamingVideoConfig =
64+
StreamingVideoConfig.newBuilder()
65+
.setFeature(StreamingFeature.STREAMING_AUTOML_ACTION_RECOGNITION)
66+
.setAutomlActionRecognitionConfig(streamingAutomlActionRecognitionConfig)
67+
.build();
68+
69+
BidiStream<StreamingAnnotateVideoRequest, StreamingAnnotateVideoResponse> call =
70+
client.streamingAnnotateVideoCallable().call();
71+
72+
// The first request must **only** contain the video configuration:
73+
call.send(
74+
StreamingAnnotateVideoRequest.newBuilder().setVideoConfig(streamingVideoConfig).build());
75+
76+
// Subsequent requests must **only** contain the video data.
77+
// Send the requests in chunks
78+
for (int i = 0; i < numChunks; i++) {
79+
call.send(
80+
StreamingAnnotateVideoRequest.newBuilder()
81+
.setInputContent(
82+
ByteString.copyFrom(
83+
Arrays.copyOfRange(data, i * chunkSize, i * chunkSize + chunkSize)))
84+
.build());
85+
}
86+
87+
// Tell the service you are done sending data
88+
call.closeSend();
89+
90+
for (StreamingAnnotateVideoResponse response : call) {
91+
if (response.hasError()) {
92+
System.out.println(response.getError().getMessage());
93+
break;
94+
}
95+
96+
StreamingVideoAnnotationResults annotationResults = response.getAnnotationResults();
97+
98+
for (LabelAnnotation annotation : annotationResults.getLabelAnnotationsList()) {
99+
String entity = annotation.getEntity().getDescription();
100+
101+
// There is only one frame per annotation
102+
LabelFrame labelFrame = annotation.getFrames(0);
103+
double offset =
104+
labelFrame.getTimeOffset().getSeconds() + labelFrame.getTimeOffset().getNanos() / 1e9;
105+
float confidence = labelFrame.getConfidence();
106+
107+
System.out.format("At %fs segment: %s (%f)\n", offset, entity, confidence);
108+
}
109+
}
110+
System.out.println("Video streamed successfully.");
111+
}
112+
}
113+
}
114+
// [END video_streaming_automl_action_recognition_beta]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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 beta.video;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import io.grpc.Status;
22+
import io.grpc.StatusRuntimeException;
23+
import java.io.ByteArrayOutputStream;
24+
import java.io.PrintStream;
25+
import org.junit.After;
26+
import org.junit.Before;
27+
import org.junit.Test;
28+
import org.junit.runner.RunWith;
29+
import org.junit.runners.JUnit4;
30+
31+
/** Integration (system) tests for {@link StreamingAutoMlActionRecognition}. */
32+
@RunWith(JUnit4.class)
33+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
34+
public class StreamingAutoMlActionRecognitionIT {
35+
36+
private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT");
37+
private static String MODEL_ID = "2787930479481847808";
38+
39+
private ByteArrayOutputStream bout;
40+
private PrintStream out;
41+
private PrintStream originalPrintStream;
42+
43+
@Before
44+
public void setUp() {
45+
bout = new ByteArrayOutputStream();
46+
out = new PrintStream(bout);
47+
originalPrintStream = System.out;
48+
System.setOut(out);
49+
}
50+
51+
@After
52+
public void tearDown() {
53+
// restores print statements in the original method
54+
System.out.flush();
55+
System.setOut(originalPrintStream);
56+
}
57+
58+
@Test
59+
public void testStreamingAutoMlActionRecognition() {
60+
// Bad Gateway sporadically occurs
61+
int tryCount = 0;
62+
int maxTries = 3;
63+
while (tryCount < maxTries) {
64+
try {
65+
StreamingAutoMlActionRecognition.streamingAutoMlActionRecognition(
66+
"resources/cat.mp4", PROJECT_ID, MODEL_ID);
67+
assertThat(bout.toString()).contains("Video streamed successfully.");
68+
69+
break;
70+
} catch (StatusRuntimeException ex) {
71+
if (ex.getStatus().getCode() == Status.Code.UNAVAILABLE) {
72+
assertThat(ex.getMessage()).contains("Bad Gateway");
73+
tryCount++;
74+
}
75+
} catch (Exception e) {
76+
e.printStackTrace();
77+
}
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)