From a62b6fe217593601a7d8324ca922c5a364a8455e Mon Sep 17 00:00:00 2001 From: nnegrey Date: Tue, 3 Mar 2020 12:45:09 -0700 Subject: [PATCH 1/3] video: move samples out of branch --- .../java/com/example/video/DetectLogo.java | 147 ++++++++++++++++++ .../java/com/example/video/DetectLogoGcs.java | 138 ++++++++++++++++ .../com/example/video/DetectLogoGcsTest.java | 51 ++++++ .../com/example/video/DetectLogoTest.java | 51 ++++++ 4 files changed, 387 insertions(+) create mode 100644 video/beta/src/main/java/com/example/video/DetectLogo.java create mode 100644 video/beta/src/main/java/com/example/video/DetectLogoGcs.java create mode 100644 video/beta/src/test/java/com/example/video/DetectLogoGcsTest.java create mode 100644 video/beta/src/test/java/com/example/video/DetectLogoTest.java diff --git a/video/beta/src/main/java/com/example/video/DetectLogo.java b/video/beta/src/main/java/com/example/video/DetectLogo.java new file mode 100644 index 00000000000..95b81135987 --- /dev/null +++ b/video/beta/src/main/java/com/example/video/DetectLogo.java @@ -0,0 +1,147 @@ +/* + * Copyright 2019 Google LLC + * + * 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 video_detect_logo_beta] +import com.google.cloud.videointelligence.v1p3beta1.AnnotateVideoRequest; +import com.google.cloud.videointelligence.v1p3beta1.AnnotateVideoResponse; +import com.google.cloud.videointelligence.v1p3beta1.DetectedAttribute; +import com.google.cloud.videointelligence.v1p3beta1.Entity; +import com.google.cloud.videointelligence.v1p3beta1.Feature; +import com.google.cloud.videointelligence.v1p3beta1.LogoRecognitionAnnotation; +import com.google.cloud.videointelligence.v1p3beta1.NormalizedBoundingBox; +import com.google.cloud.videointelligence.v1p3beta1.TimestampedObject; +import com.google.cloud.videointelligence.v1p3beta1.Track; +import com.google.cloud.videointelligence.v1p3beta1.VideoAnnotationResults; +import com.google.cloud.videointelligence.v1p3beta1.VideoIntelligenceServiceClient; +import com.google.cloud.videointelligence.v1p3beta1.VideoSegment; +import com.google.protobuf.ByteString; +import com.google.protobuf.Duration; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +import java.util.concurrent.ExecutionException; + +public class DetectLogo { + + public void detectLogo() throws IOException, ExecutionException, InterruptedException { + String filePath = "path/to/your/video.mp4"; + detectLogo(filePath); + } + + public static void detectLogo(String localFilePath) + throws IOException, ExecutionException, InterruptedException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (VideoIntelligenceServiceClient client = VideoIntelligenceServiceClient.create()) { + // Read the files contents + Path path = Paths.get(localFilePath); + byte[] data = Files.readAllBytes(path); + ByteString inputContent = ByteString.copyFrom(data); + + // Build the request with the inputContent and set the Feature + AnnotateVideoRequest request = + AnnotateVideoRequest.newBuilder() + .setInputContent(inputContent) + .addFeatures(Feature.LOGO_RECOGNITION) + .build(); + + // Make the asynchronous request + AnnotateVideoResponse response = client.annotateVideoAsync(request).get(); + + // Get the first response, since we sent only one video. + VideoAnnotationResults annotationResult = response.getAnnotationResultsList().get(0); + + // Annotations for list of logos detected, tracked and recognized in the video. + for (LogoRecognitionAnnotation logoRecognitionAnnotation : + annotationResult.getLogoRecognitionAnnotationsList()) { + + Entity entity = logoRecognitionAnnotation.getEntity(); + // Opaque entity ID. Some IDs may be available in [Google Knowledge Graph Search + // API](https://developers.google.com/knowledge-graph/). + System.out.printf("Entity Id: %s\n", entity.getEntityId()); + System.out.printf("Description: %s\n", entity.getDescription()); + + // All logo tracks where the recognized logo appears. Each track corresponds to one logo + // instance appearing in consecutive frames. + for (Track track : logoRecognitionAnnotation.getTracksList()) { + + // Video segment of a track. + VideoSegment segment = track.getSegment(); + Duration segmentStartTimeOffset = segment.getStartTimeOffset(); + System.out.printf( + "\n\tStart Time Offset: %s.%s\n", + segmentStartTimeOffset.getSeconds(), segmentStartTimeOffset.getNanos()); + Duration segmentEndTimeOffset = segment.getEndTimeOffset(); + System.out.printf( + "\tEnd Time Offset: %s.%s\n", + segmentEndTimeOffset.getSeconds(), segmentEndTimeOffset.getNanos()); + System.out.printf("\tConfidence: %s\n", track.getConfidence()); + + // The object with timestamp and attributes per frame in the track. + for (TimestampedObject timestampedObject : track.getTimestampedObjectsList()) { + + // Normalized Bounding box in a frame, where the object is located. + NormalizedBoundingBox normalizedBoundingBox = + timestampedObject.getNormalizedBoundingBox(); + System.out.printf("\n\t\tLeft: %s\n", normalizedBoundingBox.getLeft()); + System.out.printf("\t\tTop: %s\n", normalizedBoundingBox.getTop()); + System.out.printf("\t\tRight: %s\n", normalizedBoundingBox.getRight()); + System.out.printf("\t\tBottom: %s\n", normalizedBoundingBox.getBottom()); + + // Optional. The attributes of the object in the bounding box. + for (DetectedAttribute attribute : timestampedObject.getAttributesList()) { + System.out.printf("\n\t\t\tName: %s\n", attribute.getName()); + System.out.printf("\t\t\tConfidence: %s\n", attribute.getConfidence()); + System.out.printf("\t\t\tValue: %s\n", attribute.getValue()); + } + } + + // Optional. Attributes in the track level. + for (DetectedAttribute trackAttribute : track.getAttributesList()) { + System.out.printf("\n\t\tName : %s\n", trackAttribute.getName()); + System.out.printf("\t\tConfidence : %s\n", trackAttribute.getConfidence()); + System.out.printf("\t\tValue : %s\n", trackAttribute.getValue()); + } + } + + // All video segments where the recognized logo appears. There might be multiple instances + // of the same logo class appearing in one VideoSegment. + for (VideoSegment logoRecognitionAnnotationSegment : + logoRecognitionAnnotation.getSegmentsList()) { + Duration logoRecognitionAnnotationSegmentStartTimeOffset = + logoRecognitionAnnotationSegment.getStartTimeOffset(); + System.out.printf( + "\n\tStart Time Offset : %s.%s\n", + logoRecognitionAnnotationSegmentStartTimeOffset.getSeconds(), + logoRecognitionAnnotationSegmentStartTimeOffset.getNanos()); + Duration logoRecognitionAnnotationSegmentEndTimeOffset = + logoRecognitionAnnotationSegment.getEndTimeOffset(); + System.out.printf( + "\tEnd Time Offset : %s.%s\n", + logoRecognitionAnnotationSegmentEndTimeOffset.getSeconds(), + logoRecognitionAnnotationSegmentEndTimeOffset.getNanos()); + } + } + } + } +} +// [END video_detect_logo_beta] diff --git a/video/beta/src/main/java/com/example/video/DetectLogoGcs.java b/video/beta/src/main/java/com/example/video/DetectLogoGcs.java new file mode 100644 index 00000000000..81d8a8b68e9 --- /dev/null +++ b/video/beta/src/main/java/com/example/video/DetectLogoGcs.java @@ -0,0 +1,138 @@ +/* + * Copyright 2019 Google LLC + * + * 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 video_detect_logo_gcs_beta] +import com.google.cloud.videointelligence.v1p3beta1.AnnotateVideoRequest; +import com.google.cloud.videointelligence.v1p3beta1.AnnotateVideoResponse; +import com.google.cloud.videointelligence.v1p3beta1.DetectedAttribute; +import com.google.cloud.videointelligence.v1p3beta1.Entity; +import com.google.cloud.videointelligence.v1p3beta1.Feature; +import com.google.cloud.videointelligence.v1p3beta1.LogoRecognitionAnnotation; +import com.google.cloud.videointelligence.v1p3beta1.NormalizedBoundingBox; +import com.google.cloud.videointelligence.v1p3beta1.TimestampedObject; +import com.google.cloud.videointelligence.v1p3beta1.Track; +import com.google.cloud.videointelligence.v1p3beta1.VideoAnnotationResults; +import com.google.cloud.videointelligence.v1p3beta1.VideoIntelligenceServiceClient; +import com.google.cloud.videointelligence.v1p3beta1.VideoSegment; +import com.google.protobuf.Duration; + +import java.io.IOException; + +import java.util.concurrent.ExecutionException; + +public class DetectLogoGcs { + + public void detectLogo() throws IOException, ExecutionException, InterruptedException { + String inputUri = "gs://cloud-samples-data/video/googlework_short.mp4"; + detectLogoGcs(inputUri); + } + + public static void detectLogoGcs(String inputUri) + throws IOException, ExecutionException, InterruptedException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (VideoIntelligenceServiceClient client = VideoIntelligenceServiceClient.create()) { + // Build the request with the inputUri and set the Feature + AnnotateVideoRequest request = + AnnotateVideoRequest.newBuilder() + .setInputUri(inputUri) + .addFeatures(Feature.LOGO_RECOGNITION) + .build(); + + // Make the asynchronous request + AnnotateVideoResponse response = client.annotateVideoAsync(request).get(); + + // Get the first response, since we sent only one video. + VideoAnnotationResults annotationResult = response.getAnnotationResultsList().get(0); + + // Annotations for list of logos detected, tracked and recognized in the video. + for (LogoRecognitionAnnotation logoRecognitionAnnotation : + annotationResult.getLogoRecognitionAnnotationsList()) { + + Entity entity = logoRecognitionAnnotation.getEntity(); + // Opaque entity ID. Some IDs may be available in [Google Knowledge Graph Search + // API](https://developers.google.com/knowledge-graph/). + System.out.printf("Entity Id: %s\n", entity.getEntityId()); + System.out.printf("Description: %s\n", entity.getDescription()); + + // All logo tracks where the recognized logo appears. Each track corresponds to one logo + // instance appearing in consecutive frames. + for (Track track : logoRecognitionAnnotation.getTracksList()) { + + // Video segment of a track. + VideoSegment segment = track.getSegment(); + Duration segmentStartTimeOffset = segment.getStartTimeOffset(); + System.out.printf( + "\n\tStart Time Offset: %s.%s\n", + segmentStartTimeOffset.getSeconds(), segmentStartTimeOffset.getNanos()); + Duration segmentEndTimeOffset = segment.getEndTimeOffset(); + System.out.printf( + "\tEnd Time Offset: %s.%s\n", + segmentEndTimeOffset.getSeconds(), segmentEndTimeOffset.getNanos()); + System.out.printf("\tConfidence: %s\n", track.getConfidence()); + + // The object with timestamp and attributes per frame in the track. + for (TimestampedObject timestampedObject : track.getTimestampedObjectsList()) { + + // Normalized Bounding box in a frame, where the object is located. + NormalizedBoundingBox normalizedBoundingBox = + timestampedObject.getNormalizedBoundingBox(); + System.out.printf("\n\t\tLeft: %s\n", normalizedBoundingBox.getLeft()); + System.out.printf("\t\tTop: %s\n", normalizedBoundingBox.getTop()); + System.out.printf("\t\tRight: %s\n", normalizedBoundingBox.getRight()); + System.out.printf("\t\tBottom: %s\n", normalizedBoundingBox.getBottom()); + + // Optional. The attributes of the object in the bounding box. + for (DetectedAttribute attribute : timestampedObject.getAttributesList()) { + System.out.printf("\n\t\t\tName: %s\n", attribute.getName()); + System.out.printf("\t\t\tConfidence: %s\n", attribute.getConfidence()); + System.out.printf("\t\t\tValue: %s\n", attribute.getValue()); + } + } + + // Optional. Attributes in the track level. + for (DetectedAttribute trackAttribute : track.getAttributesList()) { + System.out.printf("\n\t\tName : %s\n", trackAttribute.getName()); + System.out.printf("\t\tConfidence : %s\n", trackAttribute.getConfidence()); + System.out.printf("\t\tValue : %s\n", trackAttribute.getValue()); + } + } + + // All video segments where the recognized logo appears. There might be multiple instances + // of the same logo class appearing in one VideoSegment. + for (VideoSegment logoRecognitionAnnotationSegment : + logoRecognitionAnnotation.getSegmentsList()) { + Duration logoRecognitionAnnotationSegmentStartTimeOffset = + logoRecognitionAnnotationSegment.getStartTimeOffset(); + System.out.printf( + "\n\tStart Time Offset : %s.%s\n", + logoRecognitionAnnotationSegmentStartTimeOffset.getSeconds(), + logoRecognitionAnnotationSegmentStartTimeOffset.getNanos()); + Duration logoRecognitionAnnotationSegmentEndTimeOffset = + logoRecognitionAnnotationSegment.getEndTimeOffset(); + System.out.printf( + "\tEnd Time Offset : %s.%s\n", + logoRecognitionAnnotationSegmentEndTimeOffset.getSeconds(), + logoRecognitionAnnotationSegmentEndTimeOffset.getNanos()); + } + } + } + } +} +// [END video_detect_logo_gcs_beta] diff --git a/video/beta/src/test/java/com/example/video/DetectLogoGcsTest.java b/video/beta/src/test/java/com/example/video/DetectLogoGcsTest.java new file mode 100644 index 00000000000..41477f743e1 --- /dev/null +++ b/video/beta/src/test/java/com/example/video/DetectLogoGcsTest.java @@ -0,0 +1,51 @@ +/* + * Copyright 2020 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 java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class DetectLogoGcsTest { + + 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 testDetectFaces() throws Exception { + DetectLogoGcs.detectLogoGcs("gs://cloud-samples-data/video/googlework_short.mp4"); + String got = bout.toString(); + assertThat(got).contains("Entity Id"); + } +} diff --git a/video/beta/src/test/java/com/example/video/DetectLogoTest.java b/video/beta/src/test/java/com/example/video/DetectLogoTest.java new file mode 100644 index 00000000000..3915ae63ca7 --- /dev/null +++ b/video/beta/src/test/java/com/example/video/DetectLogoTest.java @@ -0,0 +1,51 @@ +/* + * Copyright 2020 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 java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class DetectLogoTest { + + 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 testDetectFaces() throws Exception { + DetectLogo.detectLogo("resources/googlework_short.mp4"); + String got = bout.toString(); + assertThat(got).contains("Entity Id"); + } +} From 33f573ff610ecd7549f18bf65a8c080d17c42cc1 Mon Sep 17 00:00:00 2001 From: Noah Negrey Date: Tue, 3 Mar 2020 15:23:13 -0700 Subject: [PATCH 2/3] Update DetectLogo.java --- video/beta/src/main/java/com/example/video/DetectLogo.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/video/beta/src/main/java/com/example/video/DetectLogo.java b/video/beta/src/main/java/com/example/video/DetectLogo.java index 95b81135987..d448e4a0c81 100644 --- a/video/beta/src/main/java/com/example/video/DetectLogo.java +++ b/video/beta/src/main/java/com/example/video/DetectLogo.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Google LLC + * Copyright 2020 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. From 457c4570cbd37be3fc975fdcd16a56cbead76eee Mon Sep 17 00:00:00 2001 From: Noah Negrey Date: Tue, 3 Mar 2020 15:23:28 -0700 Subject: [PATCH 3/3] Update DetectLogoGcs.java --- video/beta/src/main/java/com/example/video/DetectLogoGcs.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/video/beta/src/main/java/com/example/video/DetectLogoGcs.java b/video/beta/src/main/java/com/example/video/DetectLogoGcs.java index 81d8a8b68e9..4a1d8191272 100644 --- a/video/beta/src/main/java/com/example/video/DetectLogoGcs.java +++ b/video/beta/src/main/java/com/example/video/DetectLogoGcs.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Google LLC + * Copyright 2020 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.