Skip to content

Commit af85752

Browse files
munkhuushmglchingor13
authored andcommitted
samples: Adding GA samples for Logo detection (#2393)
video: samples for logo detection
1 parent 1c0951b commit af85752

File tree

5 files changed

+399
-7
lines changed

5 files changed

+399
-7
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
* Copyright 2020 Google LLC
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 video_detect_logo]
20+
21+
import com.google.api.gax.longrunning.OperationFuture;
22+
import com.google.cloud.videointelligence.v1.AnnotateVideoProgress;
23+
import com.google.cloud.videointelligence.v1.AnnotateVideoRequest;
24+
import com.google.cloud.videointelligence.v1.AnnotateVideoResponse;
25+
import com.google.cloud.videointelligence.v1.DetectedAttribute;
26+
import com.google.cloud.videointelligence.v1.Entity;
27+
import com.google.cloud.videointelligence.v1.Feature;
28+
import com.google.cloud.videointelligence.v1.LogoRecognitionAnnotation;
29+
import com.google.cloud.videointelligence.v1.NormalizedBoundingBox;
30+
import com.google.cloud.videointelligence.v1.TimestampedObject;
31+
import com.google.cloud.videointelligence.v1.Track;
32+
import com.google.cloud.videointelligence.v1.VideoAnnotationResults;
33+
import com.google.cloud.videointelligence.v1.VideoIntelligenceServiceClient;
34+
import com.google.cloud.videointelligence.v1.VideoSegment;
35+
import com.google.protobuf.ByteString;
36+
import com.google.protobuf.Duration;
37+
import java.io.IOException;
38+
import java.nio.file.Files;
39+
import java.nio.file.Path;
40+
import java.nio.file.Paths;
41+
import java.util.concurrent.ExecutionException;
42+
import java.util.concurrent.TimeUnit;
43+
import java.util.concurrent.TimeoutException;
44+
45+
public class LogoDetection {
46+
47+
public static void detectLogo() throws Exception {
48+
// TODO(developer): Replace these variables before running the sample.
49+
String localFilePath = "path/to/your/video.mp4";
50+
detectLogo(localFilePath);
51+
}
52+
53+
public static void detectLogo(String filePath)
54+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
55+
// Initialize client that will be used to send requests. This client only needs to be created
56+
// once, and can be reused for multiple requests. After completing all of your requests, call
57+
// the "close" method on the client to safely clean up any remaining background resources.
58+
try (VideoIntelligenceServiceClient client = VideoIntelligenceServiceClient.create()) {
59+
// Read file
60+
Path path = Paths.get(filePath);
61+
byte[] data = Files.readAllBytes(path);
62+
// Create the request
63+
AnnotateVideoRequest request =
64+
AnnotateVideoRequest.newBuilder()
65+
.setInputContent(ByteString.copyFrom(data))
66+
.addFeatures(Feature.LOGO_RECOGNITION)
67+
.build();
68+
69+
// asynchronously perform object tracking on videos
70+
OperationFuture<AnnotateVideoResponse, AnnotateVideoProgress> future =
71+
client.annotateVideoAsync(request);
72+
73+
System.out.println("Waiting for operation to complete...");
74+
// The first result is retrieved because a single video was processed.
75+
AnnotateVideoResponse response = future.get(300, TimeUnit.SECONDS);
76+
VideoAnnotationResults annotationResult = response.getAnnotationResults(0);
77+
78+
// Annotations for list of logos detected, tracked and recognized in video.
79+
for (LogoRecognitionAnnotation logoRecognitionAnnotation :
80+
annotationResult.getLogoRecognitionAnnotationsList()) {
81+
Entity entity = logoRecognitionAnnotation.getEntity();
82+
// Opaque entity ID. Some IDs may be available in
83+
// [Google Knowledge Graph Search API](https://developers.google.com/knowledge-graph/).
84+
System.out.printf("Entity Id : %s\n", entity.getEntityId());
85+
System.out.printf("Description : %s\n", entity.getDescription());
86+
// All logo tracks where the recognized logo appears. Each track corresponds to one logo
87+
// instance appearing in consecutive frames.
88+
for (Track track : logoRecognitionAnnotation.getTracksList()) {
89+
90+
// Video segment of a track.
91+
Duration startTimeOffset = track.getSegment().getStartTimeOffset();
92+
System.out.printf(
93+
"\n\tStart Time Offset: %s.%s\n",
94+
startTimeOffset.getSeconds(), startTimeOffset.getNanos());
95+
Duration endTimeOffset = track.getSegment().getEndTimeOffset();
96+
System.out.printf(
97+
"\tEnd Time Offset: %s.%s\n", endTimeOffset.getSeconds(), endTimeOffset.getNanos());
98+
System.out.printf("\tConfidence: %s\n", track.getConfidence());
99+
100+
// The object with timestamp and attributes per frame in the track.
101+
for (TimestampedObject timestampedObject : track.getTimestampedObjectsList()) {
102+
103+
// Normalized Bounding box in a frame, where the object is located.
104+
NormalizedBoundingBox normalizedBoundingBox =
105+
timestampedObject.getNormalizedBoundingBox();
106+
System.out.printf("\n\t\tLeft: %s\n", normalizedBoundingBox.getLeft());
107+
System.out.printf("\t\tTop: %s\n", normalizedBoundingBox.getTop());
108+
System.out.printf("\t\tRight: %s\n", normalizedBoundingBox.getRight());
109+
System.out.printf("\t\tBottom: %s\n", normalizedBoundingBox.getBottom());
110+
111+
// Optional. The attributes of the object in the bounding box.
112+
for (DetectedAttribute attribute : timestampedObject.getAttributesList()) {
113+
System.out.printf("\n\t\t\tName: %s\n", attribute.getName());
114+
System.out.printf("\t\t\tConfidence: %s\n", attribute.getConfidence());
115+
System.out.printf("\t\t\tValue: %s\n", attribute.getValue());
116+
}
117+
}
118+
119+
// Optional. Attributes in the track level.
120+
for (DetectedAttribute trackAttribute : track.getAttributesList()) {
121+
System.out.printf("\n\t\tName : %s\n", trackAttribute.getName());
122+
System.out.printf("\t\tConfidence : %s\n", trackAttribute.getConfidence());
123+
System.out.printf("\t\tValue : %s\n", trackAttribute.getValue());
124+
}
125+
}
126+
127+
// All video segments where the recognized logo appears. There might be multiple instances
128+
// of the same logo class appearing in one VideoSegment.
129+
for (VideoSegment segment : logoRecognitionAnnotation.getSegmentsList()) {
130+
System.out.printf(
131+
"\n\tStart Time Offset : %s.%s\n",
132+
segment.getStartTimeOffset().getSeconds(), segment.getStartTimeOffset().getNanos());
133+
System.out.printf(
134+
"\tEnd Time Offset : %s.%s\n",
135+
segment.getEndTimeOffset().getSeconds(), segment.getEndTimeOffset().getNanos());
136+
}
137+
}
138+
}
139+
}
140+
}
141+
// [END video_detect_logo]
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* Copyright 2020 Google LLC
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 video_detect_logo_gcs]
20+
21+
import com.google.api.gax.longrunning.OperationFuture;
22+
import com.google.cloud.videointelligence.v1.AnnotateVideoProgress;
23+
import com.google.cloud.videointelligence.v1.AnnotateVideoRequest;
24+
import com.google.cloud.videointelligence.v1.AnnotateVideoResponse;
25+
import com.google.cloud.videointelligence.v1.DetectedAttribute;
26+
import com.google.cloud.videointelligence.v1.Entity;
27+
import com.google.cloud.videointelligence.v1.Feature;
28+
import com.google.cloud.videointelligence.v1.LogoRecognitionAnnotation;
29+
import com.google.cloud.videointelligence.v1.NormalizedBoundingBox;
30+
import com.google.cloud.videointelligence.v1.TimestampedObject;
31+
import com.google.cloud.videointelligence.v1.Track;
32+
import com.google.cloud.videointelligence.v1.VideoAnnotationResults;
33+
import com.google.cloud.videointelligence.v1.VideoIntelligenceServiceClient;
34+
import com.google.cloud.videointelligence.v1.VideoSegment;
35+
import com.google.protobuf.Duration;
36+
import java.io.IOException;
37+
import java.util.concurrent.ExecutionException;
38+
import java.util.concurrent.TimeUnit;
39+
import java.util.concurrent.TimeoutException;
40+
41+
public class LogoDetectionGcs {
42+
43+
public static void detectLogoGcs() throws Exception {
44+
// TODO(developer): Replace these variables before running the sample.
45+
String gcsUri = "gs://YOUR_BUCKET_ID/path/to/your/video.mp4";
46+
detectLogoGcs(gcsUri);
47+
}
48+
49+
public static void detectLogoGcs(String inputUri)
50+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
51+
// Initialize client that will be used to send requests. This client only needs to be created
52+
// once, and can be reused for multiple requests. After completing all of your requests, call
53+
// the "close" method on the client to safely clean up any remaining background resources.
54+
try (VideoIntelligenceServiceClient client = VideoIntelligenceServiceClient.create()) {
55+
// Create the request
56+
AnnotateVideoRequest request =
57+
AnnotateVideoRequest.newBuilder()
58+
.setInputUri(inputUri)
59+
.addFeatures(Feature.LOGO_RECOGNITION)
60+
.build();
61+
62+
// asynchronously perform object tracking on videos
63+
OperationFuture<AnnotateVideoResponse, AnnotateVideoProgress> future =
64+
client.annotateVideoAsync(request);
65+
66+
System.out.println("Waiting for operation to complete...");
67+
// The first result is retrieved because a single video was processed.
68+
AnnotateVideoResponse response = future.get(300, TimeUnit.SECONDS);
69+
VideoAnnotationResults annotationResult = response.getAnnotationResults(0);
70+
71+
// Annotations for list of logos detected, tracked and recognized in video.
72+
for (LogoRecognitionAnnotation logoRecognitionAnnotation :
73+
annotationResult.getLogoRecognitionAnnotationsList()) {
74+
Entity entity = logoRecognitionAnnotation.getEntity();
75+
// Opaque entity ID. Some IDs may be available in
76+
// [Google Knowledge Graph Search API](https://developers.google.com/knowledge-graph/).
77+
System.out.printf("Entity Id : %s\n", entity.getEntityId());
78+
System.out.printf("Description : %s\n", entity.getDescription());
79+
// All logo tracks where the recognized logo appears. Each track corresponds to one logo
80+
// instance appearing in consecutive frames.
81+
for (Track track : logoRecognitionAnnotation.getTracksList()) {
82+
83+
// Video segment of a track.
84+
Duration startTimeOffset = track.getSegment().getStartTimeOffset();
85+
System.out.printf(
86+
"\n\tStart Time Offset: %s.%s\n",
87+
startTimeOffset.getSeconds(), startTimeOffset.getNanos());
88+
Duration endTimeOffset = track.getSegment().getEndTimeOffset();
89+
System.out.printf(
90+
"\tEnd Time Offset: %s.%s\n", endTimeOffset.getSeconds(), endTimeOffset.getNanos());
91+
System.out.printf("\tConfidence: %s\n", track.getConfidence());
92+
93+
// The object with timestamp and attributes per frame in the track.
94+
for (TimestampedObject timestampedObject : track.getTimestampedObjectsList()) {
95+
96+
// Normalized Bounding box in a frame, where the object is located.
97+
NormalizedBoundingBox normalizedBoundingBox =
98+
timestampedObject.getNormalizedBoundingBox();
99+
System.out.printf("\n\t\tLeft: %s\n", normalizedBoundingBox.getLeft());
100+
System.out.printf("\t\tTop: %s\n", normalizedBoundingBox.getTop());
101+
System.out.printf("\t\tRight: %s\n", normalizedBoundingBox.getRight());
102+
System.out.printf("\t\tBottom: %s\n", normalizedBoundingBox.getBottom());
103+
104+
// Optional. The attributes of the object in the bounding box.
105+
for (DetectedAttribute attribute : timestampedObject.getAttributesList()) {
106+
System.out.printf("\n\t\t\tName: %s\n", attribute.getName());
107+
System.out.printf("\t\t\tConfidence: %s\n", attribute.getConfidence());
108+
System.out.printf("\t\t\tValue: %s\n", attribute.getValue());
109+
}
110+
}
111+
112+
// Optional. Attributes in the track level.
113+
for (DetectedAttribute trackAttribute : track.getAttributesList()) {
114+
System.out.printf("\n\t\tName : %s\n", trackAttribute.getName());
115+
System.out.printf("\t\tConfidence : %s\n", trackAttribute.getConfidence());
116+
System.out.printf("\t\tValue : %s\n", trackAttribute.getValue());
117+
}
118+
}
119+
120+
// All video segments where the recognized logo appears. There might be multiple instances
121+
// of the same logo class appearing in one VideoSegment.
122+
for (VideoSegment segment : logoRecognitionAnnotation.getSegmentsList()) {
123+
System.out.printf(
124+
"\n\tStart Time Offset : %s.%s\n",
125+
segment.getStartTimeOffset().getSeconds(), segment.getStartTimeOffset().getNanos());
126+
System.out.printf(
127+
"\tEnd Time Offset : %s.%s\n",
128+
segment.getEndTimeOffset().getSeconds(), segment.getEndTimeOffset().getNanos());
129+
}
130+
}
131+
}
132+
}
133+
}
134+
// [END video_detect_logo_gcs]

video/src/test/java/com/example/video/DetectIT.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,35 @@
2121
import com.google.cloud.videointelligence.v1.ObjectTrackingAnnotation;
2222
import com.google.cloud.videointelligence.v1.TextAnnotation;
2323
import com.google.cloud.videointelligence.v1.VideoAnnotationResults;
24+
2425
import java.io.ByteArrayOutputStream;
2526
import java.io.PrintStream;
2627
import java.util.Arrays;
2728
import java.util.List;
29+
2830
import org.junit.After;
2931
import org.junit.Before;
3032
import org.junit.Test;
3133
import org.junit.runner.RunWith;
3234
import org.junit.runners.JUnit4;
3335

34-
/** Tests for video analysis sample. */
36+
/**
37+
* Tests for video analysis sample.
38+
*/
3539
@RunWith(JUnit4.class)
3640
@SuppressWarnings("checkstyle:abbreviationaswordinname")
3741
public class DetectIT {
38-
private ByteArrayOutputStream bout;
39-
private PrintStream out;
40-
4142
static final String LABEL_GCS_LOCATION = "gs://cloud-samples-data/video/cat.mp4";
4243
static final String LABEL_FILE_LOCATION = "./resources/cat.mp4";
4344
static final String SHOTS_FILE_LOCATION = "gs://cloud-samples-data/video/gbikes_dinosaur.mp4";
44-
static final String EXPLICIT_CONTENT_LOCATION = "gs://cloud-samples-data/video/cat.mp4";
45+
static final String EXPLICIT_CONTENT_LOCATION = "gs://cloud-samples-data/video/cat.mp4";
4546
static final String SPEECH_GCS_LOCATION =
4647
"gs://java-docs-samples-testing/video/googlework_short.mp4";
4748
private static final List<String> POSSIBLE_TEXTS = Arrays.asList(
48-
"Google", "SUR", "SUR", "ROTO", "Vice President", "58oo9", "LONDRES", "OMAR", "PARIS",
49-
"METRO", "RUE", "CARLO");
49+
"Google", "SUR", "SUR", "ROTO", "Vice President", "58oo9", "LONDRES", "OMAR", "PARIS",
50+
"METRO", "RUE", "CARLO");
51+
private ByteArrayOutputStream bout;
52+
private PrintStream out;
5053

5154
@Before
5255
public void setUp() {
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2020 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+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.IOException;
23+
import java.io.PrintStream;
24+
import java.util.concurrent.ExecutionException;
25+
import java.util.concurrent.TimeoutException;
26+
import org.junit.After;
27+
import org.junit.Before;
28+
import org.junit.Test;
29+
30+
public class DetectLogoGcsTest {
31+
private ByteArrayOutputStream bout;
32+
private PrintStream out;
33+
34+
@Before
35+
public void setUp() {
36+
bout = new ByteArrayOutputStream();
37+
out = new PrintStream(bout);
38+
System.setOut(out);
39+
}
40+
41+
@After
42+
public void tearDown() {
43+
System.setOut(null);
44+
}
45+
46+
@Test
47+
public void testLogoDetectGcs()
48+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
49+
LogoDetectionGcs.detectLogoGcs("gs://cloud-samples-data/video/googlework_tiny.mp4");
50+
String got = bout.toString();
51+
52+
assertThat(got).contains("Description");
53+
assertThat(got).contains("Confidence");
54+
assertThat(got).contains("Start Time Offset");
55+
assertThat(got).contains("End Time Offset");
56+
}
57+
}

0 commit comments

Comments
 (0)