Skip to content

Commit

Permalink
Update samples to v1 (#935)
Browse files Browse the repository at this point in the history
  • Loading branch information
nnegrey authored and jabubake committed Nov 30, 2017
1 parent b724502 commit 8677f76
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 47 deletions.
68 changes: 32 additions & 36 deletions video/cloud-client/src/main/java/com/example/video/Detect.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -105,17 +101,18 @@ 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)
.addFeatures(Feature.FACE_DETECTION)
.build();

// asynchronously perform facial analysis on videos
OperationFuture<AnnotateVideoResponse, AnnotateVideoProgress> response
= client.annotateVideoAsync(request);
OperationFuture<AnnotateVideoResponse, AnnotateVideoProgress> response =
client.annotateVideoAsync(request);

System.out.println("Waiting for operation to complete...");
boolean faceFound = false;
for (VideoAnnotationResults results : response.get().getAnnotationResultsList()) {
int faceCount = 0;
Expand Down Expand Up @@ -166,19 +163,19 @@ 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()
.setInputUri(gcsUri)
.addFeatures(Feature.LABEL_DETECTION)
.build();
// Create an operation that will contain the response when the operation completes.
OperationFuture<AnnotateVideoResponse, AnnotateVideoProgress> operation =
OperationFuture<AnnotateVideoResponse, AnnotateVideoProgress> 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()) {
Expand Down Expand Up @@ -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);
Expand All @@ -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<AnnotateVideoResponse, AnnotateVideoProgress> operation =
OperationFuture<AnnotateVideoResponse, AnnotateVideoProgress> 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()) {
Expand Down Expand Up @@ -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()
Expand All @@ -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<AnnotateVideoResponse, AnnotateVideoProgress> operation =
OperationFuture<AnnotateVideoResponse, AnnotateVideoProgress> 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()) {
Expand All @@ -374,21 +371,20 @@ 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()
.setInputUri(gcsUri)
.addFeatures(Feature.EXPLICIT_CONTENT_DETECTION)
.build();

OperationFuture<AnnotateVideoResponse, AnnotateVideoProgress> operation =
OperationFuture<AnnotateVideoResponse, AnnotateVideoProgress> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -47,12 +47,12 @@ public static void main(String[] args) throws Exception {
.addFeatures(Feature.LABEL_DETECTION)
.build();

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

System.out.println("Waiting for operation to complete...");

List<VideoAnnotationResults> results = operation.get().getAnnotationResultsList();
List<VideoAnnotationResults> results = response.get().getAnnotationResultsList();
if (results.isEmpty()) {
System.out.println("No labels detected in " + gcsUri);
return;
Expand Down

0 comments on commit 8677f76

Please sign in to comment.