Skip to content

Commit 47d84ab

Browse files
authored
Add Speech API quickstart sample. (#497)
1 parent 363ceb1 commit 47d84ab

File tree

5 files changed

+214
-0
lines changed

5 files changed

+214
-0
lines changed

speech/cloud-client/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Getting Started with Google Cloud Speech API and the Google Cloud Client libraries
2+
3+
[Google Cloud Speech API][speech] enables easy integration of Google speech
4+
recognition technologies into developer applications.
5+
6+
These sample Java applications demonstrate how to access the Cloud Speech API
7+
using the [Google Cloud Client Library for Java][google-cloud-java].
8+
9+
[speech]: https://cloud.google.com/speech/docs/
10+
[google-cloud-java]: https://github.com/GoogleCloudPlatform/google-cloud-java
11+
12+
## Quickstart
13+
14+
Install [Maven](http://maven.apache.org/).
15+
16+
Build your project with:
17+
18+
mvn clean package -DskipTests
19+
20+
You can then run a given `ClassName` via:
21+
22+
mvn exec:java -Dexec.mainClass=com.example.speech.ClassName
23+
24+
### Transcribe a local audio file (using the quickstart sample)
25+
26+
mvn exec:java -Dexec.mainClass=com.example.speech.QuickstartSample

speech/cloud-client/pom.xml

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
<project>
17+
<modelVersion>4.0.0</modelVersion>
18+
<groupId>com.example.speech</groupId>
19+
<artifactId>speech-google-cloud-samples</artifactId>
20+
<packaging>jar</packaging>
21+
22+
<!-- Parent defines config for testing & linting. -->
23+
<parent>
24+
<artifactId>doc-samples</artifactId>
25+
<groupId>com.google.cloud</groupId>
26+
<version>1.0.0</version>
27+
<relativePath>../..</relativePath>
28+
</parent>
29+
30+
<properties>
31+
<maven.compiler.target>1.8</maven.compiler.target>
32+
<maven.compiler.source>1.8</maven.compiler.source>
33+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
34+
</properties>
35+
36+
<dependencies>
37+
<!-- [START dependencies] -->
38+
<dependency>
39+
<groupId>com.google.cloud</groupId>
40+
<artifactId>google-cloud-speech</artifactId>
41+
<version>0.8.1-alpha</version>
42+
</dependency>
43+
<!-- [END dependencies] -->
44+
45+
<!-- Test dependencies -->
46+
<dependency>
47+
<groupId>junit</groupId>
48+
<artifactId>junit</artifactId>
49+
<version>4.12</version>
50+
<scope>test</scope>
51+
</dependency>
52+
<dependency>
53+
<groupId>com.google.truth</groupId>
54+
<artifactId>truth</artifactId>
55+
<version>0.31</version>
56+
<scope>test</scope>
57+
</dependency>
58+
</dependencies>
59+
</project>
56.6 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
// [START speech_quickstart]
20+
// Imports the Google Cloud client library
21+
import com.google.cloud.speech.spi.v1beta1.SpeechClient;
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.nio.file.Files;
31+
import java.nio.file.Path;
32+
import java.nio.file.Paths;
33+
import java.util.List;
34+
35+
public class QuickstartSample {
36+
public static void main(String... args) throws Exception {
37+
// Instantiates a client
38+
SpeechClient speech = SpeechClient.create();
39+
40+
// The path to the audio file to transcribe
41+
String fileName = "./resources/audio.raw";
42+
43+
// Reads the audio file into memory
44+
Path path = Paths.get(fileName);
45+
byte[] data = Files.readAllBytes(path);
46+
ByteString audioBytes = ByteString.copyFrom(data);
47+
48+
// Builds the sync recognize request
49+
RecognitionConfig config = RecognitionConfig.newBuilder()
50+
.setEncoding(AudioEncoding.LINEAR16)
51+
.setSampleRate(16000)
52+
.build();
53+
RecognitionAudio audio = RecognitionAudio.newBuilder()
54+
.setContent(audioBytes)
55+
.build();
56+
57+
// Performs speech recognition on the audio file
58+
SyncRecognizeResponse response = speech.syncRecognize(config, audio);
59+
List<SpeechRecognitionResult> results = response.getResultsList();
60+
61+
for (SpeechRecognitionResult result: results) {
62+
List<SpeechRecognitionAlternative> alternatives = result.getAlternativesList();
63+
for (SpeechRecognitionAlternative alternative: alternatives) {
64+
System.out.printf("Transcription: %s%n", alternative.getTranscript());
65+
}
66+
}
67+
}
68+
}
69+
// [END speech_quickstart]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 quickstart sample.
32+
*/
33+
@RunWith(JUnit4.class)
34+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
35+
public class QuickstartSampleIT {
36+
private ByteArrayOutputStream bout;
37+
private PrintStream out;
38+
39+
@Before
40+
public void setUp() {
41+
bout = new ByteArrayOutputStream();
42+
out = new PrintStream(bout);
43+
System.setOut(out);
44+
}
45+
46+
@After
47+
public void tearDown() {
48+
System.setOut(null);
49+
}
50+
51+
@Test
52+
public void testQuickstart() throws Exception {
53+
// Act
54+
QuickstartSample.main();
55+
56+
// Assert
57+
String got = bout.toString();
58+
assertThat(got).contains("how old is the Brooklyn Bridge");
59+
}
60+
}

0 commit comments

Comments
 (0)