-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Adding GA samples for Logo detection #2393
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
Merged
Merged
Changes from 20 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
b85c44e
Draft. adding GA samples for Logo detection
munkhuushmgl 865527f
Merge branch 'master' into logo-detection
munkhuushmgl 49235a0
Merge branch 'master' into logo-detection
munkhuushmgl 8f00d73
Merge branch 'master' into logo-detection
munkhuushmgl 1e3a9cc
Update video/cloud-client/src/main/java/com/example/video/LogoDetecti…
munkhuushmgl 6a9bcf2
Update video/cloud-client/src/main/java/com/example/video/LogoDetecti…
munkhuushmgl ab97a53
Update video/cloud-client/src/main/java/com/example/video/LogoDetecti…
munkhuushmgl e470cd0
made requested changes
munkhuushmgl 1412cb1
Merge branch 'master' into logo-detection
munkhuushmgl e2b736f
Update video/cloud-client/src/test/java/com/example/video/DetectLogoG…
munkhuushmgl b3fe692
Update video/cloud-client/src/test/java/com/example/video/DetectLogoT…
munkhuushmgl d493141
added spec exceptions
munkhuushmgl 2ccb0af
Merge branch 'logo-detection' of https://github.com/GoogleCloudPlatfo…
munkhuushmgl 49a6cf2
Merge branch 'master' into logo-detection
munkhuushmgl bf9073f
Update video/cloud-client/src/main/java/com/example/video/LogoDetecti…
munkhuushmgl e1a8851
Update video/cloud-client/src/main/java/com/example/video/LogoDetecti…
munkhuushmgl 4f4db50
Merge branch 'master' into logo-detection
munkhuushmgl a125bfe
Merge branch 'master' into logo-detection
nnegrey e22796e
Merge branch 'master' into logo-detection
munkhuushmgl 132e7ad
Merge branch 'master' into logo-detection
munkhuushmgl 44486c5
clean up gcs code
munkhuushmgl 82a1aa3
Merge branch 'logo-detection' of https://github.com/GoogleCloudPlatfo…
munkhuushmgl 9ef7acb
Merge branch 'master' into logo-detection
munkhuushmgl 8ffe457
Update video/cloud-client/src/main/java/com/example/video/LogoDetecti…
munkhuushmgl 317e889
Update video/cloud-client/src/main/java/com/example/video/LogoDetecti…
munkhuushmgl 1423719
Update video/cloud-client/src/main/java/com/example/video/LogoDetecti…
munkhuushmgl 9d27e97
Merge branch 'master' into logo-detection
munkhuushmgl b09b433
Merge branch 'master' into logo-detection
nnegrey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
141 changes: 141 additions & 0 deletions
141
video/cloud-client/src/main/java/com/example/video/LogoDetection.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/* | ||
* 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. | ||
* 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] | ||
|
||
import com.google.api.gax.longrunning.OperationFuture; | ||
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.DetectedAttribute; | ||
import com.google.cloud.videointelligence.v1.Entity; | ||
import com.google.cloud.videointelligence.v1.Feature; | ||
import com.google.cloud.videointelligence.v1.LogoRecognitionAnnotation; | ||
import com.google.cloud.videointelligence.v1.NormalizedBoundingBox; | ||
import com.google.cloud.videointelligence.v1.TimestampedObject; | ||
import com.google.cloud.videointelligence.v1.Track; | ||
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 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; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
public class LogoDetection { | ||
|
||
public static void detectLogo() throws Exception { | ||
munkhuushmgl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// TODO(developer): Replace these variables before running the sample. | ||
String localFilePath = "path/to/your/video.mp4"; | ||
detectLogo(localFilePath); | ||
} | ||
|
||
public static void detectLogo(String filePath) | ||
throws IOException, ExecutionException, InterruptedException, TimeoutException { | ||
// 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()) { | ||
munkhuushmgl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Read file | ||
Path path = Paths.get(filePath); | ||
byte[] data = Files.readAllBytes(path); | ||
// Create the request | ||
AnnotateVideoRequest request = | ||
AnnotateVideoRequest.newBuilder() | ||
.setInputContent(ByteString.copyFrom(data)) | ||
.addFeatures(Feature.LOGO_RECOGNITION) | ||
.build(); | ||
|
||
// asynchronously perform object tracking on videos | ||
OperationFuture<AnnotateVideoResponse, AnnotateVideoProgress> future = | ||
client.annotateVideoAsync(request); | ||
|
||
System.out.println("Waiting for operation to complete..."); | ||
// The first result is retrieved because a single video was processed. | ||
AnnotateVideoResponse response = future.get(300, TimeUnit.SECONDS); | ||
VideoAnnotationResults annotationResult = response.getAnnotationResults(0); | ||
|
||
// Annotations for list of logos detected, tracked and recognized in 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. | ||
Duration startTimeOffset = track.getSegment().getStartTimeOffset(); | ||
System.out.printf( | ||
"\n\tStart Time Offset: %s.%s\n", | ||
startTimeOffset.getSeconds(), startTimeOffset.getNanos()); | ||
Duration endTimeOffset = track.getSegment().getEndTimeOffset(); | ||
System.out.printf( | ||
"\tEnd Time Offset: %s.%s\n", endTimeOffset.getSeconds(), endTimeOffset.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 segment : logoRecognitionAnnotation.getSegmentsList()) { | ||
System.out.printf( | ||
"\n\tStart Time Offset : %s.%s\n", | ||
segment.getStartTimeOffset().getSeconds(), segment.getStartTimeOffset().getNanos()); | ||
System.out.printf( | ||
"\tEnd Time Offset : %s.%s\n", | ||
segment.getEndTimeOffset().getSeconds(), segment.getEndTimeOffset().getNanos()); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
// [END video_detect_logo] |
143 changes: 143 additions & 0 deletions
143
video/cloud-client/src/main/java/com/example/video/LogoDetectionGcs.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/* | ||
* 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. | ||
* 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] | ||
|
||
import com.google.api.gax.longrunning.OperationFuture; | ||
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.DetectedAttribute; | ||
import com.google.cloud.videointelligence.v1.Entity; | ||
import com.google.cloud.videointelligence.v1.Feature; | ||
import com.google.cloud.videointelligence.v1.LogoRecognitionAnnotation; | ||
import com.google.cloud.videointelligence.v1.NormalizedBoundingBox; | ||
import com.google.cloud.videointelligence.v1.TimestampedObject; | ||
import com.google.cloud.videointelligence.v1.Track; | ||
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.Duration; | ||
import java.io.IOException; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
public class LogoDetectionGcs { | ||
|
||
public static void detectLogoGcs() throws Exception { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String gcsUri = "gs://path/to/your/bucket/video.mp4"; | ||
munkhuushmgl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
detectLogoGcs(gcsUri); | ||
} | ||
|
||
public static void detectLogoGcs(String inputUri) | ||
throws IOException, ExecutionException, InterruptedException, TimeoutException { | ||
// 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()) { | ||
// Create the request | ||
AnnotateVideoRequest request = | ||
AnnotateVideoRequest.newBuilder() | ||
.setInputUri(inputUri) | ||
.addFeatures(Feature.LOGO_RECOGNITION) | ||
.build(); | ||
|
||
// asynchronously perform object tracking on videos | ||
OperationFuture<AnnotateVideoResponse, AnnotateVideoProgress> future = | ||
client.annotateVideoAsync(request); | ||
|
||
System.out.println("Waiting for operation to complete..."); | ||
// The first result is retrieved because a single video was processed. | ||
AnnotateVideoResponse response = future.get(300, TimeUnit.SECONDS); | ||
VideoAnnotationResults annotationResult = response.getAnnotationResults(0); | ||
|
||
// Annotations for list of logos detected, tracked and recognized in 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()); | ||
} | ||
munkhuushmgl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
} | ||
// [END video_detect_logo_gcs] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
video/cloud-client/src/test/java/com/example/video/DetectLogoGcsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* 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.IOException; | ||
import java.io.PrintStream; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeoutException; | ||
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 testLogoDetectGcs() | ||
throws IOException, ExecutionException, InterruptedException, TimeoutException { | ||
LogoDetectionGcs.detectLogoGcs("gs://cloud-samples-data/video/googlework_tiny.mp4"); | ||
String got = bout.toString(); | ||
|
||
assertThat(got).contains("Description"); | ||
assertThat(got).contains("Confidence"); | ||
assertThat(got).contains("Start Time Offset"); | ||
assertThat(got).contains("End Time Offset"); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.