Skip to content

Commit 8a50ab6

Browse files
authored
Speech samples (#1036)
* speech punctuation, video samples * speech samples WIP * speech samples * wip update * Updated branch to new changes in library. For speech v1p1beta1 * Remove unused commands from README * Update streamingRecognizeFile with try-catch based on PR feedback * Update based on style guidelines
1 parent 1a8fdc6 commit 8a50ab6

File tree

7 files changed

+438
-311
lines changed

7 files changed

+438
-311
lines changed

speech/cloud-client/README.md

+31-12
Original file line numberDiff line numberDiff line change
@@ -12,51 +12,70 @@ using the [Google Cloud Client Library for Java][google-cloud-java].
1212
[speech]: https://cloud.google.com/speech/docs/
1313
[google-cloud-java]: https://github.com/GoogleCloudPlatform/google-cloud-java
1414

15-
## Quickstart
15+
## Setup
1616

1717
Install [Maven](http://maven.apache.org/).
1818

1919
Build your project with:
2020

21-
mvn clean compile assembly:single
22-
23-
### Transcribe a local audio file (using the quickstart sample)
21+
```
22+
mvn clean compile assembly:single
23+
```
2424

25-
java -cp target/speech-google-cloud-samples-1.0.0-jar-with-dependencies.jar \
26-
com.example.speech.QuickstartSample
25+
## Quickstart
26+
Transcribe a local audio file
27+
```
28+
java -cp target/speech-google-cloud-samples-1.0.0-jar-with-dependencies.jar \
29+
com.example.speech.QuickstartSample
30+
```
2731

28-
### Transcribe a local audio file (using the recognize sample)
32+
## Transcribe a audio file
33+
Transcribe a local audio file
2934
```
3035
java -cp target/speech-google-cloud-samples-1.0.0-jar-with-dependencies.jar \
3136
com.example.speech.Recognize syncrecognize ./resources/audio.raw
3237
```
3338

34-
### Asynchronously transcribe a local audio file (using the recognize sample)
39+
Asynchronously transcribe a local audio file
3540
```
3641
java -cp target/speech-google-cloud-samples-1.0.0-jar-with-dependencies.jar \
3742
com.example.speech.Recognize asyncrecognize ./resources/audio.raw
3843
```
3944

40-
### Transcribe a remote audio file (using the recognize sample)
45+
Transcribe a remote audio file
4146
```
4247
java -cp target/speech-google-cloud-samples-1.0.0-jar-with-dependencies.jar \
4348
com.example.speech.Recognize syncrecognize gs://cloud-samples-tests/speech/brooklyn.flac
4449
```
4550

46-
### Asynchronously transcribe a remote audio file (using the recognize sample)
51+
Asynchronously transcribe a remote audio file
4752
```
4853
java -cp target/speech-google-cloud-samples-1.0.0-jar-with-dependencies.jar \
4954
com.example.speech.Recognize asyncrecognize gs://cloud-samples-tests/speech/vr.flac
5055
```
5156

52-
### Synchronously transcribe an audio file and print word offsets
57+
## Transcribe a audio file and print word offsets
58+
Synchronously transcribe an audio file and print word offsets
5359
```
5460
java -cp target/speech-google-cloud-samples-1.0.0-jar-with-dependencies.jar \
5561
com.example.speech.Recognize wordoffsets ./resources/audio.raw
5662
```
5763

58-
### Asynchronously transcribe a remote audio file and print word offsets
64+
Asynchronously transcribe a remote audio file and print word offsets
5965
```
6066
java -cp target/speech-google-cloud-samples-1.0.0-jar-with-dependencies.jar \
6167
com.example.speech.Recognize wordoffsets gs://cloud-samples-tests/speech/vr.flac
6268
```
69+
70+
## Transcribe a video file
71+
Synchronously transcribe a video file
72+
```
73+
java -cp target/speech-google-cloud-samples-1.0.0-jar-with-dependencies.jar \
74+
com.example.speech.Recognize video ./resources/Google_Gnome.wav
75+
```
76+
77+
Asynchronously transcribe a video file hosted on GCS
78+
```
79+
java -cp target/speech-google-cloud-samples-1.0.0-jar-with-dependencies.jar \
80+
com.example.speech.Recognize video gs://cloud-samples-tests/speech/Google_Gnome.wav
81+
```

speech/cloud-client/pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
Copyright 2017, Google Inc.
2+
Copyright 2018, Google Inc.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -40,7 +40,7 @@
4040
<dependency>
4141
<groupId>com.google.cloud</groupId>
4242
<artifactId>google-cloud-speech</artifactId>
43-
<version>0.34.0-alpha</version>
43+
<version>0.35.0-alpha</version>
4444
</dependency>
4545
<!-- [END dependencies] -->
4646

1.7 MB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 Google Inc.
2+
* Copyright 2018 Google Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,54 +18,57 @@
1818

1919
// [START speech_quickstart]
2020
// Imports the Google Cloud client library
21-
22-
import com.google.cloud.speech.v1.RecognitionAudio;
23-
import com.google.cloud.speech.v1.RecognitionConfig;
24-
import com.google.cloud.speech.v1.RecognitionConfig.AudioEncoding;
25-
import com.google.cloud.speech.v1.RecognizeResponse;
26-
import com.google.cloud.speech.v1.SpeechClient;
27-
import com.google.cloud.speech.v1.SpeechRecognitionAlternative;
28-
import com.google.cloud.speech.v1.SpeechRecognitionResult;
21+
import com.google.cloud.speech.v1p1beta1.RecognitionAudio;
22+
import com.google.cloud.speech.v1p1beta1.RecognitionConfig;
23+
import com.google.cloud.speech.v1p1beta1.RecognitionConfig.AudioEncoding;
24+
import com.google.cloud.speech.v1p1beta1.RecognizeResponse;
25+
import com.google.cloud.speech.v1p1beta1.SpeechClient;
26+
import com.google.cloud.speech.v1p1beta1.SpeechRecognitionAlternative;
27+
import com.google.cloud.speech.v1p1beta1.SpeechRecognitionResult;
2928
import com.google.protobuf.ByteString;
3029
import java.nio.file.Files;
3130
import java.nio.file.Path;
3231
import java.nio.file.Paths;
3332
import java.util.List;
3433

3534
public class QuickstartSample {
35+
36+
/**
37+
* Demonstrates using the Speech API to transcribe an audio file.
38+
*/
3639
public static void main(String... args) throws Exception {
3740
// Instantiates a client
38-
SpeechClient speech = SpeechClient.create();
41+
try (SpeechClient speechClient = SpeechClient.create()) {
3942

40-
// The path to the audio file to transcribe
41-
String fileName = "./resources/audio.raw";
43+
// The path to the audio file to transcribe
44+
String fileName = "./resources/audio.raw";
4245

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);
46+
// Reads the audio file into memory
47+
Path path = Paths.get(fileName);
48+
byte[] data = Files.readAllBytes(path);
49+
ByteString audioBytes = ByteString.copyFrom(data);
4750

48-
// Builds the sync recognize request
49-
RecognitionConfig config = RecognitionConfig.newBuilder()
50-
.setEncoding(AudioEncoding.LINEAR16)
51-
.setSampleRateHertz(16000)
52-
.setLanguageCode("en-US")
53-
.build();
54-
RecognitionAudio audio = RecognitionAudio.newBuilder()
55-
.setContent(audioBytes)
56-
.build();
51+
// Builds the sync recognize request
52+
RecognitionConfig config = RecognitionConfig.newBuilder()
53+
.setEncoding(AudioEncoding.LINEAR16)
54+
.setSampleRateHertz(16000)
55+
.setLanguageCode("en-US")
56+
.build();
57+
RecognitionAudio audio = RecognitionAudio.newBuilder()
58+
.setContent(audioBytes)
59+
.build();
5760

58-
// Performs speech recognition on the audio file
59-
RecognizeResponse response = speech.recognize(config, audio);
60-
List<SpeechRecognitionResult> results = response.getResultsList();
61+
// Performs speech recognition on the audio file
62+
RecognizeResponse response = speechClient.recognize(config, audio);
63+
List<SpeechRecognitionResult> results = response.getResultsList();
6164

62-
for (SpeechRecognitionResult result: results) {
63-
// There can be several alternative transcripts for a given chunk of speech. Just use the
64-
// first (most likely) one here.
65-
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
66-
System.out.printf("Transcription: %s%n", alternative.getTranscript());
65+
for (SpeechRecognitionResult result : results) {
66+
// There can be several alternative transcripts for a given chunk of speech. Just use the
67+
// first (most likely) one here.
68+
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
69+
System.out.printf("Transcription: %s%n", alternative.getTranscript());
70+
}
6771
}
68-
speech.close();
6972
}
7073
}
7174
// [END speech_quickstart]

0 commit comments

Comments
 (0)