Skip to content

Commit 4eba6f2

Browse files
committed
Adds sync / async examples for local and remote files
1 parent ec9375e commit 4eba6f2

File tree

4 files changed

+280
-0
lines changed

4 files changed

+280
-0
lines changed

speech/cloud-client/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,15 @@ You can then run a given `ClassName` via:
2424
### Transcribe a local audio file (using the quickstart sample)
2525

2626
mvn exec:java -Dexec.mainClass=com.example.speech.QuickstartSample
27+
28+
### Transcribe a local audio file (using the recognize sample)
29+
```
30+
mvn exec:java -Dexec.mainClass=com.example.speech.Recognize \
31+
-Dexec.args="syncrecognize ./resources/audio.raw"
32+
```
33+
34+
### Transcribe a remote audio file (using the recognize sample)
35+
```
36+
mvn exec:java -Dexec.mainClass=com.example.speech.Recognize \
37+
-Dexec.args="syncrecognize 'gs://cloud-samples-tests/speech/brooklyn.flac'"
38+
```

speech/cloud-client/src/main/java/com/example/speech/QuickstartSample.java

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public static void main(String... args) throws Exception {
6464
System.out.printf("Transcription: %s%n", alternative.getTranscript());
6565
}
6666
}
67+
speech.close();
6768
}
6869
}
6970
// [END speech_quickstart]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 static com.google.common.truth.Truth.assertThat;
20+
21+
import org.junit.After;
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.junit.runners.JUnit4;
26+
27+
import java.io.ByteArrayOutputStream;
28+
import java.io.PrintStream;
29+
30+
/**
31+
* Tests for speech recognize sample.
32+
*/
33+
@RunWith(JUnit4.class)
34+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
35+
public class RecognizeIT {
36+
private ByteArrayOutputStream bout;
37+
private PrintStream out;
38+
39+
// The path to the audio file to transcribe
40+
private String fileName = "./resources/audio.raw";
41+
private String gcsPath = "gs://cloud-samples-tests/speech/brooklyn.flac";
42+
43+
@Before
44+
public void setUp() {
45+
bout = new ByteArrayOutputStream();
46+
out = new PrintStream(bout);
47+
System.setOut(out);
48+
}
49+
50+
@After
51+
public void tearDown() {
52+
System.setOut(null);
53+
}
54+
55+
@Test
56+
public void testRecognizeFile() throws Exception {
57+
Recognize.syncRecognizeFile(fileName);
58+
String got = bout.toString();
59+
assertThat(got).contains("how old is the Brooklyn Bridge");
60+
}
61+
62+
@Test
63+
public void testRecognizeGcs() throws Exception {
64+
Recognize.syncRecognizeGcs(gcsPath);
65+
String got = bout.toString();
66+
assertThat(got).contains("how old is the Brooklyn Bridge");
67+
}
68+
69+
@Test
70+
public void testAsyncRecognizeFile() throws Exception {
71+
Recognize.asyncRecognizeFile(fileName);
72+
String got = bout.toString();
73+
assertThat(got).contains("how old is the Brooklyn Bridge");
74+
}
75+
76+
@Test
77+
public void testAsyncRecognizeGcs() throws Exception {
78+
Recognize.asyncRecognizeGcs(gcsPath);
79+
String got = bout.toString();
80+
assertThat(got).contains("how old is the Brooklyn Bridge");
81+
}
82+
}

0 commit comments

Comments
 (0)