From 8677f761c0f1760f6b88c0701f36b00a032aaf82 Mon Sep 17 00:00:00 2001 From: Noah Negrey Date: Thu, 30 Nov 2017 10:34:07 -0800 Subject: [PATCH] Update samples to v1 (#935) --- .../main/java/com/example/video/Detect.java | 68 +++++++++---------- .../com/example/video/QuickstartSample.java | 22 +++--- 2 files changed, 43 insertions(+), 47 deletions(-) diff --git a/video/cloud-client/src/main/java/com/example/video/Detect.java b/video/cloud-client/src/main/java/com/example/video/Detect.java index 23043145a91..35836c3f8f4 100644 --- a/video/cloud-client/src/main/java/com/example/video/Detect.java +++ b/video/cloud-client/src/main/java/com/example/video/Detect.java @@ -17,25 +17,21 @@ package com.example.video; import com.google.api.gax.longrunning.OperationFuture; -import com.google.cloud.videointelligence.v1beta2.AnnotateVideoProgress; -import com.google.cloud.videointelligence.v1beta2.AnnotateVideoRequest; -import com.google.cloud.videointelligence.v1beta2.AnnotateVideoResponse; -import com.google.cloud.videointelligence.v1beta2.Entity; -import com.google.cloud.videointelligence.v1beta2.ExplicitContentFrame; -import com.google.cloud.videointelligence.v1beta2.FaceAnnotation; -import com.google.cloud.videointelligence.v1beta2.FaceFrame; -import com.google.cloud.videointelligence.v1beta2.FaceSegment; -import com.google.cloud.videointelligence.v1beta2.Feature; -import com.google.cloud.videointelligence.v1beta2.LabelAnnotation; -import com.google.cloud.videointelligence.v1beta2.LabelDetectionConfig; -import com.google.cloud.videointelligence.v1beta2.LabelDetectionMode; -import com.google.cloud.videointelligence.v1beta2.LabelSegment; -import com.google.cloud.videointelligence.v1beta2.NormalizedBoundingBox; -import com.google.cloud.videointelligence.v1beta2.VideoAnnotationResults; -import com.google.cloud.videointelligence.v1beta2.VideoContext; -import com.google.cloud.videointelligence.v1beta2.VideoIntelligenceServiceClient; -import com.google.cloud.videointelligence.v1beta2.VideoSegment; -import com.google.longrunning.Operation; +import com.google.cloud.videointelligence.v1.AnnotateVideoProgress; +import com.google.cloud.videointelligence.v1.AnnotateVideoRequest; +import com.google.cloud.videointelligence.v1.AnnotateVideoResponse; +import com.google.cloud.videointelligence.v1.Entity; +import com.google.cloud.videointelligence.v1.ExplicitContentFrame; +import com.google.cloud.videointelligence.v1.FaceAnnotation; +import com.google.cloud.videointelligence.v1.FaceFrame; +import com.google.cloud.videointelligence.v1.FaceSegment; +import com.google.cloud.videointelligence.v1.Feature; +import com.google.cloud.videointelligence.v1.LabelAnnotation; +import com.google.cloud.videointelligence.v1.LabelSegment; +import com.google.cloud.videointelligence.v1.NormalizedBoundingBox; +import com.google.cloud.videointelligence.v1.VideoAnnotationResults; +import com.google.cloud.videointelligence.v1.VideoIntelligenceServiceClient; +import com.google.cloud.videointelligence.v1.VideoSegment; import com.google.protobuf.ByteString; import java.io.IOException; import java.nio.file.Files; @@ -105,7 +101,7 @@ public static void argsHelper(String[] args) throws Exception { */ public static void analyzeFaces(String gcsUri) throws Exception { // [START detect_faces] - // Instantiate a com.google.cloud.videointelligence.v1beta2.VideoIntelligenceServiceClient + // Instantiate a com.google.cloud.videointelligence.v1.VideoIntelligenceServiceClient try (VideoIntelligenceServiceClient client = VideoIntelligenceServiceClient.create()) { AnnotateVideoRequest request = AnnotateVideoRequest.newBuilder() .setInputUri(gcsUri) @@ -113,9 +109,10 @@ public static void analyzeFaces(String gcsUri) throws Exception { .build(); // asynchronously perform facial analysis on videos - OperationFuture response - = client.annotateVideoAsync(request); + OperationFuture response = + client.annotateVideoAsync(request); + System.out.println("Waiting for operation to complete..."); boolean faceFound = false; for (VideoAnnotationResults results : response.get().getAnnotationResultsList()) { int faceCount = 0; @@ -166,7 +163,7 @@ public static void analyzeFaces(String gcsUri) throws Exception { */ public static void analyzeLabels(String gcsUri) throws Exception { // [START detect_labels_gcs] - // Instantiate a com.google.cloud.videointelligence.v1beta2.VideoIntelligenceServiceClient + // Instantiate a com.google.cloud.videointelligence.v1.VideoIntelligenceServiceClient try (VideoIntelligenceServiceClient client = VideoIntelligenceServiceClient.create()) { // Provide path to file hosted on GCS as "gs://bucket-name/..." AnnotateVideoRequest request = AnnotateVideoRequest.newBuilder() @@ -174,11 +171,11 @@ public static void analyzeLabels(String gcsUri) throws Exception { .addFeatures(Feature.LABEL_DETECTION) .build(); // Create an operation that will contain the response when the operation completes. - OperationFuture operation = + OperationFuture response = client.annotateVideoAsync(request); System.out.println("Waiting for operation to complete..."); - for (VideoAnnotationResults results : operation.get().getAnnotationResultsList()) { + for (VideoAnnotationResults results : response.get().getAnnotationResultsList()) { // process video / segment level label annotations System.out.println("Locations: "); for (LabelAnnotation labelAnnotation : results.getSegmentLabelAnnotationsList()) { @@ -248,7 +245,7 @@ public static void analyzeLabels(String gcsUri) throws Exception { */ public static void analyzeLabelsFile(String filePath) throws Exception { // [START detect_labels_file] - // Instantiate a com.google.cloud.videointelligence.v1beta2.VideoIntelligenceServiceClient + // Instantiate a com.google.cloud.videointelligence.v1.VideoIntelligenceServiceClient try (VideoIntelligenceServiceClient client = VideoIntelligenceServiceClient.create()) { // Read file and encode into Base64 Path path = Paths.get(filePath); @@ -261,11 +258,11 @@ public static void analyzeLabelsFile(String filePath) throws Exception { .build(); // Create an operation that will contain the response when the operation completes. - OperationFuture operation = + OperationFuture response = client.annotateVideoAsync(request); System.out.println("Waiting for operation to complete..."); - for (VideoAnnotationResults results : operation.get().getAnnotationResultsList()) { + for (VideoAnnotationResults results : response.get().getAnnotationResultsList()) { // process video / segment level label annotations System.out.println("Locations: "); for (LabelAnnotation labelAnnotation : results.getSegmentLabelAnnotationsList()) { @@ -335,7 +332,7 @@ public static void analyzeLabelsFile(String filePath) throws Exception { */ public static void analyzeShots(String gcsUri) throws Exception { // [START detect_shots] - // Instantiate a com.google.cloud.videointelligence.v1beta2.VideoIntelligenceServiceClient + // Instantiate a com.google.cloud.videointelligence.v1.VideoIntelligenceServiceClient try (VideoIntelligenceServiceClient client = VideoIntelligenceServiceClient.create()) { // Provide path to file hosted on GCS as "gs://bucket-name/..." AnnotateVideoRequest request = AnnotateVideoRequest.newBuilder() @@ -344,12 +341,12 @@ public static void analyzeShots(String gcsUri) throws Exception { .build(); // Create an operation that will contain the response when the operation completes. - OperationFuture operation = + OperationFuture response = client.annotateVideoAsync(request); - System.out.println("Waiting for operation to complete..."); + System.out.println("Waiting for operation to complete..."); // Print detected shot changes and their location ranges in the analyzed video. - for (VideoAnnotationResults result : operation.get().getAnnotationResultsList()) { + for (VideoAnnotationResults result : response.get().getAnnotationResultsList()) { if (result.getShotAnnotationsCount() > 0) { System.out.println("Shots: "); for (VideoSegment segment : result.getShotAnnotationsList()) { @@ -374,7 +371,7 @@ public static void analyzeShots(String gcsUri) throws Exception { */ public static void analyzeExplicitContent(String gcsUri) throws Exception { // [START detect_explicit_content] - // Instantiate a com.google.cloud.videointelligence.v1beta2.VideoIntelligenceServiceClient + // Instantiate a com.google.cloud.videointelligence.v1.VideoIntelligenceServiceClient try (VideoIntelligenceServiceClient client = VideoIntelligenceServiceClient.create()) { // Create an operation that will contain the response when the operation completes. AnnotateVideoRequest request = AnnotateVideoRequest.newBuilder() @@ -382,13 +379,12 @@ public static void analyzeExplicitContent(String gcsUri) throws Exception { .addFeatures(Feature.EXPLICIT_CONTENT_DETECTION) .build(); - OperationFuture operation = + OperationFuture response = client.annotateVideoAsync(request); System.out.println("Waiting for operation to complete..."); - // Print detected annotations and their positions in the analyzed video. - for (VideoAnnotationResults result : operation.get().getAnnotationResultsList()) { + for (VideoAnnotationResults result : response.get().getAnnotationResultsList()) { for (ExplicitContentFrame frame : result.getExplicitAnnotation().getFramesList()) { double frameTime = frame.getTimeOffset().getSeconds() + frame.getTimeOffset().getNanos() / 1e9; diff --git a/video/cloud-client/src/main/java/com/example/video/QuickstartSample.java b/video/cloud-client/src/main/java/com/example/video/QuickstartSample.java index e4abed0f895..7b90c0acc5c 100644 --- a/video/cloud-client/src/main/java/com/example/video/QuickstartSample.java +++ b/video/cloud-client/src/main/java/com/example/video/QuickstartSample.java @@ -19,15 +19,15 @@ // [START videointelligence_quickstart] import com.google.api.gax.longrunning.OperationFuture; -import com.google.cloud.videointelligence.v1beta2.AnnotateVideoProgress; -import com.google.cloud.videointelligence.v1beta2.AnnotateVideoRequest; -import com.google.cloud.videointelligence.v1beta2.AnnotateVideoResponse; -import com.google.cloud.videointelligence.v1beta2.Entity; -import com.google.cloud.videointelligence.v1beta2.Feature; -import com.google.cloud.videointelligence.v1beta2.LabelAnnotation; -import com.google.cloud.videointelligence.v1beta2.LabelSegment; -import com.google.cloud.videointelligence.v1beta2.VideoAnnotationResults; -import com.google.cloud.videointelligence.v1beta2.VideoIntelligenceServiceClient; +import com.google.cloud.videointelligence.v1.AnnotateVideoProgress; +import com.google.cloud.videointelligence.v1.AnnotateVideoRequest; +import com.google.cloud.videointelligence.v1.AnnotateVideoResponse; +import com.google.cloud.videointelligence.v1.Entity; +import com.google.cloud.videointelligence.v1.Feature; +import com.google.cloud.videointelligence.v1.LabelAnnotation; +import com.google.cloud.videointelligence.v1.LabelSegment; +import com.google.cloud.videointelligence.v1.VideoAnnotationResults; +import com.google.cloud.videointelligence.v1.VideoIntelligenceServiceClient; import java.util.List; public class QuickstartSample { @@ -47,12 +47,12 @@ public static void main(String[] args) throws Exception { .addFeatures(Feature.LABEL_DETECTION) .build(); - OperationFuture operation = + OperationFuture response = client.annotateVideoAsync(request); System.out.println("Waiting for operation to complete..."); - List results = operation.get().getAnnotationResultsList(); + List results = response.get().getAnnotationResultsList(); if (results.isEmpty()) { System.out.println("No labels detected in " + gcsUri); return;