Skip to content

Commit 96284ab

Browse files
author
Rebecca Taylor
committed
Merge branch 'tmp-generated-samples' of github.com:GoogleCloudPlatform/java-docs-samples into tmp-generated-samples
2 parents 78fb4c6 + d54468e commit 96284ab

File tree

3 files changed

+384
-2
lines changed

3 files changed

+384
-2
lines changed

video/beta/pom.xml

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,17 @@
4040
<dependency>
4141
<groupId>com.google.cloud</groupId>
4242
<artifactId>google-cloud-video-intelligence</artifactId>
43-
<version>0.84.0-beta</version>
43+
<version>0.102.0-beta</version>
4444
</dependency>
4545
<dependency>
4646
<groupId>com.google.cloud</groupId>
4747
<artifactId>google-cloud-storage</artifactId>
48-
<version>1.65.0</version>
48+
<version>1.84.0</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>commons-cli</groupId>
52+
<artifactId>commons-cli</artifactId>
53+
<version>1.4</version>
4954
</dependency>
5055
<!-- [END video_java_dependencies_beta] -->
5156

@@ -109,5 +114,63 @@
109114
</plugins>
110115
</build>
111116
</profile>
117+
<!-- Video Detect Logo -->
118+
<profile>
119+
<id>DetectLogoBeta</id>
120+
<activation>
121+
<property>
122+
<name>DetectLogoBeta</name>
123+
</property>
124+
</activation>
125+
<build>
126+
<plugins>
127+
<plugin>
128+
<groupId>org.codehaus.mojo</groupId>
129+
<artifactId>exec-maven-plugin</artifactId>
130+
<version>1.6.0</version>
131+
<executions>
132+
<execution>
133+
<goals>
134+
<goal>exec</goal>
135+
</goals>
136+
</execution>
137+
</executions>
138+
<configuration>
139+
<mainClass>com.example.video.VideoDetectLogoBeta</mainClass>
140+
<cleanupDaemonThreads>false</cleanupDaemonThreads>
141+
</configuration>
142+
</plugin>
143+
</plugins>
144+
</build>
145+
</profile>
146+
<!-- Video Detect Logo GCS -->
147+
<profile>
148+
<id>DetectLogoGcsBeta</id>
149+
<activation>
150+
<property>
151+
<name>DetectLogoGcsBeta</name>
152+
</property>
153+
</activation>
154+
<build>
155+
<plugins>
156+
<plugin>
157+
<groupId>org.codehaus.mojo</groupId>
158+
<artifactId>exec-maven-plugin</artifactId>
159+
<version>1.6.0</version>
160+
<executions>
161+
<execution>
162+
<goals>
163+
<goal>exec</goal>
164+
</goals>
165+
</execution>
166+
</executions>
167+
<configuration>
168+
<mainClass>com.example.video.VideoDetectLogoGcsBeta</mainClass>
169+
<cleanupDaemonThreads>false</cleanupDaemonThreads>
170+
</configuration>
171+
</plugin>
172+
</plugins>
173+
</build>
174+
</profile>
112175
</profiles>
113176
</project>
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/*
2+
* Copyright 2019 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+
* https://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+
// DO NOT EDIT! This is a generated sample ("LongRunningRequestAsync", "video_detect_logo_beta")
17+
// sample-metadata:
18+
// title:
19+
// description: Performs asynchronous video annotation for logo recognition on a local file.
20+
// usage: gradle run -PmainClass=com.google.cloud.examples.videointelligence.v1p3beta1.VideoDetectLogoBeta [--args='[--local_file_path "resources/googlework_short.mp4"]']
21+
22+
package com.example.video;
23+
24+
import com.google.cloud.videointelligence.v1p3beta1.AnnotateVideoRequest;
25+
import com.google.cloud.videointelligence.v1p3beta1.AnnotateVideoResponse;
26+
import com.google.cloud.videointelligence.v1p3beta1.DetectedAttribute;
27+
import com.google.cloud.videointelligence.v1p3beta1.Entity;
28+
import com.google.cloud.videointelligence.v1p3beta1.Feature;
29+
import com.google.cloud.videointelligence.v1p3beta1.LogoRecognitionAnnotation;
30+
import com.google.cloud.videointelligence.v1p3beta1.NormalizedBoundingBox;
31+
import com.google.cloud.videointelligence.v1p3beta1.TimestampedObject;
32+
import com.google.cloud.videointelligence.v1p3beta1.Track;
33+
import com.google.cloud.videointelligence.v1p3beta1.VideoAnnotationResults;
34+
import com.google.cloud.videointelligence.v1p3beta1.VideoIntelligenceServiceClient;
35+
import com.google.cloud.videointelligence.v1p3beta1.VideoSegment;
36+
import com.google.protobuf.ByteString;
37+
import com.google.protobuf.Duration;
38+
import java.nio.file.Files;
39+
import java.nio.file.Path;
40+
import java.nio.file.Paths;
41+
import java.util.Arrays;
42+
import java.util.List;
43+
import org.apache.commons.cli.CommandLine;
44+
import org.apache.commons.cli.DefaultParser;
45+
import org.apache.commons.cli.Option;
46+
import org.apache.commons.cli.Options;
47+
48+
public class VideoDetectLogoBeta {
49+
// [START video_detect_logo_beta]
50+
/*
51+
* Please include the following imports to run this sample.
52+
*
53+
* import com.google.cloud.videointelligence.v1p3beta1.AnnotateVideoRequest;
54+
* import com.google.cloud.videointelligence.v1p3beta1.AnnotateVideoResponse;
55+
* import com.google.cloud.videointelligence.v1p3beta1.DetectedAttribute;
56+
* import com.google.cloud.videointelligence.v1p3beta1.Entity;
57+
* import com.google.cloud.videointelligence.v1p3beta1.Feature;
58+
* import com.google.cloud.videointelligence.v1p3beta1.LogoRecognitionAnnotation;
59+
* import com.google.cloud.videointelligence.v1p3beta1.NormalizedBoundingBox;
60+
* import com.google.cloud.videointelligence.v1p3beta1.TimestampedObject;
61+
* import com.google.cloud.videointelligence.v1p3beta1.Track;
62+
* import com.google.cloud.videointelligence.v1p3beta1.VideoAnnotationResults;
63+
* import com.google.cloud.videointelligence.v1p3beta1.VideoIntelligenceServiceClient;
64+
* import com.google.cloud.videointelligence.v1p3beta1.VideoSegment;
65+
* import com.google.protobuf.ByteString;
66+
* import com.google.protobuf.Duration;
67+
* import java.nio.file.Files;
68+
* import java.nio.file.Path;
69+
* import java.nio.file.Paths;
70+
* import java.util.Arrays;
71+
* import java.util.List;
72+
*/
73+
74+
/**
75+
* Performs asynchronous video annotation for logo recognition on a local file.
76+
*
77+
* @param localFilePath Path to local video file, e.g. /path/video.mp4
78+
*/
79+
public static void sampleAnnotateVideo(String localFilePath) {
80+
try (VideoIntelligenceServiceClient videoIntelligenceServiceClient =
81+
VideoIntelligenceServiceClient.create()) {
82+
// localFilePath = "resources/googlework_short.mp4";
83+
Path path = Paths.get(localFilePath);
84+
byte[] data = Files.readAllBytes(path);
85+
ByteString inputContent = ByteString.copyFrom(data);
86+
Feature featuresElement = Feature.LOGO_RECOGNITION;
87+
List<Feature> features = Arrays.asList(featuresElement);
88+
AnnotateVideoRequest request =
89+
AnnotateVideoRequest.newBuilder()
90+
.setInputContent(inputContent)
91+
.addAllFeatures(features)
92+
.build();
93+
AnnotateVideoResponse response =
94+
videoIntelligenceServiceClient.annotateVideoAsync(request).get();
95+
// Get the first response, since we sent only one video.
96+
VideoAnnotationResults annotationResult = response.getAnnotationResultsList().get(0);
97+
// Annotations for list of logos detected, tracked and recognized in video.
98+
for (LogoRecognitionAnnotation logoRecognitionAnnotation :
99+
annotationResult.getLogoRecognitionAnnotationsList()) {
100+
Entity entity = logoRecognitionAnnotation.getEntity();
101+
// Opaque entity ID. Some IDs may be available in [Google Knowledge Graph Search API](https://developers.google.com/knowledge-graph/).
102+
System.out.printf("Entity Id : %s\n", entity.getEntityId());
103+
// Textual description, e.g. `Google`.
104+
System.out.printf("Description : %s\n", entity.getDescription());
105+
// All logo tracks where the recognized logo appears. Each track corresponds to one logo instance appearing in consecutive frames.
106+
for (Track track : logoRecognitionAnnotation.getTracksList()) {
107+
// Video segment of a track.
108+
VideoSegment segment = track.getSegment();
109+
Duration segmentStartTimeOffset = segment.getStartTimeOffset();
110+
System.out.printf(
111+
"\n\tStart Time Offset : %s.%s\n",
112+
segmentStartTimeOffset.getSeconds(), segmentStartTimeOffset.getNanos());
113+
Duration segmentEndTimeOffset = segment.getEndTimeOffset();
114+
System.out.printf(
115+
"\tEnd Time Offset : %s.%s\n",
116+
segmentEndTimeOffset.getSeconds(), segmentEndTimeOffset.getNanos());
117+
System.out.printf("\tConfidence : %s\n", track.getConfidence());
118+
// The object with timestamp and attributes per frame in the track.
119+
for (TimestampedObject timestampedObject : track.getTimestampedObjectsList()) {
120+
// Normalized Bounding box in a frame, where the object is located.
121+
NormalizedBoundingBox normalizedBoundingBox =
122+
timestampedObject.getNormalizedBoundingBox();
123+
System.out.printf("\n\t\tLeft : %s\n", normalizedBoundingBox.getLeft());
124+
System.out.printf("\t\tTop : %s\n", normalizedBoundingBox.getTop());
125+
System.out.printf("\t\tRight : %s\n", normalizedBoundingBox.getRight());
126+
System.out.printf("\t\tBottom : %s\n", normalizedBoundingBox.getBottom());
127+
// Optional. The attributes of the object in the bounding box.
128+
for (DetectedAttribute attribute : timestampedObject.getAttributesList()) {
129+
System.out.printf("\n\t\t\tName : %s\n", attribute.getName());
130+
System.out.printf("\t\t\tConfidence : %s\n", attribute.getConfidence());
131+
System.out.printf("\t\t\tValue : %s\n", attribute.getValue());
132+
}
133+
}
134+
// Optional. Attributes in the track level.
135+
for (DetectedAttribute trackAttribute : track.getAttributesList()) {
136+
System.out.printf("\n\t\tName : %s\n", trackAttribute.getName());
137+
System.out.printf("\t\tConfidence : %s\n", trackAttribute.getConfidence());
138+
System.out.printf("\t\tValue : %s\n", trackAttribute.getValue());
139+
}
140+
}
141+
// All video segments where the recognized logo appears. There might be multiple instances of the same logo class appearing in one VideoSegment.
142+
for (VideoSegment logoRecognitionAnnotationSegment :
143+
logoRecognitionAnnotation.getSegmentsList()) {
144+
Duration logoRecognitionAnnotationSegmentStartTimeOffset =
145+
logoRecognitionAnnotationSegment.getStartTimeOffset();
146+
System.out.printf(
147+
"\n\tStart Time Offset : %s.%s\n",
148+
logoRecognitionAnnotationSegmentStartTimeOffset.getSeconds(),
149+
logoRecognitionAnnotationSegmentStartTimeOffset.getNanos());
150+
Duration logoRecognitionAnnotationSegmentEndTimeOffset =
151+
logoRecognitionAnnotationSegment.getEndTimeOffset();
152+
System.out.printf(
153+
"\tEnd Time Offset : %s.%s\n",
154+
logoRecognitionAnnotationSegmentEndTimeOffset.getSeconds(),
155+
logoRecognitionAnnotationSegmentEndTimeOffset.getNanos());
156+
}
157+
}
158+
} catch (Exception exception) {
159+
System.err.println("Failed to create the client due to: " + exception);
160+
}
161+
}
162+
// [END video_detect_logo_beta]
163+
164+
public static void main(String[] args) throws Exception {
165+
Options options = new Options();
166+
options.addOption(
167+
Option.builder("").required(false).hasArg(true).longOpt("local_file_path").build());
168+
169+
CommandLine cl = (new DefaultParser()).parse(options, args);
170+
String localFilePath = cl.getOptionValue("local_file_path", "resources/googlework_short.mp4");
171+
172+
sampleAnnotateVideo(localFilePath);
173+
}
174+
}

0 commit comments

Comments
 (0)