Skip to content

Commit c90a83d

Browse files
authored
Merge pull request #678 from GoogleCloudPlatform/video-quickstart
Adds quickstart and test for Video
2 parents a90e1d7 + cf56d46 commit c90a83d

File tree

3 files changed

+138
-1
lines changed

3 files changed

+138
-1
lines changed

video/cloud-client/src/main/java/com/example/video/Detect.java

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import java.nio.file.Files;
3636
import java.nio.file.Path;
3737
import java.nio.file.Paths;
38-
import java.util.ArrayList;
3938
import java.util.concurrent.ExecutionException;
4039

4140

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2017, Google, Inc.
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 videointelligence_quickstart]
20+
import com.google.api.gax.grpc.OperationFuture;
21+
import com.google.cloud.videointelligence.spi.v1beta1.VideoIntelligenceServiceClient;
22+
import com.google.cloud.videointelligence.spi.v1beta1.VideoIntelligenceServiceSettings;
23+
import com.google.cloud.videointelligence.v1beta1.AnnotateVideoRequest;
24+
import com.google.cloud.videointelligence.v1beta1.AnnotateVideoResponse;
25+
import com.google.cloud.videointelligence.v1beta1.Feature;
26+
import com.google.cloud.videointelligence.v1beta1.LabelAnnotation;
27+
import com.google.cloud.videointelligence.v1beta1.LabelLocation;
28+
import com.google.cloud.videointelligence.v1beta1.VideoAnnotationResults;
29+
30+
import java.io.IOException;
31+
import java.util.concurrent.ExecutionException;
32+
33+
34+
public class QuickstartSample {
35+
public static void main(String[] args) throws
36+
ExecutionException, IOException, InterruptedException {
37+
// Instantiate the client
38+
VideoIntelligenceServiceSettings settings =
39+
VideoIntelligenceServiceSettings.defaultBuilder().build();
40+
VideoIntelligenceServiceClient client = VideoIntelligenceServiceClient.create(settings);
41+
42+
// The Google Cloud Storage path to the video to annotate.
43+
String gcsUri = "gs://demomaker/cat.mp4";
44+
45+
// Create an operation that will contain the response when the operation completes.
46+
AnnotateVideoRequest request = AnnotateVideoRequest.newBuilder()
47+
.setInputUri(gcsUri)
48+
.addFeatures(Feature.LABEL_DETECTION)
49+
.build();
50+
51+
OperationFuture<AnnotateVideoResponse> operation =
52+
client.annotateVideoAsync(request);
53+
54+
System.out.println("Waiting for operation to complete...");
55+
for (VideoAnnotationResults result : operation.get().getAnnotationResultsList()) {
56+
if (result.getLabelAnnotationsCount() > 0) {
57+
System.out.println("Labels:");
58+
for (LabelAnnotation annotation : result.getLabelAnnotationsList()) {
59+
System.out.println("\tDescription: " + annotation.getDescription());
60+
for (LabelLocation loc : annotation.getLocationsList()) {
61+
if (loc.getSegment().getStartTimeOffset() == -1
62+
&& loc.getSegment().getEndTimeOffset() == -1) {
63+
System.out.println("\tLocation: Entire video");
64+
} else {
65+
System.out.printf(
66+
"\tLocation: %fs - %fs\n",
67+
loc.getSegment().getStartTimeOffset() / 1000000.0,
68+
loc.getSegment().getEndTimeOffset() / 1000000.0);
69+
}
70+
}
71+
System.out.println();
72+
}
73+
} else {
74+
System.out.println("No labels detected in " + gcsUri);
75+
}
76+
}
77+
}
78+
}
79+
// [END videointelligence_quickstart]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Copyright 2017, Google, Inc.
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+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import org.junit.After;
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.junit.runners.JUnit4;
26+
27+
import java.io.ByteArrayOutputStream;
28+
import java.io.IOException;
29+
import java.io.PrintStream;
30+
import java.util.concurrent.ExecutionException;
31+
32+
/** Tests for video analysis sample. */
33+
@RunWith(JUnit4.class)
34+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
35+
public class QuickstartIT {
36+
private ByteArrayOutputStream bout;
37+
private PrintStream out;
38+
39+
@Before
40+
public void setUp() {
41+
bout = new ByteArrayOutputStream();
42+
out = new PrintStream(bout);
43+
System.setOut(out);
44+
}
45+
46+
@After
47+
public void tearDown() {
48+
System.setOut(null);
49+
}
50+
51+
@Test
52+
public void test() throws IOException, InterruptedException, ExecutionException {
53+
QuickstartSample.main(new String[0]);
54+
String got = bout.toString();
55+
56+
// Test that the video with a cat has the whiskers label (may change).
57+
assertThat(got.toUpperCase()).contains("WHISKERS");
58+
}
59+
}

0 commit comments

Comments
 (0)