Search in sources :

Example 1 with AudioEncoding

use of com.google.cloud.speech.v1p1beta1.RecognitionConfig.AudioEncoding in project java-speech by googleapis.

the class ExportToStorageBeta method exportToStorage.

// Exports the recognized output to specified GCS destination.
public static void exportToStorage(String inputUri, String outputStorageUri, String encoding, int sampleRateHertz, String languageCode, String bucketName, String objectName) throws IOException, ExecutionException, InterruptedException {
    // 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);
        // Instantiates a client
        Storage storage = StorageOptions.getDefaultInstance().getService();
        // 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...");
        future.get();
        // Get blob given bucket and object name
        Blob blob = storage.get(BlobId.of(bucketName, objectName));
        // Extract byte contents from blob
        byte[] bytes = blob.getContent();
        // Get decoded representation
        String decoded = new String(bytes, "UTF-8");
        // Create json object
        JSONObject jsonObject = new JSONObject(decoded);
        // Get json string
        String json = jsonObject.toString();
        // Specefy the proto type message
        LongRunningRecognizeResponse.Builder builder = LongRunningRecognizeResponse.newBuilder();
        // Construct a parser
        JsonFormat.Parser parser = JsonFormat.parser().ignoringUnknownFields();
        // Parses from JSON into a protobuf message.
        parser.merge(json, builder);
        // Get the converted values
        LongRunningRecognizeResponse storageResponse = builder.build();
        System.out.println("Results saved to specified output Cloud Storage bucket.");
        String output = storageResponse.getResultsList().stream().map(result -> String.valueOf(result.getAlternatives(0).getTranscript())).collect(Collectors.joining("\n"));
        System.out.printf("Transcription: %s", output);
    }
}
Also used : SpeechClient(com.google.cloud.speech.v1p1beta1.SpeechClient) OperationFuture(com.google.api.gax.longrunning.OperationFuture) LongRunningRecognizeMetadata(com.google.cloud.speech.v1p1beta1.LongRunningRecognizeMetadata) AudioEncoding(com.google.cloud.speech.v1p1beta1.RecognitionConfig.AudioEncoding) RecognitionConfig(com.google.cloud.speech.v1p1beta1.RecognitionConfig) BlobId(com.google.cloud.storage.BlobId) IOException(java.io.IOException) StorageOptions(com.google.cloud.storage.StorageOptions) Collectors(java.util.stream.Collectors) LongRunningRecognizeRequest(com.google.cloud.speech.v1p1beta1.LongRunningRecognizeRequest) TranscriptOutputConfig(com.google.cloud.speech.v1p1beta1.TranscriptOutputConfig) ExecutionException(java.util.concurrent.ExecutionException) Blob(com.google.cloud.storage.Blob) LongRunningRecognizeResponse(com.google.cloud.speech.v1p1beta1.LongRunningRecognizeResponse) JSONObject(org.json.JSONObject) JsonFormat(com.google.protobuf.util.JsonFormat) Storage(com.google.cloud.storage.Storage) RecognitionAudio(com.google.cloud.speech.v1p1beta1.RecognitionAudio) LongRunningRecognizeResponse(com.google.cloud.speech.v1p1beta1.LongRunningRecognizeResponse) Blob(com.google.cloud.storage.Blob) RecognitionAudio(com.google.cloud.speech.v1p1beta1.RecognitionAudio) TranscriptOutputConfig(com.google.cloud.speech.v1p1beta1.TranscriptOutputConfig) JsonFormat(com.google.protobuf.util.JsonFormat) Storage(com.google.cloud.storage.Storage) JSONObject(org.json.JSONObject) LongRunningRecognizeRequest(com.google.cloud.speech.v1p1beta1.LongRunningRecognizeRequest) RecognitionConfig(com.google.cloud.speech.v1p1beta1.RecognitionConfig) SpeechClient(com.google.cloud.speech.v1p1beta1.SpeechClient) AudioEncoding(com.google.cloud.speech.v1p1beta1.RecognitionConfig.AudioEncoding) LongRunningRecognizeMetadata(com.google.cloud.speech.v1p1beta1.LongRunningRecognizeMetadata)

Aggregations

OperationFuture (com.google.api.gax.longrunning.OperationFuture)1 LongRunningRecognizeMetadata (com.google.cloud.speech.v1p1beta1.LongRunningRecognizeMetadata)1 LongRunningRecognizeRequest (com.google.cloud.speech.v1p1beta1.LongRunningRecognizeRequest)1 LongRunningRecognizeResponse (com.google.cloud.speech.v1p1beta1.LongRunningRecognizeResponse)1 RecognitionAudio (com.google.cloud.speech.v1p1beta1.RecognitionAudio)1 RecognitionConfig (com.google.cloud.speech.v1p1beta1.RecognitionConfig)1 AudioEncoding (com.google.cloud.speech.v1p1beta1.RecognitionConfig.AudioEncoding)1 SpeechClient (com.google.cloud.speech.v1p1beta1.SpeechClient)1 TranscriptOutputConfig (com.google.cloud.speech.v1p1beta1.TranscriptOutputConfig)1 Blob (com.google.cloud.storage.Blob)1 BlobId (com.google.cloud.storage.BlobId)1 Storage (com.google.cloud.storage.Storage)1 StorageOptions (com.google.cloud.storage.StorageOptions)1 JsonFormat (com.google.protobuf.util.JsonFormat)1 IOException (java.io.IOException)1 ExecutionException (java.util.concurrent.ExecutionException)1 Collectors (java.util.stream.Collectors)1 JSONObject (org.json.JSONObject)1