Search in sources :

Example 11 with SpeechClient

use of com.google.cloud.speech.v1.SpeechClient in project google-cloud-java by GoogleCloudPlatform.

the class RecognizeSpeech method main.

public static void main(String... args) throws Exception {
    // Instantiates a client
    SpeechClient speech = SpeechClient.create();
    // The path to the audio file to transcribe
    // for example "./resources/audio.raw";
    String fileName = "your/speech/audio/file.raw";
    // Reads the audio file into memory
    Path path = Paths.get(fileName);
    byte[] data = Files.readAllBytes(path);
    ByteString audioBytes = ByteString.copyFrom(data);
    // Builds the sync recognize request
    RecognitionConfig config = RecognitionConfig.newBuilder().setEncoding(AudioEncoding.LINEAR16).setSampleRateHertz(16000).setLanguageCode("en-US").build();
    RecognitionAudio audio = RecognitionAudio.newBuilder().setContent(audioBytes).build();
    // Performs speech recognition on the audio file
    RecognizeResponse response = speech.recognize(config, audio);
    List<SpeechRecognitionResult> results = response.getResultsList();
    for (SpeechRecognitionResult result : results) {
        List<SpeechRecognitionAlternative> alternatives = result.getAlternativesList();
        for (SpeechRecognitionAlternative alternative : alternatives) {
            System.out.printf("Transcription: %s%n", alternative.getTranscript());
        }
    }
    speech.close();
}
Also used : Path(java.nio.file.Path) SpeechRecognitionAlternative(com.google.cloud.speech.v1.SpeechRecognitionAlternative) RecognitionAudio(com.google.cloud.speech.v1.RecognitionAudio) ByteString(com.google.protobuf.ByteString) RecognitionConfig(com.google.cloud.speech.v1.RecognitionConfig) SpeechClient(com.google.cloud.speech.v1.SpeechClient) RecognizeResponse(com.google.cloud.speech.v1.RecognizeResponse) ByteString(com.google.protobuf.ByteString) SpeechRecognitionResult(com.google.cloud.speech.v1.SpeechRecognitionResult)

Aggregations

RecognitionConfig (com.google.cloud.speech.v1p1beta1.RecognitionConfig)10 SpeechClient (com.google.cloud.speech.v1p1beta1.SpeechClient)10 SpeechRecognitionAlternative (com.google.cloud.speech.v1p1beta1.SpeechRecognitionAlternative)10 RecognitionAudio (com.google.cloud.speech.v1p1beta1.RecognitionAudio)9 SpeechRecognitionResult (com.google.cloud.speech.v1p1beta1.SpeechRecognitionResult)9 StreamingRecognitionConfig (com.google.cloud.speech.v1p1beta1.StreamingRecognitionConfig)9 LongRunningRecognizeResponse (com.google.cloud.speech.v1p1beta1.LongRunningRecognizeResponse)8 Path (java.nio.file.Path)7 RecognizeResponse (com.google.cloud.speech.v1p1beta1.RecognizeResponse)5 StreamingRecognizeResponse (com.google.cloud.speech.v1p1beta1.StreamingRecognizeResponse)5 ByteString (com.google.protobuf.ByteString)5 LongRunningRecognizeMetadata (com.google.cloud.speech.v1p1beta1.LongRunningRecognizeMetadata)4 WordInfo (com.google.cloud.speech.v1p1beta1.WordInfo)2 ApiStreamObserver (com.google.api.gax.rpc.ApiStreamObserver)1 RecognitionAudio (com.google.cloud.speech.v1.RecognitionAudio)1 RecognitionConfig (com.google.cloud.speech.v1.RecognitionConfig)1 RecognizeResponse (com.google.cloud.speech.v1.RecognizeResponse)1 SpeechClient (com.google.cloud.speech.v1.SpeechClient)1 SpeechRecognitionAlternative (com.google.cloud.speech.v1.SpeechRecognitionAlternative)1 SpeechRecognitionResult (com.google.cloud.speech.v1.SpeechRecognitionResult)1