|
| 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 | +} |
0 commit comments