|
| 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", "speech_transcribe_async_word_time_offsets_gcs") |
| 17 | +// sample-metadata: |
| 18 | +// title: Getting word timestamps (Cloud Storage) (LRO) |
| 19 | +// description: Print start and end time of each word spoken in audio file from Cloud Storage |
| 20 | +// usage: gradle run -PmainClass=com.google.cloud.examples.speech.v1.SpeechTranscribeAsyncWordTimeOffsetsGcs [--args='[--storage_uri "gs://cloud-samples-data/speech/brooklyn_bridge.flac"]'] |
| 21 | + |
| 22 | +package com.google.cloud.examples.speech.v1; |
| 23 | + |
| 24 | +import com.google.api.gax.longrunning.OperationFuture; |
| 25 | +import com.google.cloud.speech.v1.LongRunningRecognizeMetadata; |
| 26 | +import com.google.cloud.speech.v1.LongRunningRecognizeRequest; |
| 27 | +import com.google.cloud.speech.v1.LongRunningRecognizeResponse; |
| 28 | +import com.google.cloud.speech.v1.RecognitionAudio; |
| 29 | +import com.google.cloud.speech.v1.RecognitionConfig; |
| 30 | +import com.google.cloud.speech.v1.SpeechClient; |
| 31 | +import com.google.cloud.speech.v1.SpeechRecognitionAlternative; |
| 32 | +import com.google.cloud.speech.v1.SpeechRecognitionResult; |
| 33 | +import com.google.cloud.speech.v1.WordInfo; |
| 34 | +import org.apache.commons.cli.CommandLine; |
| 35 | +import org.apache.commons.cli.DefaultParser; |
| 36 | +import org.apache.commons.cli.Option; |
| 37 | +import org.apache.commons.cli.Options; |
| 38 | + |
| 39 | +public class SpeechTranscribeAsyncWordTimeOffsetsGcs { |
| 40 | + // [START speech_transcribe_async_word_time_offsets_gcs] |
| 41 | + /* |
| 42 | + * Please include the following imports to run this sample. |
| 43 | + * |
| 44 | + * import com.google.api.gax.longrunning.OperationFuture; |
| 45 | + * import com.google.cloud.speech.v1.LongRunningRecognizeMetadata; |
| 46 | + * import com.google.cloud.speech.v1.LongRunningRecognizeRequest; |
| 47 | + * import com.google.cloud.speech.v1.LongRunningRecognizeResponse; |
| 48 | + * import com.google.cloud.speech.v1.RecognitionAudio; |
| 49 | + * import com.google.cloud.speech.v1.RecognitionConfig; |
| 50 | + * import com.google.cloud.speech.v1.SpeechClient; |
| 51 | + * import com.google.cloud.speech.v1.SpeechRecognitionAlternative; |
| 52 | + * import com.google.cloud.speech.v1.SpeechRecognitionResult; |
| 53 | + * import com.google.cloud.speech.v1.WordInfo; |
| 54 | + */ |
| 55 | + |
| 56 | + /** |
| 57 | + * Print start and end time of each word spoken in audio file from Cloud Storage |
| 58 | + * |
| 59 | + * @param storageUri URI for audio file in Cloud Storage, e.g. gs://[BUCKET]/[FILE] |
| 60 | + */ |
| 61 | + public static void sampleLongRunningRecognize(String storageUri) { |
| 62 | + try (SpeechClient speechClient = SpeechClient.create()) { |
| 63 | + // storageUri = "gs://cloud-samples-data/speech/brooklyn_bridge.flac"; |
| 64 | + |
| 65 | + // When enabled, the first result returned by the API will include a list |
| 66 | + // of words and the start and end time offsets (timestamps) for those words. |
| 67 | + boolean enableWordTimeOffsets = true; |
| 68 | + |
| 69 | + // The language of the supplied audio |
| 70 | + String languageCode = "en-US"; |
| 71 | + RecognitionConfig config = |
| 72 | + RecognitionConfig.newBuilder() |
| 73 | + .setEnableWordTimeOffsets(enableWordTimeOffsets) |
| 74 | + .setLanguageCode(languageCode) |
| 75 | + .build(); |
| 76 | + RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(storageUri).build(); |
| 77 | + LongRunningRecognizeRequest request = |
| 78 | + LongRunningRecognizeRequest.newBuilder().setConfig(config).setAudio(audio).build(); |
| 79 | + OperationFuture<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> future = |
| 80 | + speechClient.longRunningRecognizeAsync(request); |
| 81 | + |
| 82 | + System.out.println("Waiting for operation to complete..."); |
| 83 | + LongRunningRecognizeResponse response = future.get(); |
| 84 | + // The first result includes start and end time word offsets |
| 85 | + SpeechRecognitionResult result = response.getResultsList().get(0); |
| 86 | + // First alternative is the most probable result |
| 87 | + SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0); |
| 88 | + System.out.printf("Transcript: %s\n", alternative.getTranscript()); |
| 89 | + // Print the start and end time of each word |
| 90 | + for (WordInfo word : alternative.getWordsList()) { |
| 91 | + System.out.printf("Word: %s\n", word.getWord()); |
| 92 | + System.out.printf( |
| 93 | + "Start time: %s seconds %s nanos\n", |
| 94 | + word.getStartTime().getSeconds(), word.getStartTime().getNanos()); |
| 95 | + System.out.printf( |
| 96 | + "End time: %s seconds %s nanos\n", |
| 97 | + word.getEndTime().getSeconds(), word.getEndTime().getNanos()); |
| 98 | + } |
| 99 | + } catch (Exception exception) { |
| 100 | + System.err.println("Failed to create the client due to: " + exception); |
| 101 | + } |
| 102 | + } |
| 103 | + // [END speech_transcribe_async_word_time_offsets_gcs] |
| 104 | + |
| 105 | + public static void main(String[] args) throws Exception { |
| 106 | + Options options = new Options(); |
| 107 | + options.addOption( |
| 108 | + Option.builder("").required(false).hasArg(true).longOpt("storage_uri").build()); |
| 109 | + |
| 110 | + CommandLine cl = (new DefaultParser()).parse(options, args); |
| 111 | + String storageUri = |
| 112 | + cl.getOptionValue("storage_uri", "gs://cloud-samples-data/speech/brooklyn_bridge.flac"); |
| 113 | + |
| 114 | + sampleLongRunningRecognize(storageUri); |
| 115 | + } |
| 116 | +} |
0 commit comments