|
| 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] |
0 commit comments