|
| 1 | +/* |
| 2 | + * Copyright 2020 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_with_multi_region_gcs] |
| 20 | + |
| 21 | +import com.google.cloud.speech.v1.RecognitionAudio; |
| 22 | +import com.google.cloud.speech.v1.RecognitionConfig; |
| 23 | +import com.google.cloud.speech.v1.RecognitionConfig.AudioEncoding; |
| 24 | +import com.google.cloud.speech.v1.RecognizeResponse; |
| 25 | +import com.google.cloud.speech.v1.SpeechClient; |
| 26 | +import com.google.cloud.speech.v1.SpeechRecognitionAlternative; |
| 27 | +import com.google.cloud.speech.v1.SpeechRecognitionResult; |
| 28 | +import com.google.cloud.speech.v1.SpeechSettings; |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.List; |
| 31 | + |
| 32 | +public class SpeechTranscribeMultiRegion { |
| 33 | + |
| 34 | + public void speechTranscribeMultiRegion() throws Exception { |
| 35 | + String uriPath = "gs://cloud-samples-tests/speech/brooklyn.flac"; |
| 36 | + speechTranscribeMultiRegion(uriPath); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Transcribe a remote audio file with multi-channel recognition |
| 41 | + * |
| 42 | + * @param gcsUri the path to the audio file |
| 43 | + */ |
| 44 | + public static void speechTranscribeMultiRegion(String gcsUri) throws Exception { |
| 45 | + // Use the SpeechSettings to initialize the SpeechClient with the new endpoint. |
| 46 | + String endPoint = "eu-speech.googleapis.com:443"; |
| 47 | + SpeechSettings speechSettings = |
| 48 | + SpeechSettings.newBuilder() |
| 49 | + .setEndpoint(endPoint) |
| 50 | + .build(); |
| 51 | + |
| 52 | + // Instantiates a client with GOOGLE_APPLICATION_CREDENTIALS |
| 53 | + try (SpeechClient speech = SpeechClient.create(speechSettings)) { |
| 54 | + |
| 55 | + // Configure remote file request |
| 56 | + RecognitionConfig config = |
| 57 | + RecognitionConfig.newBuilder() |
| 58 | + .setEncoding(AudioEncoding.FLAC) |
| 59 | + .setLanguageCode("en-US") |
| 60 | + .setSampleRateHertz(16000) |
| 61 | + .build(); |
| 62 | + |
| 63 | + // Set the remote path for the audio file |
| 64 | + RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(gcsUri).build(); |
| 65 | + |
| 66 | + // Use blocking call to get audio transcript |
| 67 | + RecognizeResponse response = speech.recognize(config, audio); |
| 68 | + List<SpeechRecognitionResult> results = response.getResultsList(); |
| 69 | + |
| 70 | + for (SpeechRecognitionResult result : results) { |
| 71 | + // There can be several alternative transcripts for a given chunk of speech. Just use the |
| 72 | + // first (most likely) one here. |
| 73 | + SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0); |
| 74 | + System.out.printf("Transcription: %s\n", alternative.getTranscript()); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | +// [END speech_transcribe_with_multi_region_gcs] |
0 commit comments