|
| 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 | + * 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.speech; |
| 18 | + |
| 19 | +// [START speech_transcribe_diarization_gcs] |
| 20 | + |
| 21 | +import com.google.api.gax.longrunning.OperationFuture; |
| 22 | +import com.google.cloud.speech.v1.LongRunningRecognizeMetadata; |
| 23 | +import com.google.cloud.speech.v1.LongRunningRecognizeResponse; |
| 24 | +import com.google.cloud.speech.v1.RecognitionAudio; |
| 25 | +import com.google.cloud.speech.v1.RecognitionConfig; |
| 26 | +import com.google.cloud.speech.v1.SpeakerDiarizationConfig; |
| 27 | +import com.google.cloud.speech.v1.SpeechClient; |
| 28 | +import com.google.cloud.speech.v1.SpeechRecognitionAlternative; |
| 29 | +import com.google.cloud.speech.v1.WordInfo; |
| 30 | + |
| 31 | +import java.io.IOException; |
| 32 | +import java.util.concurrent.ExecutionException; |
| 33 | + |
| 34 | +public class TranscribeDiarizationGcs { |
| 35 | + |
| 36 | + static void transcribeDiarizationGcs() throws IOException, ExecutionException, |
| 37 | + InterruptedException { |
| 38 | + // TODO(developer): Replace these variables before running the sample. |
| 39 | + String gcsUri = "gs://cloud-samples-data/speech/commercial_mono.wav"; |
| 40 | + transcribeDiarizationGcs(gcsUri); |
| 41 | + } |
| 42 | + |
| 43 | + // Transcribe the give gcs file using speaker diarization |
| 44 | + public static void transcribeDiarizationGcs(String gcsUri) throws IOException, |
| 45 | + ExecutionException, InterruptedException { |
| 46 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 47 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 48 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 49 | + try (SpeechClient speechClient = SpeechClient.create()) { |
| 50 | + SpeakerDiarizationConfig speakerDiarizationConfig = SpeakerDiarizationConfig.newBuilder() |
| 51 | + .setEnableSpeakerDiarization(true) |
| 52 | + .setMinSpeakerCount(2) |
| 53 | + .setMaxSpeakerCount(2) |
| 54 | + .build(); |
| 55 | + // Configure request to enable Speaker diarization |
| 56 | + RecognitionConfig config = |
| 57 | + RecognitionConfig.newBuilder() |
| 58 | + .setEncoding(RecognitionConfig.AudioEncoding.LINEAR16) |
| 59 | + .setLanguageCode("en-US") |
| 60 | + .setSampleRateHertz(8000) |
| 61 | + .setDiarizationConfig(speakerDiarizationConfig) |
| 62 | + .build(); |
| 63 | + // Set the remote path for the audio file |
| 64 | + RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(gcsUri).build(); |
| 65 | + |
| 66 | + // Use non-blocking call for getting file transcription |
| 67 | + OperationFuture<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> future = |
| 68 | + speechClient.longRunningRecognizeAsync(config, audio); |
| 69 | + System.out.println("Waiting for response..."); |
| 70 | + |
| 71 | + // Speaker Tags are only included in the last result object, which has only one alternative. |
| 72 | + LongRunningRecognizeResponse response = future.get(); |
| 73 | + SpeechRecognitionAlternative alternative = |
| 74 | + response.getResults( |
| 75 | + response.getResultsCount() - 1) |
| 76 | + .getAlternatives(0); |
| 77 | + // The alternative is made up of WordInfo objects that contain the speaker_tag. |
| 78 | + WordInfo wordInfo = alternative.getWords(0); |
| 79 | + int currentSpeakerTag = wordInfo.getSpeakerTag(); |
| 80 | + // For each word, get all the words associated with one speaker, once the speaker changes, |
| 81 | + // add a new line with the new speaker and their spoken words. |
| 82 | + StringBuilder speakerWords = new StringBuilder( |
| 83 | + String.format("Speaker %d: %s", wordInfo.getSpeakerTag(), wordInfo.getWord())); |
| 84 | + for (int i = 1; i < alternative.getWordsCount(); i++) { |
| 85 | + wordInfo = alternative.getWords(i); |
| 86 | + if (currentSpeakerTag == wordInfo.getSpeakerTag()) { |
| 87 | + speakerWords.append(" "); |
| 88 | + speakerWords.append(wordInfo.getWord()); |
| 89 | + } else { |
| 90 | + speakerWords.append( |
| 91 | + String.format("\nSpeaker %d: %s", |
| 92 | + wordInfo.getSpeakerTag(), |
| 93 | + wordInfo.getWord())); |
| 94 | + currentSpeakerTag = wordInfo.getSpeakerTag(); |
| 95 | + } |
| 96 | + } |
| 97 | + System.out.println(speakerWords.toString()); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | +// [END speech_transcribe_diarization_gcs] |
0 commit comments