Skip to content

Commit 40b41c3

Browse files
nnegreychingor13
authored andcommitted
samples: video: move samples out of branch (#2300)
* video: move samples out of branch * Update DetectLogo.java * Update DetectLogoGcs.java
1 parent 79738ef commit 40b41c3

File tree

4 files changed

+387
-0
lines changed

4 files changed

+387
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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_beta]
20+
import com.google.cloud.videointelligence.v1p3beta1.AnnotateVideoRequest;
21+
import com.google.cloud.videointelligence.v1p3beta1.AnnotateVideoResponse;
22+
import com.google.cloud.videointelligence.v1p3beta1.DetectedAttribute;
23+
import com.google.cloud.videointelligence.v1p3beta1.Entity;
24+
import com.google.cloud.videointelligence.v1p3beta1.Feature;
25+
import com.google.cloud.videointelligence.v1p3beta1.LogoRecognitionAnnotation;
26+
import com.google.cloud.videointelligence.v1p3beta1.NormalizedBoundingBox;
27+
import com.google.cloud.videointelligence.v1p3beta1.TimestampedObject;
28+
import com.google.cloud.videointelligence.v1p3beta1.Track;
29+
import com.google.cloud.videointelligence.v1p3beta1.VideoAnnotationResults;
30+
import com.google.cloud.videointelligence.v1p3beta1.VideoIntelligenceServiceClient;
31+
import com.google.cloud.videointelligence.v1p3beta1.VideoSegment;
32+
import com.google.protobuf.ByteString;
33+
import com.google.protobuf.Duration;
34+
35+
import java.io.IOException;
36+
import java.nio.file.Files;
37+
import java.nio.file.Path;
38+
import java.nio.file.Paths;
39+
40+
import java.util.concurrent.ExecutionException;
41+
42+
public class DetectLogo {
43+
44+
public void detectLogo() throws IOException, ExecutionException, InterruptedException {
45+
String filePath = "path/to/your/video.mp4";
46+
detectLogo(filePath);
47+
}
48+
49+
public static void detectLogo(String localFilePath)
50+
throws IOException, ExecutionException, InterruptedException {
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+
// Read the files contents
56+
Path path = Paths.get(localFilePath);
57+
byte[] data = Files.readAllBytes(path);
58+
ByteString inputContent = ByteString.copyFrom(data);
59+
60+
// Build the request with the inputContent and set the Feature
61+
AnnotateVideoRequest request =
62+
AnnotateVideoRequest.newBuilder()
63+
.setInputContent(inputContent)
64+
.addFeatures(Feature.LOGO_RECOGNITION)
65+
.build();
66+
67+
// Make the asynchronous request
68+
AnnotateVideoResponse response = client.annotateVideoAsync(request).get();
69+
70+
// Get the first response, since we sent only one video.
71+
VideoAnnotationResults annotationResult = response.getAnnotationResultsList().get(0);
72+
73+
// Annotations for list of logos detected, tracked and recognized in the video.
74+
for (LogoRecognitionAnnotation logoRecognitionAnnotation :
75+
annotationResult.getLogoRecognitionAnnotationsList()) {
76+
77+
Entity entity = logoRecognitionAnnotation.getEntity();
78+
// Opaque entity ID. Some IDs may be available in [Google Knowledge Graph Search
79+
// API](https://developers.google.com/knowledge-graph/).
80+
System.out.printf("Entity Id: %s\n", entity.getEntityId());
81+
System.out.printf("Description: %s\n", entity.getDescription());
82+
83+
// All logo tracks where the recognized logo appears. Each track corresponds to one logo
84+
// instance appearing in consecutive frames.
85+
for (Track track : logoRecognitionAnnotation.getTracksList()) {
86+
87+
// Video segment of a track.
88+
VideoSegment segment = track.getSegment();
89+
Duration segmentStartTimeOffset = segment.getStartTimeOffset();
90+
System.out.printf(
91+
"\n\tStart Time Offset: %s.%s\n",
92+
segmentStartTimeOffset.getSeconds(), segmentStartTimeOffset.getNanos());
93+
Duration segmentEndTimeOffset = segment.getEndTimeOffset();
94+
System.out.printf(
95+
"\tEnd Time Offset: %s.%s\n",
96+
segmentEndTimeOffset.getSeconds(), segmentEndTimeOffset.getNanos());
97+
System.out.printf("\tConfidence: %s\n", track.getConfidence());
98+
99+
// The object with timestamp and attributes per frame in the track.
100+
for (TimestampedObject timestampedObject : track.getTimestampedObjectsList()) {
101+
102+
// Normalized Bounding box in a frame, where the object is located.
103+
NormalizedBoundingBox normalizedBoundingBox =
104+
timestampedObject.getNormalizedBoundingBox();
105+
System.out.printf("\n\t\tLeft: %s\n", normalizedBoundingBox.getLeft());
106+
System.out.printf("\t\tTop: %s\n", normalizedBoundingBox.getTop());
107+
System.out.printf("\t\tRight: %s\n", normalizedBoundingBox.getRight());
108+
System.out.printf("\t\tBottom: %s\n", normalizedBoundingBox.getBottom());
109+
110+
// Optional. The attributes of the object in the bounding box.
111+
for (DetectedAttribute attribute : timestampedObject.getAttributesList()) {
112+
System.out.printf("\n\t\t\tName: %s\n", attribute.getName());
113+
System.out.printf("\t\t\tConfidence: %s\n", attribute.getConfidence());
114+
System.out.printf("\t\t\tValue: %s\n", attribute.getValue());
115+
}
116+
}
117+
118+
// Optional. Attributes in the track level.
119+
for (DetectedAttribute trackAttribute : track.getAttributesList()) {
120+
System.out.printf("\n\t\tName : %s\n", trackAttribute.getName());
121+
System.out.printf("\t\tConfidence : %s\n", trackAttribute.getConfidence());
122+
System.out.printf("\t\tValue : %s\n", trackAttribute.getValue());
123+
}
124+
}
125+
126+
// All video segments where the recognized logo appears. There might be multiple instances
127+
// of the same logo class appearing in one VideoSegment.
128+
for (VideoSegment logoRecognitionAnnotationSegment :
129+
logoRecognitionAnnotation.getSegmentsList()) {
130+
Duration logoRecognitionAnnotationSegmentStartTimeOffset =
131+
logoRecognitionAnnotationSegment.getStartTimeOffset();
132+
System.out.printf(
133+
"\n\tStart Time Offset : %s.%s\n",
134+
logoRecognitionAnnotationSegmentStartTimeOffset.getSeconds(),
135+
logoRecognitionAnnotationSegmentStartTimeOffset.getNanos());
136+
Duration logoRecognitionAnnotationSegmentEndTimeOffset =
137+
logoRecognitionAnnotationSegment.getEndTimeOffset();
138+
System.out.printf(
139+
"\tEnd Time Offset : %s.%s\n",
140+
logoRecognitionAnnotationSegmentEndTimeOffset.getSeconds(),
141+
logoRecognitionAnnotationSegmentEndTimeOffset.getNanos());
142+
}
143+
}
144+
}
145+
}
146+
}
147+
// [END video_detect_logo_beta]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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_beta]
20+
import com.google.cloud.videointelligence.v1p3beta1.AnnotateVideoRequest;
21+
import com.google.cloud.videointelligence.v1p3beta1.AnnotateVideoResponse;
22+
import com.google.cloud.videointelligence.v1p3beta1.DetectedAttribute;
23+
import com.google.cloud.videointelligence.v1p3beta1.Entity;
24+
import com.google.cloud.videointelligence.v1p3beta1.Feature;
25+
import com.google.cloud.videointelligence.v1p3beta1.LogoRecognitionAnnotation;
26+
import com.google.cloud.videointelligence.v1p3beta1.NormalizedBoundingBox;
27+
import com.google.cloud.videointelligence.v1p3beta1.TimestampedObject;
28+
import com.google.cloud.videointelligence.v1p3beta1.Track;
29+
import com.google.cloud.videointelligence.v1p3beta1.VideoAnnotationResults;
30+
import com.google.cloud.videointelligence.v1p3beta1.VideoIntelligenceServiceClient;
31+
import com.google.cloud.videointelligence.v1p3beta1.VideoSegment;
32+
import com.google.protobuf.Duration;
33+
34+
import java.io.IOException;
35+
36+
import java.util.concurrent.ExecutionException;
37+
38+
public class DetectLogoGcs {
39+
40+
public void detectLogo() throws IOException, ExecutionException, InterruptedException {
41+
String inputUri = "gs://cloud-samples-data/video/googlework_short.mp4";
42+
detectLogoGcs(inputUri);
43+
}
44+
45+
public static void detectLogoGcs(String inputUri)
46+
throws IOException, ExecutionException, InterruptedException {
47+
// Initialize client that will be used to send requests. This client only needs to be created
48+
// once, and can be reused for multiple requests. After completing all of your requests, call
49+
// the "close" method on the client to safely clean up any remaining background resources.
50+
try (VideoIntelligenceServiceClient client = VideoIntelligenceServiceClient.create()) {
51+
// Build the request with the inputUri and set the Feature
52+
AnnotateVideoRequest request =
53+
AnnotateVideoRequest.newBuilder()
54+
.setInputUri(inputUri)
55+
.addFeatures(Feature.LOGO_RECOGNITION)
56+
.build();
57+
58+
// Make the asynchronous request
59+
AnnotateVideoResponse response = client.annotateVideoAsync(request).get();
60+
61+
// Get the first response, since we sent only one video.
62+
VideoAnnotationResults annotationResult = response.getAnnotationResultsList().get(0);
63+
64+
// Annotations for list of logos detected, tracked and recognized in the video.
65+
for (LogoRecognitionAnnotation logoRecognitionAnnotation :
66+
annotationResult.getLogoRecognitionAnnotationsList()) {
67+
68+
Entity entity = logoRecognitionAnnotation.getEntity();
69+
// Opaque entity ID. Some IDs may be available in [Google Knowledge Graph Search
70+
// API](https://developers.google.com/knowledge-graph/).
71+
System.out.printf("Entity Id: %s\n", entity.getEntityId());
72+
System.out.printf("Description: %s\n", entity.getDescription());
73+
74+
// All logo tracks where the recognized logo appears. Each track corresponds to one logo
75+
// instance appearing in consecutive frames.
76+
for (Track track : logoRecognitionAnnotation.getTracksList()) {
77+
78+
// Video segment of a track.
79+
VideoSegment segment = track.getSegment();
80+
Duration segmentStartTimeOffset = segment.getStartTimeOffset();
81+
System.out.printf(
82+
"\n\tStart Time Offset: %s.%s\n",
83+
segmentStartTimeOffset.getSeconds(), segmentStartTimeOffset.getNanos());
84+
Duration segmentEndTimeOffset = segment.getEndTimeOffset();
85+
System.out.printf(
86+
"\tEnd Time Offset: %s.%s\n",
87+
segmentEndTimeOffset.getSeconds(), segmentEndTimeOffset.getNanos());
88+
System.out.printf("\tConfidence: %s\n", track.getConfidence());
89+
90+
// The object with timestamp and attributes per frame in the track.
91+
for (TimestampedObject timestampedObject : track.getTimestampedObjectsList()) {
92+
93+
// Normalized Bounding box in a frame, where the object is located.
94+
NormalizedBoundingBox normalizedBoundingBox =
95+
timestampedObject.getNormalizedBoundingBox();
96+
System.out.printf("\n\t\tLeft: %s\n", normalizedBoundingBox.getLeft());
97+
System.out.printf("\t\tTop: %s\n", normalizedBoundingBox.getTop());
98+
System.out.printf("\t\tRight: %s\n", normalizedBoundingBox.getRight());
99+
System.out.printf("\t\tBottom: %s\n", normalizedBoundingBox.getBottom());
100+
101+
// Optional. The attributes of the object in the bounding box.
102+
for (DetectedAttribute attribute : timestampedObject.getAttributesList()) {
103+
System.out.printf("\n\t\t\tName: %s\n", attribute.getName());
104+
System.out.printf("\t\t\tConfidence: %s\n", attribute.getConfidence());
105+
System.out.printf("\t\t\tValue: %s\n", attribute.getValue());
106+
}
107+
}
108+
109+
// Optional. Attributes in the track level.
110+
for (DetectedAttribute trackAttribute : track.getAttributesList()) {
111+
System.out.printf("\n\t\tName : %s\n", trackAttribute.getName());
112+
System.out.printf("\t\tConfidence : %s\n", trackAttribute.getConfidence());
113+
System.out.printf("\t\tValue : %s\n", trackAttribute.getValue());
114+
}
115+
}
116+
117+
// All video segments where the recognized logo appears. There might be multiple instances
118+
// of the same logo class appearing in one VideoSegment.
119+
for (VideoSegment logoRecognitionAnnotationSegment :
120+
logoRecognitionAnnotation.getSegmentsList()) {
121+
Duration logoRecognitionAnnotationSegmentStartTimeOffset =
122+
logoRecognitionAnnotationSegment.getStartTimeOffset();
123+
System.out.printf(
124+
"\n\tStart Time Offset : %s.%s\n",
125+
logoRecognitionAnnotationSegmentStartTimeOffset.getSeconds(),
126+
logoRecognitionAnnotationSegmentStartTimeOffset.getNanos());
127+
Duration logoRecognitionAnnotationSegmentEndTimeOffset =
128+
logoRecognitionAnnotationSegment.getEndTimeOffset();
129+
System.out.printf(
130+
"\tEnd Time Offset : %s.%s\n",
131+
logoRecognitionAnnotationSegmentEndTimeOffset.getSeconds(),
132+
logoRecognitionAnnotationSegmentEndTimeOffset.getNanos());
133+
}
134+
}
135+
}
136+
}
137+
}
138+
// [END video_detect_logo_gcs_beta]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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.PrintStream;
23+
24+
import org.junit.After;
25+
import org.junit.Before;
26+
import org.junit.Test;
27+
28+
public class DetectLogoGcsTest {
29+
30+
private ByteArrayOutputStream bout;
31+
private PrintStream out;
32+
33+
@Before
34+
public void setUp() {
35+
bout = new ByteArrayOutputStream();
36+
out = new PrintStream(bout);
37+
System.setOut(out);
38+
}
39+
40+
@After
41+
public void tearDown() {
42+
System.setOut(null);
43+
}
44+
45+
@Test
46+
public void testDetectFaces() throws Exception {
47+
DetectLogoGcs.detectLogoGcs("gs://cloud-samples-data/video/googlework_short.mp4");
48+
String got = bout.toString();
49+
assertThat(got).contains("Entity Id");
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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.PrintStream;
23+
24+
import org.junit.After;
25+
import org.junit.Before;
26+
import org.junit.Test;
27+
28+
public class DetectLogoTest {
29+
30+
private ByteArrayOutputStream bout;
31+
private PrintStream out;
32+
33+
@Before
34+
public void setUp() {
35+
bout = new ByteArrayOutputStream();
36+
out = new PrintStream(bout);
37+
System.setOut(out);
38+
}
39+
40+
@After
41+
public void tearDown() {
42+
System.setOut(null);
43+
}
44+
45+
@Test
46+
public void testDetectFaces() throws Exception {
47+
DetectLogo.detectLogo("resources/googlework_short.mp4");
48+
String got = bout.toString();
49+
assertThat(got).contains("Entity Id");
50+
}
51+
}

0 commit comments

Comments
 (0)