Skip to content

Commit dbe2bdc

Browse files
authored
samples: moving beta samples from java-docs and refactored GA samples (#299)
* samples: moving beta samples from java-docs and refactored GA samples * renamed beta package and refactored tests
1 parent 7dc8566 commit dbe2bdc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1900
-267
lines changed

video/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
<groupId>com.google.cloud</groupId>
4343
<artifactId>google-cloud-video-intelligence</artifactId>
4444
</dependency>
45+
<dependency>
46+
<groupId>com.google.cloud</groupId>
47+
<artifactId>google-cloud-storage</artifactId>
48+
</dependency>
4549
<!-- [END videointelligence_install_with_bom] -->
4650

4751
<dependency>
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* Copyright 2018 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 beta.video;
18+
19+
import com.google.api.gax.longrunning.OperationFuture;
20+
import com.google.cloud.videointelligence.v1p1beta1.AnnotateVideoProgress;
21+
import com.google.cloud.videointelligence.v1p1beta1.AnnotateVideoRequest;
22+
import com.google.cloud.videointelligence.v1p1beta1.AnnotateVideoResponse;
23+
import com.google.cloud.videointelligence.v1p1beta1.Feature;
24+
import com.google.cloud.videointelligence.v1p1beta1.SpeechRecognitionAlternative;
25+
import com.google.cloud.videointelligence.v1p1beta1.SpeechTranscription;
26+
import com.google.cloud.videointelligence.v1p1beta1.SpeechTranscriptionConfig;
27+
import com.google.cloud.videointelligence.v1p1beta1.VideoAnnotationResults;
28+
import com.google.cloud.videointelligence.v1p1beta1.VideoContext;
29+
import com.google.cloud.videointelligence.v1p1beta1.VideoIntelligenceServiceClient;
30+
import com.google.cloud.videointelligence.v1p1beta1.WordInfo;
31+
import java.io.IOException;
32+
import java.util.concurrent.TimeUnit;
33+
34+
public class Detect {
35+
/**
36+
* Detects video transcription using the Video Intelligence API
37+
*
38+
* @param args specifies features to detect and the path to the video on Google Cloud Storage.
39+
*/
40+
public static void main(String[] args) {
41+
try {
42+
argsHelper(args);
43+
} catch (Exception e) {
44+
System.out.println("Exception while running:\n" + e.getMessage() + "\n");
45+
e.printStackTrace(System.out);
46+
}
47+
}
48+
49+
/**
50+
* Helper that handles the input passed to the program.
51+
*
52+
* @param args specifies features to detect and the path to the video on Google Cloud Storage.
53+
* @throws IOException on Input/Output errors.
54+
*/
55+
public static void argsHelper(String[] args) throws Exception {
56+
if (args.length < 1) {
57+
System.out.println("Usage:");
58+
System.out.printf(
59+
"\tjava %s \"<command>\" \"<path-to-video>\"\n"
60+
+ "Commands:\n"
61+
+ "\tspeech-transcription\n"
62+
+ "Path:\n\tA URI for a Cloud Storage resource (gs://...)\n"
63+
+ "Examples: ",
64+
Detect.class.getCanonicalName());
65+
return;
66+
}
67+
String command = args[0];
68+
String path = args.length > 1 ? args[1] : "";
69+
70+
if (command.equals("speech-transcription")) {
71+
speechTranscription(path);
72+
}
73+
}
74+
75+
// [START video_speech_transcription_gcs_beta]
76+
/**
77+
* Transcribe speech from a video stored on GCS.
78+
*
79+
* @param gcsUri the path to the video file to analyze.
80+
*/
81+
public static void speechTranscription(String gcsUri) throws Exception {
82+
// Instantiate a com.google.cloud.videointelligence.v1p1beta1.VideoIntelligenceServiceClient
83+
try (VideoIntelligenceServiceClient client = VideoIntelligenceServiceClient.create()) {
84+
// Set the language code
85+
SpeechTranscriptionConfig config =
86+
SpeechTranscriptionConfig.newBuilder()
87+
.setLanguageCode("en-US")
88+
.setEnableAutomaticPunctuation(true)
89+
.build();
90+
91+
// Set the video context with the above configuration
92+
VideoContext context = VideoContext.newBuilder().setSpeechTranscriptionConfig(config).build();
93+
94+
// Create the request
95+
AnnotateVideoRequest request =
96+
AnnotateVideoRequest.newBuilder()
97+
.setInputUri(gcsUri)
98+
.addFeatures(Feature.SPEECH_TRANSCRIPTION)
99+
.setVideoContext(context)
100+
.build();
101+
102+
// asynchronously perform speech transcription on videos
103+
OperationFuture<AnnotateVideoResponse, AnnotateVideoProgress> response =
104+
client.annotateVideoAsync(request);
105+
106+
System.out.println("Waiting for operation to complete...");
107+
// Display the results
108+
for (VideoAnnotationResults results :
109+
response.get(300, TimeUnit.SECONDS).getAnnotationResultsList()) {
110+
for (SpeechTranscription speechTranscription : results.getSpeechTranscriptionsList()) {
111+
try {
112+
// Print the transcription
113+
if (speechTranscription.getAlternativesCount() > 0) {
114+
SpeechRecognitionAlternative alternative = speechTranscription.getAlternatives(0);
115+
116+
System.out.printf("Transcript: %s\n", alternative.getTranscript());
117+
System.out.printf("Confidence: %.2f\n", alternative.getConfidence());
118+
119+
System.out.println("Word level information:");
120+
for (WordInfo wordInfo : alternative.getWordsList()) {
121+
double startTime =
122+
wordInfo.getStartTime().getSeconds() + wordInfo.getStartTime().getNanos() / 1e9;
123+
double endTime =
124+
wordInfo.getEndTime().getSeconds() + wordInfo.getEndTime().getNanos() / 1e9;
125+
System.out.printf(
126+
"\t%4.2fs - %4.2fs: %s\n", startTime, endTime, wordInfo.getWord());
127+
}
128+
} else {
129+
System.out.println("No transcription found");
130+
}
131+
} catch (IndexOutOfBoundsException ioe) {
132+
System.out.println("Could not retrieve frame: " + ioe.getMessage());
133+
}
134+
}
135+
}
136+
}
137+
}
138+
// [END video_speech_transcription_gcs_beta]
139+
}

video/src/main/java/com/example/video/DetectFaces.java renamed to video/src/main/java/beta/video/DetectFaces.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.example.video;
17+
package beta.video;
1818

1919
// [START video_detect_faces_beta]
2020

video/src/main/java/com/example/video/DetectFacesGcs.java renamed to video/src/main/java/beta/video/DetectFacesGcs.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.example.video;
17+
package beta.video;
1818

1919
// [START video_detect_faces_gcs_beta]
2020

video/src/main/java/com/example/video/DetectLogo.java renamed to video/src/main/java/beta/video/DetectLogo.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.example.video;
17+
package beta.video;
1818

1919
// [START video_detect_logo_beta]
20+
2021
import com.google.cloud.videointelligence.v1p3beta1.AnnotateVideoRequest;
2122
import com.google.cloud.videointelligence.v1p3beta1.AnnotateVideoResponse;
2223
import com.google.cloud.videointelligence.v1p3beta1.DetectedAttribute;

video/src/main/java/com/example/video/DetectLogoGcs.java renamed to video/src/main/java/beta/video/DetectLogoGcs.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.example.video;
17+
package beta.video;
1818

1919
// [START video_detect_logo_gcs_beta]
20+
2021
import com.google.cloud.videointelligence.v1p3beta1.AnnotateVideoRequest;
2122
import com.google.cloud.videointelligence.v1p3beta1.AnnotateVideoResponse;
2223
import com.google.cloud.videointelligence.v1p3beta1.DetectedAttribute;

video/src/main/java/com/example/video/DetectPerson.java renamed to video/src/main/java/beta/video/DetectPerson.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.example.video;
17+
package beta.video;
1818

1919
// [START video_detect_person_beta]
2020

video/src/main/java/com/example/video/DetectPersonGcs.java renamed to video/src/main/java/beta/video/DetectPersonGcs.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.example.video;
17+
package beta.video;
1818

1919
// [START video_detect_person_gcs_beta]
2020

video/src/main/java/com/example/video/StreamingAnnotationToStorage.java renamed to video/src/main/java/beta/video/StreamingAnnotationToStorage.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.example.video;
17+
package beta.video;
1818

1919
// [START video_streaming_annotation_to_storage_beta]
20+
2021
import com.google.api.gax.rpc.BidiStream;
2122
import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoRequest;
2223
import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoResponse;

video/src/main/java/com/example/video/StreamingAutoMlClassification.java renamed to video/src/main/java/beta/video/StreamingAutoMlClassification.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.example.video;
17+
package beta.video;
1818

1919
// [START video_streaming_automl_classification_beta]
20+
2021
import com.google.api.gax.rpc.BidiStream;
2122
import com.google.cloud.videointelligence.v1p3beta1.LabelAnnotation;
2223
import com.google.cloud.videointelligence.v1p3beta1.LabelFrame;

video/src/main/java/com/example/video/StreamingExplicitContentDetection.java renamed to video/src/main/java/beta/video/StreamingExplicitContentDetection.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.example.video;
17+
package beta.video;
1818

1919
// [START video_streaming_explicit_content_detection_beta]
20+
2021
import com.google.api.gax.rpc.BidiStream;
2122
import com.google.cloud.videointelligence.v1p3beta1.ExplicitContentFrame;
2223
import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoRequest;

video/src/main/java/com/example/video/StreamingLabelDetection.java renamed to video/src/main/java/beta/video/StreamingLabelDetection.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.example.video;
17+
package beta.video;
1818

1919
// [START video_streaming_label_detection_beta]
20+
2021
import com.google.api.gax.rpc.BidiStream;
2122
import com.google.cloud.videointelligence.v1p3beta1.LabelAnnotation;
2223
import com.google.cloud.videointelligence.v1p3beta1.LabelFrame;

video/src/main/java/com/example/video/StreamingObjectTracking.java renamed to video/src/main/java/beta/video/StreamingObjectTracking.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.example.video;
17+
package beta.video;
1818

1919
// [START video_streaming_object_tracking_beta]
20+
2021
import com.google.api.gax.rpc.BidiStream;
2122
import com.google.cloud.videointelligence.v1p3beta1.ObjectTrackingAnnotation;
2223
import com.google.cloud.videointelligence.v1p3beta1.ObjectTrackingFrame;

video/src/main/java/com/example/video/StreamingShotChangeDetection.java renamed to video/src/main/java/beta/video/StreamingShotChangeDetection.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.example.video;
17+
package beta.video;
1818

1919
// [START video_streaming_shot_change_detection_beta]
20+
2021
import com.google.api.gax.rpc.BidiStream;
2122
import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoRequest;
2223
import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoResponse;

0 commit comments

Comments
 (0)