Skip to content

Commit a38833b

Browse files
authored
samples: add streaming_automl_object_tracking sample (#502)
1 parent 83112bd commit a38833b

File tree

2 files changed

+209
-0
lines changed

2 files changed

+209
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* Copyright 2021 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_object_tracking_beta]
20+
21+
import com.google.api.gax.rpc.BidiStream;
22+
import com.google.cloud.videointelligence.v1p3beta1.ObjectTrackingAnnotation;
23+
import com.google.cloud.videointelligence.v1p3beta1.ObjectTrackingFrame;
24+
import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoRequest;
25+
import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoResponse;
26+
import com.google.cloud.videointelligence.v1p3beta1.StreamingAutomlObjectTrackingConfig;
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+
39+
class StreamingAutoMlObjectTracking {
40+
41+
public static void main(String[] args)
42+
throws IOException {
43+
// TODO(developer): Replace these variables before running the sample.
44+
String filePath = "YOUR_VIDEO_FILE";
45+
String projectId = "YOUR_PROJECT_ID";
46+
String modelId = "YOUR_AUTOML_OBJECT_TRACKING_MODEL_ID";
47+
streamingAutoMlObjectTracking(filePath, projectId, modelId);
48+
}
49+
50+
// Perform streaming video object tracking with an AutoML Model
51+
static void streamingAutoMlObjectTracking(String filePath, String projectId, String modelId)
52+
throws StatusRuntimeException, IOException {
53+
54+
try (StreamingVideoIntelligenceServiceClient client =
55+
StreamingVideoIntelligenceServiceClient.create()) {
56+
57+
Path path = Paths.get(filePath);
58+
byte[] data = Files.readAllBytes(path);
59+
// Set the chunk size to 5MB (recommended less than 10MB).
60+
int chunkSize = 5 * 1024 * 1024;
61+
int numChunks = (int) Math.ceil((double) data.length / chunkSize);
62+
63+
String modelPath =
64+
String.format("projects/%s/locations/us-central1/models/%s", projectId, modelId);
65+
66+
StreamingAutomlObjectTrackingConfig streamingAutomlObjectTrackingConfig =
67+
StreamingAutomlObjectTrackingConfig.newBuilder()
68+
.setModelName(modelPath)
69+
.build();
70+
71+
StreamingVideoConfig streamingVideoConfig =
72+
StreamingVideoConfig.newBuilder()
73+
.setFeature(StreamingFeature.STREAMING_AUTOML_OBJECT_TRACKING)
74+
.setAutomlObjectTrackingConfig(streamingAutomlObjectTrackingConfig)
75+
.build();
76+
77+
BidiStream<StreamingAnnotateVideoRequest, StreamingAnnotateVideoResponse> call =
78+
client.streamingAnnotateVideoCallable().call();
79+
80+
// The first request must **only** contain the audio configuration:
81+
call.send(
82+
StreamingAnnotateVideoRequest.newBuilder()
83+
.setVideoConfig(streamingVideoConfig)
84+
.build());
85+
86+
// Subsequent requests must **only** contain the audio data.
87+
// Send the requests in chunks
88+
for (int i = 0; i < numChunks; i++) {
89+
call.send(
90+
StreamingAnnotateVideoRequest.newBuilder()
91+
.setInputContent(
92+
ByteString.copyFrom(
93+
Arrays.copyOfRange(data, i * chunkSize, i * chunkSize + chunkSize)))
94+
.build());
95+
}
96+
97+
// Tell the service you are done sending data
98+
call.closeSend();
99+
100+
for (StreamingAnnotateVideoResponse response : call) {
101+
StreamingVideoAnnotationResults annotationResults = response.getAnnotationResults();
102+
103+
for (ObjectTrackingAnnotation objectAnnotations :
104+
annotationResults.getObjectAnnotationsList()) {
105+
106+
String entity = objectAnnotations.getEntity().getDescription();
107+
float confidence = objectAnnotations.getConfidence();
108+
long trackId = objectAnnotations.getTrackId();
109+
System.out.format("%s: %f (ID: %d)\n", entity, confidence, trackId);
110+
111+
// In streaming, there is always one frame.
112+
ObjectTrackingFrame frame = objectAnnotations.getFrames(0);
113+
double offset =
114+
frame.getTimeOffset().getSeconds() + frame.getTimeOffset().getNanos() / 1e9;
115+
System.out.format("Offset: %f\n", offset);
116+
117+
System.out.println("Bounding Box:");
118+
System.out.format("\tLeft: %f\n", frame.getNormalizedBoundingBox().getLeft());
119+
System.out.format("\tTop: %f\n", frame.getNormalizedBoundingBox().getTop());
120+
System.out.format("\tRight: %f\n", frame.getNormalizedBoundingBox().getRight());
121+
System.out.format("\tBottom: %f\n", frame.getNormalizedBoundingBox().getBottom());
122+
}
123+
}
124+
System.out.println("Video streamed successfully.");
125+
}
126+
}
127+
}
128+
// [END video_streaming_automl_object_tracking_beta]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2021 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.IOException;
25+
import java.io.PrintStream;
26+
import org.junit.After;
27+
import org.junit.Before;
28+
import org.junit.Test;
29+
import org.junit.runner.RunWith;
30+
import org.junit.runners.JUnit4;
31+
32+
/** Integration (system) tests for {@link StreamingAutoMlObjectTracking}. */
33+
@RunWith(JUnit4.class)
34+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
35+
public class StreamingAutoMlObjectTrackingIT {
36+
37+
private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT");
38+
private static String MODEL_ID = System.getenv().get("VIDEO_OBJECT_TRACKING_MODEL_ID");
39+
40+
private ByteArrayOutputStream bout;
41+
private PrintStream out;
42+
private PrintStream originalPrintStream;
43+
44+
@Before
45+
public void setUp() {
46+
bout = new ByteArrayOutputStream();
47+
out = new PrintStream(bout);
48+
originalPrintStream = System.out;
49+
System.setOut(out);
50+
}
51+
52+
@After
53+
public void tearDown() {
54+
// restores print statements in the original method
55+
System.out.flush();
56+
System.setOut(originalPrintStream);
57+
}
58+
59+
@Test
60+
public void testStreamingAutoMlObjectTracking() {
61+
// Bad Gateway sporadically occurs
62+
int tryCount = 0;
63+
int maxTries = 3;
64+
while (tryCount < maxTries) {
65+
try {
66+
StreamingAutoMlObjectTracking.streamingAutoMlObjectTracking(
67+
"resources/cat.mp4", PROJECT_ID, MODEL_ID);
68+
assertThat(bout.toString()).contains("Video streamed successfully.");
69+
70+
break;
71+
} catch (StatusRuntimeException ex) {
72+
if (ex.getStatus().getCode() == Status.Code.UNAVAILABLE) {
73+
assertThat(ex.getMessage()).contains("Bad Gateway");
74+
tryCount++;
75+
}
76+
} catch (Exception e) {
77+
e.printStackTrace();
78+
}
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)