-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
samples: adds export to GCS sample (#544)
* samples: adds export to GCS sample * fixed checkstyle * added missing depends in pom files
- Loading branch information
1 parent
3d47150
commit e06f4a3
Showing
3 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
speech/snippets/src/main/java/com/example/speech/ExportToStorageBeta.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright 2021 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.speech; | ||
|
||
// [START speech_export_to_gcs] | ||
|
||
import com.google.api.gax.longrunning.OperationFuture; | ||
import com.google.cloud.speech.v1p1beta1.LongRunningRecognizeMetadata; | ||
import com.google.cloud.speech.v1p1beta1.LongRunningRecognizeRequest; | ||
import com.google.cloud.speech.v1p1beta1.LongRunningRecognizeResponse; | ||
import com.google.cloud.speech.v1p1beta1.RecognitionAudio; | ||
import com.google.cloud.speech.v1p1beta1.RecognitionConfig; | ||
import com.google.cloud.speech.v1p1beta1.RecognitionConfig.AudioEncoding; | ||
import com.google.cloud.speech.v1p1beta1.SpeechClient; | ||
import com.google.cloud.speech.v1p1beta1.TranscriptOutputConfig; | ||
import java.io.IOException; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.stream.Collectors; | ||
|
||
public class ExportToStorageBeta { | ||
|
||
public static void main(String[] args) throws Exception { | ||
String inputUri = "gs://YOUR_BUCKET_ID/path/to/your/audio_file.wav"; | ||
String outputStorageUri = "gs://YOUR_BUCKET_ID/output_dir_prefix/"; | ||
String encoding = "LINEAR16"; // encoding of the audio | ||
int sampleRateHertz = 8000; | ||
String languageCode = "en-US"; // language code BCP-47_LANGUAGE_CODE_OF_AUDIO | ||
exportToStorage(inputUri, outputStorageUri, encoding, sampleRateHertz, languageCode); | ||
} | ||
|
||
// Exports the recognized output to specified GCS destination. | ||
public static void exportToStorage( | ||
String inputUri, String outputStorageUri, String encoding, int sampleRateHertz, | ||
String languageCode) | ||
throws IOException, ExecutionException, InterruptedException { | ||
// Initialize client that will be used to send requests. This client only needs to be created | ||
// once, and can be reused for multiple requests. After completing all of your requests, call | ||
// the "close" method on the client to safely clean up any remaining background resources. | ||
try (SpeechClient speechClient = SpeechClient.create()) { | ||
RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(inputUri).build(); | ||
|
||
AudioEncoding audioEncoding = AudioEncoding.valueOf(encoding); | ||
|
||
// Pass in the URI of the Cloud Storage bucket to hold the transcription | ||
TranscriptOutputConfig outputConfig = TranscriptOutputConfig.newBuilder() | ||
.setGcsUri(outputStorageUri).build(); | ||
|
||
RecognitionConfig config = RecognitionConfig.newBuilder() | ||
.setEncoding(audioEncoding) | ||
.setSampleRateHertz(sampleRateHertz) | ||
.setLanguageCode(languageCode) | ||
.build(); | ||
|
||
LongRunningRecognizeRequest request = | ||
LongRunningRecognizeRequest.newBuilder() | ||
.setConfig(config) | ||
.setAudio(audio) | ||
.setOutputConfig(outputConfig) | ||
.build(); | ||
|
||
OperationFuture<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> future = | ||
speechClient.longRunningRecognizeAsync(request); | ||
|
||
System.out.println("Waiting for operation to complete..."); | ||
LongRunningRecognizeResponse response = future.get(); | ||
|
||
System.out.println("Results saved to specified output Cloud Storage bucket."); | ||
|
||
String output = response.getResultsList().stream().map( | ||
result -> String.valueOf(result.getAlternatives(0).getTranscript())) | ||
.collect(Collectors.joining("\n")); | ||
System.out.printf("Transcription: %s", output); | ||
} | ||
} | ||
} | ||
// [END speech_export_to_gcs] |
96 changes: 96 additions & 0 deletions
96
speech/snippets/src/test/java/com/example/speech/ExportToStorageBetaTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright 2021 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.speech; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.google.api.gax.paging.Page; | ||
import com.google.cloud.storage.Blob; | ||
import com.google.cloud.storage.Storage; | ||
import com.google.cloud.storage.StorageOptions; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.PrintStream; | ||
import java.util.UUID; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class ExportToStorageBetaTest { | ||
|
||
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); | ||
private static final String AUDIO_STORAGE_URI = | ||
"gs://cloud-samples-data/speech/commercial_mono.wav"; | ||
private static final String PREFIX = "EXPORT_TEST_OUTPUTS"; | ||
private static final String OUTPUT_STORAGE_URI = | ||
String.format("gs://%s/%s/%s/", PROJECT_ID, PREFIX, UUID.randomUUID()); | ||
private static final String ENCODING = "LINEAR16"; | ||
private static final String LANGUAGE_CODE = "en-US"; | ||
|
||
private static final int SAMPLE_RATE_HERTZ = 8000; | ||
|
||
private ByteArrayOutputStream bout; | ||
private PrintStream originalPrintStream; | ||
private PrintStream out; | ||
|
||
private static void cleanUpBucket() { | ||
Storage storage = StorageOptions.getDefaultInstance().getService(); | ||
Page<Blob> blobs = | ||
storage.list( | ||
PROJECT_ID, | ||
Storage.BlobListOption.currentDirectory(), | ||
Storage.BlobListOption.prefix(PREFIX)); | ||
|
||
deleteDirectory(storage, blobs); | ||
} | ||
|
||
private static void deleteDirectory(Storage storage, Page<Blob> blobs) { | ||
for (Blob blob : blobs.iterateAll()) { | ||
if (!blob.delete()) { | ||
Page<Blob> subBlobs = | ||
storage.list( | ||
PROJECT_ID, | ||
Storage.BlobListOption.currentDirectory(), | ||
Storage.BlobListOption.prefix(blob.getName())); | ||
|
||
deleteDirectory(storage, subBlobs); | ||
} | ||
} | ||
} | ||
|
||
@Before | ||
public void setUp() { | ||
bout = new ByteArrayOutputStream(); | ||
out = new PrintStream(bout); | ||
originalPrintStream = System.out; | ||
System.setOut(out); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
cleanUpBucket(); | ||
System.out.flush(); | ||
System.setOut(originalPrintStream); | ||
} | ||
|
||
@Test | ||
public void testExportToStorageBeta() throws Exception { | ||
ExportToStorageBeta.exportToStorage( | ||
AUDIO_STORAGE_URI, OUTPUT_STORAGE_URI, ENCODING, SAMPLE_RATE_HERTZ, LANGUAGE_CODE); | ||
String got = bout.toString(); | ||
assertThat(got).contains("Transcription:"); | ||
} | ||
} |