|
| 1 | +/* |
| 2 | + Copyright 2017, 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 com.example.speech; |
| 18 | + |
| 19 | +import com.google.api.gax.grpc.OperationFuture; |
| 20 | +import com.google.cloud.speech.spi.v1beta1.SpeechClient; |
| 21 | +import com.google.cloud.speech.v1beta1.AsyncRecognizeResponse; |
| 22 | +import com.google.cloud.speech.v1beta1.RecognitionAudio; |
| 23 | +import com.google.cloud.speech.v1beta1.RecognitionConfig; |
| 24 | +import com.google.cloud.speech.v1beta1.RecognitionConfig.AudioEncoding; |
| 25 | +import com.google.cloud.speech.v1beta1.SpeechRecognitionAlternative; |
| 26 | +import com.google.cloud.speech.v1beta1.SpeechRecognitionResult; |
| 27 | +import com.google.cloud.speech.v1beta1.SyncRecognizeResponse; |
| 28 | +import com.google.protobuf.ByteString; |
| 29 | + |
| 30 | +import java.io.IOException; |
| 31 | +import java.nio.file.Files; |
| 32 | +import java.nio.file.Path; |
| 33 | +import java.nio.file.Paths; |
| 34 | +import java.util.List; |
| 35 | + |
| 36 | +public class Recognize { |
| 37 | + public static void main(String... args) throws Exception { |
| 38 | + if (args.length < 1) { |
| 39 | + System.out.println("Usage:"); |
| 40 | + System.out.printf( |
| 41 | + "\tjava %s \"<command>\" \"<path-to-image>\"\n" |
| 42 | + + "Commands:\n" |
| 43 | + + "\tsyncrecognize | asyncrecognize\n" |
| 44 | + + "Path:\n\tA file path (ex: ./resources/audio.raw) or a URI " |
| 45 | + + "for a Cloud Storage resource (gs://...)\n", |
| 46 | + Recognize.class.getCanonicalName()); |
| 47 | + return; |
| 48 | + } |
| 49 | + String command = args[0]; |
| 50 | + String path = args.length > 1 ? args[1] : ""; |
| 51 | + |
| 52 | + if (command.equals("syncrecognize")) { |
| 53 | + if (path.startsWith("gs://")) { |
| 54 | + syncRecognizeGcs(path); |
| 55 | + } else { |
| 56 | + syncRecognizeFile(path); |
| 57 | + } |
| 58 | + } else if (command.equals("asyncrecognize")) { |
| 59 | + if (path.startsWith("gs://")) { |
| 60 | + asyncRecognizeGcs(path); |
| 61 | + } else { |
| 62 | + asyncRecognizeFile(path); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + } |
| 68 | + |
| 69 | + public static void syncRecognizeFile(String fileName) throws Exception, IOException { |
| 70 | + SpeechClient speech = SpeechClient.create(); |
| 71 | + |
| 72 | + Path path = Paths.get(fileName); |
| 73 | + byte[] data = Files.readAllBytes(path); |
| 74 | + ByteString audioBytes = ByteString.copyFrom(data); |
| 75 | + |
| 76 | + RecognitionConfig config = RecognitionConfig.newBuilder() |
| 77 | + .setEncoding(AudioEncoding.LINEAR16) |
| 78 | + .setSampleRate(16000) |
| 79 | + .build(); |
| 80 | + RecognitionAudio audio = RecognitionAudio.newBuilder() |
| 81 | + .setContent(audioBytes) |
| 82 | + .build(); |
| 83 | + |
| 84 | + SyncRecognizeResponse response = speech.syncRecognize(config, audio); |
| 85 | + List<SpeechRecognitionResult> results = response.getResultsList(); |
| 86 | + |
| 87 | + for (SpeechRecognitionResult result: results) { |
| 88 | + List<SpeechRecognitionAlternative> alternatives = result.getAlternativesList(); |
| 89 | + for (SpeechRecognitionAlternative alternative: alternatives) { |
| 90 | + System.out.printf("Transcription: %s%n", alternative.getTranscript()); |
| 91 | + } |
| 92 | + } |
| 93 | + speech.close(); |
| 94 | + } |
| 95 | + |
| 96 | + public static void syncRecognizeGcs(String gcsUri) throws Exception, IOException { |
| 97 | + // Instantiates a client |
| 98 | + SpeechClient speech = SpeechClient.create(); |
| 99 | + |
| 100 | + // Builds the sync recognize request |
| 101 | + RecognitionConfig config = RecognitionConfig.newBuilder() |
| 102 | + .setEncoding(AudioEncoding.FLAC) |
| 103 | + .setSampleRate(16000) |
| 104 | + .build(); |
| 105 | + RecognitionAudio audio = RecognitionAudio.newBuilder() |
| 106 | + .setUri(gcsUri) |
| 107 | + .build(); |
| 108 | + |
| 109 | + // Performs speech recognition on the audio file |
| 110 | + SyncRecognizeResponse response = speech.syncRecognize(config, audio); |
| 111 | + List<SpeechRecognitionResult> results = response.getResultsList(); |
| 112 | + |
| 113 | + for (SpeechRecognitionResult result: results) { |
| 114 | + List<SpeechRecognitionAlternative> alternatives = result.getAlternativesList(); |
| 115 | + for (SpeechRecognitionAlternative alternative: alternatives) { |
| 116 | + System.out.printf("Transcription: %s%n", alternative.getTranscript()); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | + speech.close(); |
| 122 | + } |
| 123 | + |
| 124 | + public static void asyncRecognizeFile(String fileName) throws Exception, IOException { |
| 125 | + SpeechClient speech = SpeechClient.create(); |
| 126 | + |
| 127 | + Path path = Paths.get(fileName); |
| 128 | + byte[] data = Files.readAllBytes(path); |
| 129 | + ByteString audioBytes = ByteString.copyFrom(data); |
| 130 | + |
| 131 | + RecognitionConfig config = RecognitionConfig.newBuilder() |
| 132 | + .setEncoding(AudioEncoding.LINEAR16) |
| 133 | + .setSampleRate(16000) |
| 134 | + .build(); |
| 135 | + RecognitionAudio audio = RecognitionAudio.newBuilder() |
| 136 | + .setContent(audioBytes) |
| 137 | + .build(); |
| 138 | + |
| 139 | + OperationFuture<AsyncRecognizeResponse> response = speech.asyncRecognizeAsync(config, audio); |
| 140 | + |
| 141 | + while(!response.isDone()) { |
| 142 | + System.out.println("Waiting for response..."); |
| 143 | + Thread.sleep(200); |
| 144 | + } |
| 145 | + |
| 146 | + List<SpeechRecognitionResult> results = response.get().getResultsList(); |
| 147 | + |
| 148 | + for (SpeechRecognitionResult result: results) { |
| 149 | + List<SpeechRecognitionAlternative> alternatives = result.getAlternativesList(); |
| 150 | + for (SpeechRecognitionAlternative alternative: alternatives) { |
| 151 | + System.out.printf("Transcription: %s%n", alternative.getTranscript()); |
| 152 | + } |
| 153 | + } |
| 154 | + speech.close(); |
| 155 | + } |
| 156 | + |
| 157 | + public static void asyncRecognizeGcs(String gcsUri) throws Exception, IOException { |
| 158 | + SpeechClient speech = SpeechClient.create(); |
| 159 | + |
| 160 | + RecognitionConfig config = RecognitionConfig.newBuilder() |
| 161 | + .setEncoding(AudioEncoding.FLAC) |
| 162 | + .setSampleRate(16000) |
| 163 | + .build(); |
| 164 | + RecognitionAudio audio = RecognitionAudio.newBuilder() |
| 165 | + .setUri(gcsUri) |
| 166 | + .build(); |
| 167 | + |
| 168 | + OperationFuture<AsyncRecognizeResponse> response = speech.asyncRecognizeAsync(config, audio); |
| 169 | + |
| 170 | + while(!response.isDone()) { |
| 171 | + System.out.println("Waiting for response..."); |
| 172 | + Thread.sleep(200); |
| 173 | + } |
| 174 | + |
| 175 | + List<SpeechRecognitionResult> results = response.get().getResultsList(); |
| 176 | + |
| 177 | + for (SpeechRecognitionResult result: results) { |
| 178 | + List<SpeechRecognitionAlternative> alternatives = result.getAlternativesList(); |
| 179 | + for (SpeechRecognitionAlternative alternative: alternatives) { |
| 180 | + System.out.printf("Transcription: %s%n", alternative.getTranscript()); |
| 181 | + } |
| 182 | + } |
| 183 | + speech.close(); |
| 184 | + } |
| 185 | +} |
0 commit comments