Skip to content
This repository was archived by the owner on Sep 16, 2023. It is now read-only.

Commit 7d87ef4

Browse files
nnegreychingor13
authored andcommitted
samples: Add new beta samples for Video Intelligence Streaming with an AutoML … (#1568)
* Add new beta samples for Video Intelligence Streaming with an AutoML Model * Update StreamingAutoMlClassification.java * Update Track Objects timeout for Java8 * Same timeout increase for Java8
1 parent 0f57e2d commit 7d87ef4

File tree

4 files changed

+187
-3
lines changed

4 files changed

+187
-3
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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_automl_classification_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.StreamingAutomlClassificationConfig;
26+
import com.google.cloud.videointelligence.v1p3beta1.StreamingFeature;
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+
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 StreamingAutoMlClassification {
38+
39+
// Perform streaming video classification with an AutoML Model
40+
static void streamingAutoMlClassification(String filePath, String projectId, String modelId) {
41+
// String filePath = "path_to_your_video_file";
42+
// String projectId = "YOUR_GCP_PROJECT_ID";
43+
// String modelId = "YOUR_AUTO_ML_CLASSIFICATION_MODEL_ID";
44+
45+
try (StreamingVideoIntelligenceServiceClient client =
46+
StreamingVideoIntelligenceServiceClient.create()) {
47+
48+
Path path = Paths.get(filePath);
49+
byte[] data = Files.readAllBytes(path);
50+
// Set the chunk size to 5MB (recommended less than 10MB).
51+
int chunkSize = 5 * 1024 * 1024;
52+
int numChunks = (int) Math.ceil((double) data.length / chunkSize);
53+
54+
String modelPath = String.format("projects/%s/locations/us-central1/models/%s",
55+
projectId,
56+
modelId);
57+
58+
System.out.println(modelPath);
59+
60+
StreamingAutomlClassificationConfig streamingAutomlClassificationConfig =
61+
StreamingAutomlClassificationConfig.newBuilder()
62+
.setModelName(modelPath)
63+
.build();
64+
65+
StreamingVideoConfig streamingVideoConfig = StreamingVideoConfig.newBuilder()
66+
.setFeature(StreamingFeature.STREAMING_AUTOML_CLASSIFICATION)
67+
.setAutomlClassificationConfig(streamingAutomlClassificationConfig)
68+
.build();
69+
70+
BidiStream<StreamingAnnotateVideoRequest, StreamingAnnotateVideoResponse> call =
71+
client.streamingAnnotateVideoCallable().call();
72+
73+
// The first request must **only** contain the audio configuration:
74+
call.send(
75+
StreamingAnnotateVideoRequest.newBuilder()
76+
.setVideoConfig(streamingVideoConfig)
77+
.build());
78+
79+
// Subsequent requests must **only** contain the audio data.
80+
// Send the requests in chunks
81+
for (int i = 0; i < numChunks; i++) {
82+
call.send(
83+
StreamingAnnotateVideoRequest.newBuilder()
84+
.setInputContent(ByteString.copyFrom(
85+
Arrays.copyOfRange(data, i * chunkSize, i * chunkSize + chunkSize)))
86+
.build());
87+
}
88+
89+
// Tell the service you are done sending data
90+
call.closeSend();
91+
92+
for (StreamingAnnotateVideoResponse response : call) {
93+
if (response.hasError()) {
94+
System.out.println(response.getError().getMessage());
95+
break;
96+
}
97+
98+
StreamingVideoAnnotationResults annotationResults = response.getAnnotationResults();
99+
100+
for (LabelAnnotation annotation : annotationResults.getLabelAnnotationsList()) {
101+
String entity = annotation.getEntity().getDescription();
102+
103+
// There is only one frame per annotation
104+
LabelFrame labelFrame = annotation.getFrames(0);
105+
double offset = labelFrame.getTimeOffset().getSeconds()
106+
+ labelFrame.getTimeOffset().getNanos() / 1e9;
107+
float confidence = labelFrame.getConfidence();
108+
109+
System.out.format("%fs: %s (%f)\n", offset, entity, confidence);
110+
}
111+
}
112+
} catch (Exception e) {
113+
e.printStackTrace();
114+
}
115+
}
116+
}
117+
// [END video_streaming_automl_classification_beta]

samples/snippets/src/main/java/com/example/video/TextDetection.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static VideoAnnotationResults detectText(String filePath) throws Exceptio
6363

6464
System.out.println("Waiting for operation to complete...");
6565
// The first result is retrieved because a single video was processed.
66-
AnnotateVideoResponse response = future.get(300, TimeUnit.SECONDS);
66+
AnnotateVideoResponse response = future.get(600, TimeUnit.SECONDS);
6767
VideoAnnotationResults results = response.getAnnotationResults(0);
6868

6969
// Get only the first annotation for demo purposes.
@@ -123,7 +123,7 @@ public static VideoAnnotationResults detectTextGcs(String gcsUri) throws Excepti
123123

124124
System.out.println("Waiting for operation to complete...");
125125
// The first result is retrieved because a single video was processed.
126-
AnnotateVideoResponse response = future.get(300, TimeUnit.SECONDS);
126+
AnnotateVideoResponse response = future.get(600, TimeUnit.SECONDS);
127127
VideoAnnotationResults results = response.getAnnotationResults(0);
128128

129129
// Get only the first annotation for demo purposes.

samples/snippets/src/main/java/com/example/video/TrackObjects.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static VideoAnnotationResults trackObjects(String filePath) throws Except
6363

6464
System.out.println("Waiting for operation to complete...");
6565
// The first result is retrieved because a single video was processed.
66-
AnnotateVideoResponse response = future.get(300, TimeUnit.SECONDS);
66+
AnnotateVideoResponse response = future.get(600, TimeUnit.SECONDS);
6767
VideoAnnotationResults results = response.getAnnotationResults(0);
6868

6969
// Get only the first annotation for demo purposes.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
18+
package com.example.video;
19+
20+
import static com.google.common.truth.Truth.assertThat;
21+
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.PrintStream;
24+
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+
32+
/**
33+
* Integration (system) tests for {@link StreamingAutoMlClassification}.
34+
*/
35+
@RunWith(JUnit4.class)
36+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
37+
public class StreamingAutoMlClassificationIT {
38+
39+
private static String PROJECT_ID = "779844219229"; // System.getenv().get("GOOGLE_CLOUD_PROJECT");
40+
private static String MODEL_ID = "VCN6455760532254228480";
41+
42+
private ByteArrayOutputStream bout;
43+
private PrintStream out;
44+
45+
@Before
46+
public void setUp() {
47+
bout = new ByteArrayOutputStream();
48+
out = new PrintStream(bout);
49+
System.setOut(out);
50+
}
51+
52+
@After
53+
public void tearDown() {
54+
System.setOut(null);
55+
}
56+
57+
@Test
58+
public void testStreamingAutoMlClassification() {
59+
StreamingAutoMlClassification.streamingAutoMlClassification(
60+
"resources/cat.mp4",
61+
PROJECT_ID,
62+
MODEL_ID);
63+
String got = bout.toString();
64+
65+
assertThat(got).contains("brush_hair");
66+
}
67+
}

0 commit comments

Comments
 (0)