|
| 1 | +/* |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 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 | +// Client that sends audio to Speech.AsyncRecognize via gRPC and returns transcription. |
| 18 | +// |
| 19 | +// Uses a service account for OAuth2 authentication, which you may obtain at |
| 20 | +// https://console.developers.google.com |
| 21 | +// API Manager > Google Cloud Speech API > Enable |
| 22 | +// API Manager > Credentials > Create credentials > Service account key > New service account. |
| 23 | +// |
| 24 | +// Then set environment variable GOOGLE_APPLICATION_CREDENTIALS to the full path of that file. |
| 25 | + |
| 26 | +package com.google.cloud.speech.grpc.demos; |
| 27 | + |
| 28 | +import com.google.auth.oauth2.GoogleCredentials; |
| 29 | +import com.google.cloud.speech.v1beta1.RecognitionAudio; |
| 30 | +import com.google.cloud.speech.v1beta1.RecognitionConfig; |
| 31 | +import com.google.cloud.speech.v1beta1.RecognitionConfig.AudioEncoding; |
| 32 | +import com.google.cloud.speech.v1beta1.SpeechGrpc; |
| 33 | +import com.google.cloud.speech.v1beta1.AsyncRecognizeRequest; |
| 34 | +import com.google.cloud.speech.v1beta1.AsyncRecognizeResponse; |
| 35 | +import com.google.protobuf.TextFormat; |
| 36 | + |
| 37 | +import google.longrunning.Operation; |
| 38 | + |
| 39 | +import io.grpc.ManagedChannel; |
| 40 | +import io.grpc.StatusRuntimeException; |
| 41 | +import io.grpc.auth.ClientAuthInterceptor; |
| 42 | +import io.grpc.netty.NegotiationType; |
| 43 | +import io.grpc.netty.NettyChannelBuilder; |
| 44 | + |
| 45 | +import org.apache.commons.cli.CommandLine; |
| 46 | +import org.apache.commons.cli.CommandLineParser; |
| 47 | +import org.apache.commons.cli.DefaultParser; |
| 48 | +import org.apache.commons.cli.OptionBuilder; |
| 49 | +import org.apache.commons.cli.Options; |
| 50 | +import org.apache.commons.cli.ParseException; |
| 51 | + |
| 52 | +import java.io.IOException; |
| 53 | +import java.net.URI; |
| 54 | +import java.util.Arrays; |
| 55 | +import java.util.List; |
| 56 | +import java.util.concurrent.Executors; |
| 57 | +import java.util.concurrent.TimeUnit; |
| 58 | +import java.util.logging.Level; |
| 59 | +import java.util.logging.Logger; |
| 60 | + |
| 61 | +/** |
| 62 | + * Client that sends audio to Speech.AsyncRecognize and returns transcript. |
| 63 | + */ |
| 64 | +public class AsyncRecognizeClient { |
| 65 | + |
| 66 | + private static final Logger logger = |
| 67 | + Logger.getLogger(AsyncRecognizeClient.class.getName()); |
| 68 | + |
| 69 | + private static final List<String> OAUTH2_SCOPES = |
| 70 | + Arrays.asList("https://www.googleapis.com/auth/cloud-platform"); |
| 71 | + |
| 72 | + private final String host; |
| 73 | + private final int port; |
| 74 | + private final URI input; |
| 75 | + private final int samplingRate; |
| 76 | + |
| 77 | + private final ManagedChannel channel; |
| 78 | + private final SpeechGrpc.SpeechBlockingStub stub; |
| 79 | + |
| 80 | + /** |
| 81 | + * Construct client connecting to Cloud Speech server at {@code host:port}. |
| 82 | + */ |
| 83 | + public AsyncRecognizeClient(String host, int port, URI input, int samplingRate) |
| 84 | + throws IOException { |
| 85 | + this.host = host; |
| 86 | + this.port = port; |
| 87 | + this.input = input; |
| 88 | + this.samplingRate = samplingRate; |
| 89 | + |
| 90 | + GoogleCredentials creds = GoogleCredentials.getApplicationDefault(); |
| 91 | + creds = creds.createScoped(OAUTH2_SCOPES); |
| 92 | + channel = NettyChannelBuilder.forAddress(host, port) |
| 93 | + .negotiationType(NegotiationType.TLS) |
| 94 | + .intercept(new ClientAuthInterceptor(creds, Executors.newSingleThreadExecutor())) |
| 95 | + .build(); |
| 96 | + stub = SpeechGrpc.newBlockingStub(channel); |
| 97 | + logger.info("Created stub for " + host + ":" + port); |
| 98 | + } |
| 99 | + |
| 100 | + private RecognitionAudio createRecognitionAudio() throws IOException { |
| 101 | + return RecognitionAudioFactory.createRecognitionAudio(this.input); |
| 102 | + } |
| 103 | + |
| 104 | + public void shutdown() throws InterruptedException { |
| 105 | + channel.shutdown().awaitTermination(5, TimeUnit.SECONDS); |
| 106 | + } |
| 107 | + |
| 108 | + /** Send an async-recognize request to server. */ |
| 109 | + public void recognize() { |
| 110 | + RecognitionAudio audio; |
| 111 | + try { |
| 112 | + audio = createRecognitionAudio(); |
| 113 | + } catch (IOException e) { |
| 114 | + logger.log(Level.WARNING, "Failed to read audio uri input: " + input); |
| 115 | + return; |
| 116 | + } |
| 117 | + logger.info("Sending " + audio.getContent().size() + " bytes from audio uri input: " + input); |
| 118 | + RecognitionConfig config = RecognitionConfig.newBuilder() |
| 119 | + .setEncoding(AudioEncoding.LINEAR16) |
| 120 | + .setSampleRate(samplingRate) |
| 121 | + .build(); |
| 122 | + AsyncRecognizeRequest request = AsyncRecognizeRequest.newBuilder() |
| 123 | + .setConfig(config) |
| 124 | + .setAudio(audio) |
| 125 | + .build(); |
| 126 | + |
| 127 | + Operation operation; |
| 128 | + try { |
| 129 | + operation = stub.asyncRecognize(request); |
| 130 | + |
| 131 | + //Print the long running operation handle |
| 132 | + System.out.println(operation); |
| 133 | + } catch (StatusRuntimeException e) { |
| 134 | + logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus()); |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + while(operation.hasDone()) { |
| 139 | + System.out.println(operation); |
| 140 | + //Sleep for 1 s |
| 141 | + Thread.sleep(1000); |
| 142 | + } |
| 143 | + //logger.info("Received response: " + TextFormat.printToString(response)); |
| 144 | + } |
| 145 | + |
| 146 | + public static void main(String[] args) throws Exception { |
| 147 | + |
| 148 | + String audioFile = ""; |
| 149 | + String host = "speech.googleapis.com"; |
| 150 | + Integer port = 443; |
| 151 | + Integer sampling = 16000; |
| 152 | + |
| 153 | + CommandLineParser parser = new DefaultParser(); |
| 154 | + |
| 155 | + Options options = new Options(); |
| 156 | + options.addOption(OptionBuilder.withLongOpt("uri") |
| 157 | + .withDescription("path to audio uri") |
| 158 | + .hasArg() |
| 159 | + .withArgName("FILE_PATH") |
| 160 | + .create()); |
| 161 | + options.addOption(OptionBuilder.withLongOpt("host") |
| 162 | + .withDescription("endpoint for api, e.g. speech.googleapis.com") |
| 163 | + .hasArg() |
| 164 | + .withArgName("ENDPOINT") |
| 165 | + .create()); |
| 166 | + options.addOption(OptionBuilder.withLongOpt("port") |
| 167 | + .withDescription("SSL port, usually 443") |
| 168 | + .hasArg() |
| 169 | + .withArgName("PORT") |
| 170 | + .create()); |
| 171 | + options.addOption(OptionBuilder.withLongOpt("sampling") |
| 172 | + .withDescription("Sampling Rate, i.e. 16000") |
| 173 | + .hasArg() |
| 174 | + .withArgName("RATE") |
| 175 | + .create()); |
| 176 | + |
| 177 | + try { |
| 178 | + CommandLine line = parser.parse(options, args); |
| 179 | + if (line.hasOption("uri")) { |
| 180 | + audioFile = line.getOptionValue("uri"); |
| 181 | + } else { |
| 182 | + System.err.println("An Audio uri must be specified (e.g. file:///foo/baz.raw)."); |
| 183 | + System.exit(1); |
| 184 | + } |
| 185 | + |
| 186 | + if (line.hasOption("host")) { |
| 187 | + host = line.getOptionValue("host"); |
| 188 | + } else { |
| 189 | + System.err.println("An API enpoint must be specified (typically speech.googleapis.com)."); |
| 190 | + System.exit(1); |
| 191 | + } |
| 192 | + |
| 193 | + if (line.hasOption("port")) { |
| 194 | + port = Integer.parseInt(line.getOptionValue("port")); |
| 195 | + } else { |
| 196 | + System.err.println("An SSL port must be specified (typically 443)."); |
| 197 | + System.exit(1); |
| 198 | + } |
| 199 | + |
| 200 | + if (line.hasOption("sampling")) { |
| 201 | + sampling = Integer.parseInt(line.getOptionValue("sampling")); |
| 202 | + } else { |
| 203 | + System.err.println("An Audio sampling rate must be specified."); |
| 204 | + System.exit(1); |
| 205 | + } |
| 206 | + } catch (ParseException exp) { |
| 207 | + System.err.println("Unexpected exception:" + exp.getMessage()); |
| 208 | + System.exit(1); |
| 209 | + } |
| 210 | + |
| 211 | + AsyncRecognizeClient client = |
| 212 | + new AsyncRecognizeClient(host, port, URI.create(audioFile), sampling); |
| 213 | + try { |
| 214 | + client.recognize(); |
| 215 | + } finally { |
| 216 | + client.shutdown(); |
| 217 | + } |
| 218 | + } |
| 219 | +} |
0 commit comments