Skip to content

Commit e06f4a3

Browse files
authored
samples: adds export to GCS sample (#544)
* samples: adds export to GCS sample * fixed checkstyle * added missing depends in pom files
1 parent 3d47150 commit e06f4a3

File tree

3 files changed

+190
-0
lines changed

3 files changed

+190
-0
lines changed

speech/snippets/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343
<artifactId>google-cloud-speech</artifactId>
4444
</dependency>
4545
<!-- [START_EXCLUDE] -->
46+
<dependency>
47+
<groupId>com.google.cloud</groupId>
48+
<artifactId>google-cloud-storage</artifactId>
49+
</dependency>
4650
<dependency>
4751
<groupId>commons-cli</groupId>
4852
<artifactId>commons-cli</artifactId>
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2021 Google LLC
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_export_to_gcs]
20+
21+
import com.google.api.gax.longrunning.OperationFuture;
22+
import com.google.cloud.speech.v1p1beta1.LongRunningRecognizeMetadata;
23+
import com.google.cloud.speech.v1p1beta1.LongRunningRecognizeRequest;
24+
import com.google.cloud.speech.v1p1beta1.LongRunningRecognizeResponse;
25+
import com.google.cloud.speech.v1p1beta1.RecognitionAudio;
26+
import com.google.cloud.speech.v1p1beta1.RecognitionConfig;
27+
import com.google.cloud.speech.v1p1beta1.RecognitionConfig.AudioEncoding;
28+
import com.google.cloud.speech.v1p1beta1.SpeechClient;
29+
import com.google.cloud.speech.v1p1beta1.TranscriptOutputConfig;
30+
import java.io.IOException;
31+
import java.util.concurrent.ExecutionException;
32+
import java.util.stream.Collectors;
33+
34+
public class ExportToStorageBeta {
35+
36+
public static void main(String[] args) throws Exception {
37+
String inputUri = "gs://YOUR_BUCKET_ID/path/to/your/audio_file.wav";
38+
String outputStorageUri = "gs://YOUR_BUCKET_ID/output_dir_prefix/";
39+
String encoding = "LINEAR16"; // encoding of the audio
40+
int sampleRateHertz = 8000;
41+
String languageCode = "en-US"; // language code BCP-47_LANGUAGE_CODE_OF_AUDIO
42+
exportToStorage(inputUri, outputStorageUri, encoding, sampleRateHertz, languageCode);
43+
}
44+
45+
// Exports the recognized output to specified GCS destination.
46+
public static void exportToStorage(
47+
String inputUri, String outputStorageUri, String encoding, int sampleRateHertz,
48+
String languageCode)
49+
throws IOException, ExecutionException, InterruptedException {
50+
// Initialize client that will be used to send requests. This client only needs to be created
51+
// once, and can be reused for multiple requests. After completing all of your requests, call
52+
// the "close" method on the client to safely clean up any remaining background resources.
53+
try (SpeechClient speechClient = SpeechClient.create()) {
54+
RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(inputUri).build();
55+
56+
AudioEncoding audioEncoding = AudioEncoding.valueOf(encoding);
57+
58+
// Pass in the URI of the Cloud Storage bucket to hold the transcription
59+
TranscriptOutputConfig outputConfig = TranscriptOutputConfig.newBuilder()
60+
.setGcsUri(outputStorageUri).build();
61+
62+
RecognitionConfig config = RecognitionConfig.newBuilder()
63+
.setEncoding(audioEncoding)
64+
.setSampleRateHertz(sampleRateHertz)
65+
.setLanguageCode(languageCode)
66+
.build();
67+
68+
LongRunningRecognizeRequest request =
69+
LongRunningRecognizeRequest.newBuilder()
70+
.setConfig(config)
71+
.setAudio(audio)
72+
.setOutputConfig(outputConfig)
73+
.build();
74+
75+
OperationFuture<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> future =
76+
speechClient.longRunningRecognizeAsync(request);
77+
78+
System.out.println("Waiting for operation to complete...");
79+
LongRunningRecognizeResponse response = future.get();
80+
81+
System.out.println("Results saved to specified output Cloud Storage bucket.");
82+
83+
String output = response.getResultsList().stream().map(
84+
result -> String.valueOf(result.getAlternatives(0).getTranscript()))
85+
.collect(Collectors.joining("\n"));
86+
System.out.printf("Transcription: %s", output);
87+
}
88+
}
89+
}
90+
// [END speech_export_to_gcs]
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2021 Google LLC
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 com.google.api.gax.paging.Page;
22+
import com.google.cloud.storage.Blob;
23+
import com.google.cloud.storage.Storage;
24+
import com.google.cloud.storage.StorageOptions;
25+
import java.io.ByteArrayOutputStream;
26+
import java.io.PrintStream;
27+
import java.util.UUID;
28+
import org.junit.After;
29+
import org.junit.Before;
30+
import org.junit.Test;
31+
32+
public class ExportToStorageBetaTest {
33+
34+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
35+
private static final String AUDIO_STORAGE_URI =
36+
"gs://cloud-samples-data/speech/commercial_mono.wav";
37+
private static final String PREFIX = "EXPORT_TEST_OUTPUTS";
38+
private static final String OUTPUT_STORAGE_URI =
39+
String.format("gs://%s/%s/%s/", PROJECT_ID, PREFIX, UUID.randomUUID());
40+
private static final String ENCODING = "LINEAR16";
41+
private static final String LANGUAGE_CODE = "en-US";
42+
43+
private static final int SAMPLE_RATE_HERTZ = 8000;
44+
45+
private ByteArrayOutputStream bout;
46+
private PrintStream originalPrintStream;
47+
private PrintStream out;
48+
49+
private static void cleanUpBucket() {
50+
Storage storage = StorageOptions.getDefaultInstance().getService();
51+
Page<Blob> blobs =
52+
storage.list(
53+
PROJECT_ID,
54+
Storage.BlobListOption.currentDirectory(),
55+
Storage.BlobListOption.prefix(PREFIX));
56+
57+
deleteDirectory(storage, blobs);
58+
}
59+
60+
private static void deleteDirectory(Storage storage, Page<Blob> blobs) {
61+
for (Blob blob : blobs.iterateAll()) {
62+
if (!blob.delete()) {
63+
Page<Blob> subBlobs =
64+
storage.list(
65+
PROJECT_ID,
66+
Storage.BlobListOption.currentDirectory(),
67+
Storage.BlobListOption.prefix(blob.getName()));
68+
69+
deleteDirectory(storage, subBlobs);
70+
}
71+
}
72+
}
73+
74+
@Before
75+
public void setUp() {
76+
bout = new ByteArrayOutputStream();
77+
out = new PrintStream(bout);
78+
originalPrintStream = System.out;
79+
System.setOut(out);
80+
}
81+
82+
@After
83+
public void tearDown() {
84+
cleanUpBucket();
85+
System.out.flush();
86+
System.setOut(originalPrintStream);
87+
}
88+
89+
@Test
90+
public void testExportToStorageBeta() throws Exception {
91+
ExportToStorageBeta.exportToStorage(
92+
AUDIO_STORAGE_URI, OUTPUT_STORAGE_URI, ENCODING, SAMPLE_RATE_HERTZ, LANGUAGE_CODE);
93+
String got = bout.toString();
94+
assertThat(got).contains("Transcription:");
95+
}
96+
}

0 commit comments

Comments
 (0)