Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds quickstart and test for Video #678

Merged
merged 1 commit into from
May 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 2017, Google, Inc.
*
* 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 com.example.video;

// [START videointelligence_quickstart]
import com.google.api.gax.grpc.OperationFuture;
import com.google.cloud.videointelligence.spi.v1beta1.VideoIntelligenceServiceClient;
import com.google.cloud.videointelligence.spi.v1beta1.VideoIntelligenceServiceSettings;
import com.google.cloud.videointelligence.v1beta1.AnnotateVideoRequest;
import com.google.cloud.videointelligence.v1beta1.AnnotateVideoResponse;
import com.google.cloud.videointelligence.v1beta1.Feature;
import com.google.cloud.videointelligence.v1beta1.LabelAnnotation;
import com.google.cloud.videointelligence.v1beta1.LabelLocation;
import com.google.cloud.videointelligence.v1beta1.VideoAnnotationResults;

import java.io.IOException;
import java.util.concurrent.ExecutionException;


public class QuickstartSample {
public static void main(String[] args) throws
ExecutionException, IOException, InterruptedException {
// Instantiate the client
VideoIntelligenceServiceSettings settings =
VideoIntelligenceServiceSettings.defaultBuilder().build();
VideoIntelligenceServiceClient client = VideoIntelligenceServiceClient.create(settings);

// The Google Cloud Storage path to the video to annotate.
String gcsUri = "gs://demomaker/cat.mp4";

// Create an operation that will contain the response when the operation completes.
AnnotateVideoRequest request = AnnotateVideoRequest.newBuilder()
.setInputUri(gcsUri)
.addFeatures(Feature.LABEL_DETECTION)
.build();

OperationFuture<AnnotateVideoResponse> operation =
client.annotateVideoAsync(request);

System.out.println("Waiting for operation to complete...");
for (VideoAnnotationResults result : operation.get().getAnnotationResultsList()) {
if (result.getLabelAnnotationsCount() > 0) {
System.out.println("Labels:");
for (LabelAnnotation annotation : result.getLabelAnnotationsList()) {
System.out.println("\tDescription: " + annotation.getDescription());
for (LabelLocation loc : annotation.getLocationsList()) {
if (loc.getSegment().getStartTimeOffset() == -1
&& loc.getSegment().getEndTimeOffset() == -1) {
System.out.println("\tLocation: Entire video");
} else {
System.out.printf(
"\tLocation: %fs - %fs\n",
loc.getSegment().getStartTimeOffset() / 1000000.0,
loc.getSegment().getEndTimeOffset() / 1000000.0);
}
}
System.out.println();
}
} else {
System.out.println("No labels detected in " + gcsUri);
}
}
}
}
// [END videointelligence_quickstart]
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright 2017, Google, Inc.

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 com.example.video;

import static com.google.common.truth.Truth.assertThat;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.concurrent.ExecutionException;

/** Tests for video analysis sample. */
@RunWith(JUnit4.class)
@SuppressWarnings("checkstyle:abbreviationaswordinname")
public class QuickstartIT {
private ByteArrayOutputStream bout;
private PrintStream out;

@Before
public void setUp() {
bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
System.setOut(out);
}

@After
public void tearDown() {
System.setOut(null);
}

@Test
public void test() throws IOException, InterruptedException, ExecutionException {
QuickstartSample.main(new String[0]);
String got = bout.toString();

// Test that the video with a cat has the whiskers label (may change).
assertThat(got.toUpperCase()).contains("WHISKERS");
}
}